The Python programming language controls the flow of program execution through the use of conditionals, `for`

loops (including the statements `break`

and `continue`

within them), and functions.

If a particular condition is met, a certain amount of code can be executed using the `if`

statement. If the condition is not met, then we can execute the code following the `else`

statement. If the first condition is not met, we can set the next condition for the code to be executed using the `elif`

statement.

**I****nput**:

**# source_code/appendix_c_python/example09_if_else_elif.py
**x = 10
if x == 10:
print 'The variable x is equal to 10.'
if x > 20:
print 'The variable x is greater than 20.'
else:
print 'The variable x is not greater than 20.'
if x > 10:
print 'The variable x is greater than 10.'
elif x > 5:
print 'The variable x is not greater than 10, but greater ' +
'than 5.'
else:
print 'The variable x is not greater than 5 or 10.'

**Output**:

**$ python example09_if_else_elif.py
**The variable x is equal to 10.
The variable x is not greater than 20.
The variable x is not greater than 10, but greater than 5.

The `for`

loop facilitates iteration through every element in a set of elements, for example, `range`

, `python set`

, and `list`

.

**Input**:

**source_code/appendix_c_python/example10_for_loop_range.py
**print "The first 5 positive integers are:"
for i in range(1,6):
print i

**Output**:

**$ python example10_for_loop_range.py
**The first 5 positive integers are:
1
2
3
4
5

**Input**:

**source_code/appendix_c_python/example11_for_loop_list.py
**primes = [2, 3, 5, 7, 11, 13]
print 'The first', len(primes), 'primes are:'
for prime in primes:
print prime

**Output**:

**$ python example11_for_loop_list.py
**The first 6 primes are:
2
3
5
7
11
13

The `for`

loops can be exited earlier using the `break`

statement. The remainder of the cycle in the `for`

loop can be skipped using the `continue`

statement.

**Input**:

**source_code/appendix_c_python/example12_break_continue.py
**for i in range(0,10):
if i % 2 == 1: #remainder from the division by 2
continue
print 'The number', i, 'is divisible by 2.'
for j in range(20,100):
print j
if j > 22:
break;

**Output**:

**$ python example12_break_continue.py
**The number 0 is divisible by 2.
The number 2 is divisible by 2.
The number 4 is divisible by 2.
The number 6 is divisible by 2.
The number 8 is divisible by 2.
20
21
22
23

Python supports the definition of functions, which is a good way to define a piece of code that can be executed at multiple places in the program. A function is defined using the `def`

keyword.

**Input**:

**source_code/appendix_c_python/example13_function.py
**def rectangle_perimeter(a, b):
return 2 * (a + b)
print 'Let a rectangle have its sides 2 and 3 units long.'
print 'Then its perimeter is', rectangle_perimeter(2, 3), 'units.'
print 'Let a rectangle have its sides 4 and 5 units long.'
print 'Then its perimeter is', rectangle_perimeter(4, 5), 'units.'

**Output**:

**$ python example13_function.py
**Let a rectangle have its sides 2 and 3 units long.
Then its perimeter is 10 units.
Let a rectangle have its sides 4 and 5 units long.
Then its perimeter is 18 units.