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.
%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).
if (1 < 2):
print("one is less than two")
if 1 < 2:
print("one is less than two")
We could always verify the output of the boolean expression like this
(1 < 2)
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.
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')
Python 3.8 introduced "the walrus operator" (:=) shown below to assign values to variables as part of a larger expression.
# 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
my_list = ['eat', 'dance', 'love']
for w in my_list:
print(w, len(w))
a = 0
while a < 3:
print(a)
a += 1
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.
a = 0
while a < 3:
if a == 1:
print("stopping at a = 1")
break
print(a)
a += 1
continue continues with the next iteration of the loop
for num in range(2, 6):
if num % 2 == 0:
print("Found an even number", num)
continue
print("Found an odd number", num)
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
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')
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
# import sys
# print(sys.version)
# !jupyter --version