Control Flow#

Controls the order in which the code is executed.

if/elif/else#

if 2**2 == 4:
    print("Obvious!")
Obvious!

Blocks are delimited by indentation

a = 10
if a == 1:
    print(1)
elif a == 2:
    print(2)
else:
    print("A lot")
A lot

Indentation is compulsory in scripts as well. As an exercise, re-type the previous lines with the same indentation in a script condition.py, and execute the script with run condition.py in IPython.

for/range#

Iterating with an index:

for i in range(4):
    print(i)
0
1
2
3

But most often, it is more readable to iterate over values:

for word in ('cool', 'powerful', 'readable'):
    print('Python is %s' % word)
Python is cool
Python is powerful
Python is readable

while/break/continue#

Typical C-style while loop (Mandelbrot problem):

z = 1 + 1j
while abs(z) < 100:
    z = z**2 + 1
z
(-134+352j)

More advanced features

break out of enclosing for/while loop:

z = 1 + 1j
while abs(z) < 100:
    if z.imag == 0:
        break
    z = z**2 + 1

continue the next iteration of a loop.:

a = [1, 0, 2, 4]
for element in a:
    if element == 0:
        continue
    print(1. / element)
1.0
0.5
0.25

Conditional Expressions#

if <OBJECT>:#

Evaluates to False for:

  • any number equal to zero (0, 0.0, 0+0j)

  • an empty container (list, tuple, set, dictionary, …)

  • False, None

Evaluates to True for:

  • everything else

Examples:

a = 10
if a:
    print("Evaluated to `True`")
else:
    print('Evaluated to `False')
Evaluated to `True`
a = []
if a:
    print("Evaluated to `True`")
else:
    print('Evaluated to `False')
Evaluated to `False

a == b:#

Tests equality, with logics::

1 == 1.
True

a is b#

Tests identity: both sides are the same object:

a = 1
b = 1.
a == b
True
a is b
False
a = 'A string'
b = a
a is b
True

a in b#

For any collection b: b contains a :

b = [1, 2, 3]
2 in b
True
5 in b
False

If b is a dictionary, this tests that a is a key of b.

b = {'first': 0, 'second': 1}
# Tests for key.
'first' in b
True
# Does not test for value.
0 in b
False

Advanced iteration#

Iterate over any sequence:

You can iterate over any sequence (string, list, keys in a dictionary, lines in a file, …):

vowels = 'aeiouy'
for i in 'powerful':
    if i in vowels:
        print(i)
o
e
u
message = "Hello how are you?"
message.split() # returns a list
['Hello', 'how', 'are', 'you?']
for word in message.split():
    print(word)
Hello
how
are
you?

Warning

It is not safe to modify the sequence you are iterating over.

Keeping track of enumeration number#

Common task is to iterate over a sequence while keeping track of the item number.

We could use while loop with a counter as above. Or a for loop:

words = ('cool', 'powerful', 'readable')
for i in range(0, len(words)):
    print((i, words[i]))
(0, 'cool')
(1, 'powerful')
(2, 'readable')

But, Python provides a built-in function - enumerate - for this:

for index, item in enumerate(words):
    print((index, item))
(0, 'cool')
(1, 'powerful')
(2, 'readable')

Looping over a dictionary#

Use items:

d = {'a': 1, 'b':1.2, 'c':1j}
for key, val in d.items():
    print('Key: %s has value: %s' % (key, val))
Key: a has value: 1
Key: b has value: 1.2
Key: c has value: 1j

List Comprehensions#

Instead of creating a list by means of a loop, one can make use of a list comprehension with a rather self-explaining syntax.

[i**2 for i in range(4)]
[0, 1, 4, 9]