Python Logic and Control Flow

This notebook introduces Python control flow.

While

Like most programming languages Python includes a while loop. The loop executes for as long as the condition remains true.

a, b = 0, 5
while a < b:
    print(a)
    a += 1

In [ ]:
a, b = 0, 5
while a < b:
    print(a)
    a += 1

Unlike most other programming languages however Python adds an else clause to the while loop. This takes the form:

a, b = 0, 5
while a < b:
    print(a)
    a += 1 
else:
    print('loop over')

In [ ]:
a, b = 0, 5
while a < b:
    print(a)
    a += 1 
else:
    print('loop over')

Note that when the loop is exited via a break clause the else clause will not be executed.


In [ ]:
a, b = 0, 5
while a < b:
    print(a)
    a += 1 
    if a > 3:
        break
else:
    print('loop over')

If

The if statement allows the programmer to control program flow based on a boolean condition. For example:

x = int(input("Please enter an integer: "))
if x == 42:
    print('That is the answer!')

In [ ]:
x = int(input("Please enter an integer: "))
if x == 42:
    print('That is the answer')

You can also provide an else clause that will be executed if the statement is not true:

x = int(input("Please enter an integer: "))
if x == 42:
    print('That is the answer!')
else:
    print('Wrong')

You can also chain together several conditions using another condition chained on to the else. Python supports this directly with the elif clause:

x = int(input("Is this a 5 or a 30 minute argument?"))
if x == 5:
    print('That will be five pounds please')
elif x == 30:
    print('That will be ten pounds please')
else:
    print('Sorry I can only offer 5 or 30 minutes')

In [ ]:
x = int(input("Is this a 5 or a 30 minute argument?"))
if x == 5:
    print('That will be five pounds please')
elif x == 30:
    print('That will be ten pounds please')
else:
    print('Sorry I can only offer 5 or 30 minutes')

For

Unlike other programming languages Python's for loop iterates over a sequence rather than an arthimetic progression.

For example in C++ we might have:

for (int i = 0; i < limit; ++i)
{
   ...
}

In Python we would have:

for i in range(limit):
   ...

Here range is a function that returns a sequence of numbers, in the example above the sequence goes from 0 to limit - 1.

the range function accepts multiple arguments, for example:

range(5)        # the sequence 0 to 5
range(1, 5)     # the sequence 1 to 5
range(1, 10, 3) # the sequence 1, 4, 7

Finally note that range is an interesting function, try printing it below.


In [ ]:
print(range(5))

range does not return the sequence directly, instead it returns an iterable that can be repeatedly called to get the next item in the sequence. This lazy evaluation allows you to use a very big sequence without the cost of creating all the items at once.


In [ ]:


In [ ]:


In [ ]:

Python Tuple Unpacking

Tuples can be upacked:

tup = 1, 2
a, b = tup

Tuple unpacking can also be used for function calls:

tup = 3, 7
print(max(*tup))

In [ ]:
tup = 3, 7
print(max(*tup))

We can also unpack using wild cards:

tup = 1, 2, 3, 4
a, b, * = tup

This can also be done in the middle:

tup = 1, 2, 3, 4
a, *, b = tup

By convention if we want to disregard we use the underscore:

tup = 1, 2, 3, 4
a, _, _, b = tup

It is also possible to use a slice:

tup = 3, 7, 4, 1, 2
print(max(*tup[2:4]))

In [ ]:
tup = 3, 7, 4, 1, 2
print(max(*tup[2:4]))

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]: