Python 101 - Flow Control in Python Code
Introduction
As we move beyond the “building blocks” of Python, i.e., scalar variables and data structures, we shift focus onto how to integrate these objects into logical workflows. Specifically, we cover conditional processing (if
… else
) and iteration using two type of loops: for
and while
.
This session builds right off the last session again using VanderPlas’ Whirlwind Tour of Python as a guide. This document highlights the key concepts you should know, and then we cementing these concepts with a set of exercises to work on within your Jupyter environment.
Topics and Learning Objectives
Topic | On completion, you should be able to… |
---|---|
1. Conditional Statements | ♦ Use colons and whitespace to define code blocks ♦ Construct simple and compound Boolean conditions ♦ Conditionally execute code blocks using if ♦ Describe why else might be used with an if statement ♦ Describe wh y elif might be used with an if statement |
2. Iteration with for loops |
♦ Iterate through items in a collection using a for loop♦ Use the variable created in the for loop within the repeated code |
3. Iterators and the range() function |
♦ Iterate through sequential numbers using range() w/ a for loop♦ Modify the range function to count via steps and backwards ♦ Describe what an iterator is relative to a list object and why it is useful |
4. Iteration with while loops |
♦ Explain the difference between a for loop and a while loop ♦ Iterate until a condition is met using a while loop♦ Explain why it’s necessary that the evaluator changes inside a while loop♦ Use Jupyter’s interrupt command to exit out of an infinite loop |
5. Fine tuning loops with break and continue |
♦ Use the break statement in a loop to end the loop♦ Use the continue statement in a loop to skip to its next iteration |
1. Conditional Statements: if
-elif
-else
Reading: WToP: 07-Control-Flow-Statements.ipynb
Key Concepts:
- Using the
if
statement to control what code gets run. - Code blocks in Python and the importance of colons (
:
) and white pace in constructing them. - Constructing simple and compound Boolean conditions for use in conditional statements.
- How
elif
can be used to add more conditional code blocks. - How
else
can be used to run code when all theif
’s andelif
’s are not met.
Exercises:
- 3.1.1 and 3.1.2 in
PythonExercises3.ipynb
2. Iteration with for
loops
Reading: WToP: 07-Control-Flow-Statements.ipynb - for loops
Key Concepts:
- Using
for
loops to iterate through items in a collection - The structure and syntax of
for
loops:- The use of
:
and indentation to define the code blocks to repeat
- The use of
- How the variable used in
for
loops is used in the repeated code block:- In
for x in [1,2,3]:
, the value ofx
changes with each iteration of the code block
- In
Exercises:
- 3.2.1 thru 3.2.4 in
PythonExercises3.ipynb
3. [Iterators and] The range()
function
Reading: WToP: 10-Iterators.ipynb
Key Concepts:
- The
range()
function to generate a collection of numbers. - → Python v2 vs v3: use
list
withrange()
in v3 to show values- Because in Python 3,
range()
returns an iterator object…
- Because in Python 3,
- Setting only the end point:
range(20)
- Setting the start and end point
range(10,20)
- Setting the start and end points, and the step (
range(10,20,2)
) - Counting down by making the step negative (range(10,0,-1))
- Using the
range()
function in afor
loop to iterate a set amount of times.
Exercises:
- 3.3.1 thru 3.3.3 in
PythonExercises3.ipynb
4. Iteration with while
loops
Reading: WToP: 07-Control-Flow-Statements.ipynb - while loops
Key Concepts:
- Using
while
to iterate until a condition is no longer met - The structure of the
while
loop syntax- The use of
:
, evaluation condition, and indentation to define the code blocks to repeat
- The use of
- The need to define a variable that gets evaluated as True/False before starting the
while
loop - The need to change the variable that gets evaluated within the
while
loop - How to stop infinite loops (interrupting the kernel in Jupyter)
Exercises:
- 3.4.1 thru 3.4.2 in
PythonExercises3.ipynb
5. Fine-Tuning your loops w/ break
and continue
Reading: WToP: 07-Control-Flow-Statements.ipynb - break & continue
Key Concepts:
- The
break
statement breaks-out of the loop entirely - The
continue
statement skips the remainder of the current loop, and goes to the next iteration
Exercises:
- 3.5.1 thru 3.5.2 in
PythonExercises3.ipynb
Recap - What’s next
Ok. That’s a lot of Python without much context of what we can actually do with it. You would be ahead of the game if you have all this material neatly packed in your head. More realistically, you now have a basic feel for how the language works but maybe still have to jump back and forth between the documentation to get it right. And that’s fine! (I hope it is, because I still do it quite often and I’ve been using Python for over a decade!)
So, what’s next is putting all this knowledge into context with examples of what Python can do for you, and how to go about doing that. We’ll explore some additional tools that help you code and techniques that should allow you to code more effectively and efficiently.