The content for this site available on GitHub. If you want to launch the notebooks interactively click on the binder stamp below. Binder

Contents | Data Structures in Python >

Control Flow in Python

Purpose: To help you get practiced and comfortable with the topics outlined below.

Recomended Usage

  • Run each of the cells (Shift+Enter) and edit them as necessary to solidify your understanding
  • Do any of the exercises that are relevant to helping you understand the material

Topics Covered

  • Conditional Statements
  • Loops

Workbook Setup

The following magics will reload all modules before executing a new line and help make sure you follow PEP8 code style.

In [1]:
%load_ext autoreload
%autoreload 2

%load_ext pycodestyle_magic
%pycodestyle_on

Control Flow

Controlling the execution flow of code using conditions and loops

Conditional Statements: if, elif, else

If statements are used to do specific things based on conditions. Their general syntax is below

if boolean_expression:
    # do something
elif boolean_expression:
    # do something else
else:
    # do something else

The following statements show an if followed by something that evaluates to a boolean expression (True or False).

In [2]:
if (1 < 2):
    print("one is less than two")
one is less than two
In [3]:
if 1 < 2:
    print("one is less than two")
one is less than two

We could always verify the output of the boolean expression like this

In [4]:
(1 < 2)
Out[4]:
True

The following statement uses if, elif and else.

Try running the cell. It will prompt you for input, then print a statement based on what you enter. Try to follow the logic and guess that it will print before looking at the result.

In [5]:
x = int(input("Please enter an integer: "))

if x < 0:
    x = 0
    print('Negative changed to zero')
elif x == 0:
    print('Zero')
elif x == 1:
    print('Single')
else:
    print('More')
More

Python 3.8 introduced "the walrus operator" (:=) shown below to assign values to variables as part of a larger expression.

In [10]:
# This will not work unless you are running Python 3.8+
# if (n := len(a)) > 10:
#     print(f"List is too long ({n} elements, expected <= 10)")

Loops: for, while

Loops are used to iterate over the items of any sequence. Their syntax is below.

# For-loop Syntax
for variable in iterable:
    # do something
# While-loop syntax
while boolean_expression:
    # do something
In [8]:
my_list = ['eat', 'dance', 'love']

for w in my_list:
    print(w, len(w))
eat 3
dance 5
love 4
In [9]:
a = 0

while a < 3:
    print(a)
    a += 1
0
1
2

Additional loop clauses: break, continue, else

We can also do a few more things with loops like test conditions within them, break out of them, and more. Let's look at a few below.

break breaks out of the innermost enclosing for or while loop.

In [10]:
a = 0
while a < 3:
    if a == 1:
        print("stopping at a = 1")
        break
    print(a)
    a += 1
0
stopping at a = 1

continue continues with the next iteration of the loop

In [11]:
for num in range(2, 6):
    if num % 2 == 0:
        print("Found an even number", num)
        continue
    print("Found an odd number", num)
Found an even number 2
Found an odd number 3
Found an even number 4
Found an odd number 5

else is optional and if present will be executed at the end of a loop BUT NOT when the loop is terminated by a break statement

In [12]:
for n in range(2, 10):
    for x in range(2, n):
        if n % x == 0:
            print(n, 'equals', x, '*', n//x)
            break
    else:
        # loop fell through without finding a factor
        print(n, 'is a prime number')
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3

Troubleshooting Tips

If you run into issues running any of the code in this notebook, check your version of Python and Jupyter against mine below

import sys
print(sys.version)
3.7.6 (default, Dec 31 2019, 17:12:14) 
[Clang 11.0.0 (clang-1100.0.33.16)]
!jupyter --version
jupyter core     : 4.6.1
jupyter-notebook : 6.0.2
qtconsole        : not installed
ipython          : 7.9.0
ipykernel        : 5.1.3
jupyter client   : 5.3.4
jupyter lab      : 1.2.3
nbconvert        : 5.6.1
ipywidgets       : not installed
nbformat         : 4.4.0
traitlets        : 4.3.3
In [7]:
# import sys
# print(sys.version)
In [6]:
# !jupyter --version


Contents | Data Structures in Python >