Lecture 3: Control Flow

Here we'll go through some basic control flow so you can start to write more complicated Python code.

Each section will begin with some pseudocode - code that is not synatically correct, would not run if passed to the Python interpreter but represents the basic ideas. Pseudocode is a good way to approach any programming task.

if statements

The simplest if statement is just checking if some condition is met:

if condition:
   do a thing

But we might want to have the code do something else if the condition is not met:

if condition:
   do a thing
else:
   do a different thing

Alternatively we could have the code check multiple conditions:

if condition:
   do a thing
elif another condition:
   do a thing
else:
   do a thing

Note that we intended the body of these statements. If you've used other programming languages you might be used to seeing parentheses, but in Python whitespace at the beginning of the line is used to denote which lines are within loops, functions, or other meaningful groupings of code.

Let's have the code print 'my_var is an even number' or 'my_var is an odd number' using if conditions.


In [1]:
my_var = 6

In [2]:
if my_var % 2 == 0:
    print('my_var is an even number')
elif my_var % 2 == 1:
    print('my_var is an odd number')
else:
    print('my_var is a floating point number')


my_var is an even number

Try some different numerical values for my_var.

for loops

A for loop executes a piece of code for every value in a sequence:

for each in sequence:
    do a thing

In [3]:
favorite_numbers = [2, 3, 5, 7, 11]
for number in favorite_numbers:
    print(number)


2
3
5
7
11

In [4]:
favorite_numbers = [2, 3, 5, 7, 11]
sum_of_numbers = 0
for number in favorite_numbers:
    sum_of_numbers += number
print(sum_of_numbers)


28

In [5]:
sum(favorite_numbers)


Out[5]:
28

range()

Range is a handy function that produces a list of numbers. This is useful for use in loops in your code.

By default range() begins at 0:


In [6]:
for x in range(10):
    print(x)


0
1
2
3
4
5
6
7
8
9

But you can have range() start at a specified value:


In [7]:
for x in range(7, 10):
    print(x)


7
8
9

By default range() will increment by 1, but you can specify that as well:


In [8]:
for x in range(2, 10, 2):
    print(x)


2
4
6
8

Note that the upper limit will never be printed.

while loops

While loops repeat some code until a condition has been met:

while condition is not met:
    do a thing

In [9]:
x = 0
while x < 5:
    print(x)
    x += 1


0
1
2
3
4

break, continue, pass

If you want to prematurely exit a loop, use break:


In [10]:
favorite_numbers = [2, 3, 5, 7, 11]
for number in favorite_numbers:
    print(number)
    if number == 5:
        break


2
3
5

This can be used inside loops, but not if statements.

You can use continue to start the next iteration of the loop.


In [11]:
favorite_numbers = [2, 3, 5, 7, 11]
for number in favorite_numbers:
    if number == 5:
        continue
    print(number)


2
3
7
11

If you want to simply pass over a line, but some piece of code is required for the syntax, then use the pass statement.


In [12]:
favorite_numbers = [2, 3, 5, 7, 11]
for number in favorite_numbers:
    if number == 5:
        pass
    print(number)


2
3
5
7
11

From the output of these last two examples, we can see that pass and continue are not the same thing.