James Howison's Data Wrangling course from the Information School at the University of Texas at Austin.
Flow of control is about how we move through a program. We cover if/else
statements and while
and for
loops. You can copy each code snippets below into a notebook and run it. Then you can follow along with the recorded lecture or experiment yourself.
"""A short script to demonstrate branching."""
# No branching
destination = "home"
print("Done with work, I'm off {}".format(destination))
But often we want to execute only some lines of the code, depending on the content of a variable.
##############
# One branch
#############
day = "Friday"
# Is it Friday yet?
if (day == "Friday"):
destination = "bar"
else:
destination = "gym"
print("Done with work, I'm off {}".format(destination))
We can combine this, taking a branch inside another branch, leading to three possible outcomes.
##########
# Two branches
##########
day = "Wednesday"
# Is it Friday yet?
if (day == "Friday"):
destination = "bar"
else:
if (day == "Wednesday"):
destination = "Park"
else:
destination = "gym"
print("Done with work, I'm off {}".format(destination))
We could split off even further. Examine the code below and draw a picture similar to the others on this page.
day = "Wednesday"
# Is it Friday yet?
if (day == "Friday"):
destination = "bar"
else:
if (day == "Wednesday"):
destination = "Park"
else:
if (day == "Tuesday"):
destination = "Sleep"
else:
destination = "gym"
print("Done with work, I'm off {}".format(destination))
When we’re mapping options in this way we can simplify the syntax using the elif
construct.
##########
# Two branches, a little less indentation, using "elif"
##########
day = "Wednesday"
# Is it Friday yet?
if (day == "Friday"):
destination = "bar"
elif (day == "Wednesday"):
destination = "Park"
else:
destination = "gym"
print("Done with work, I'm off {}".format(destination))
There is also a screencast from a previous semester that covers the if/else material. It doesn’t use notebooks but it uses the same code: Flow of Control Screencast.
Note the difference between a single =
and double ==
. The single =
is the “Assignment operator” and puts things into variables. The double ==
is the “comparison operator” and tests whether things are the same. A double ==
returns either True
or False
. You don’t want to have a =
in the parens for an if
statement.
Can you see the colon at the end of the if
and the else
lines? The colon starts a section of code, called a “block”. It’s easy to forget the colon!
if (day == "Friday"):
destination = "bar"
else:
destination = "gym"
After the colon comes an indent, we will use 4 spaces (following the Python style guide called PEP8). The block ends when we return to the previous indent level. So know you know what it means for code to be “in” the if
block or “in” the else
block.
We briefly touched on using a for
loop to iterate over items in a list. The material below provides additional insight into how those work, but it is optional, you can use for
loops in this course without understanding this material.
A while
loop is a more manual way to iterate, compared to a for
loop.
The while loop is also explained in this previous semester Screencast on While Loops. The screencast uses the code below.
This code celebrates with “hip, hip, hurray” but you can customize it
for greater anticipation (e.g., “hip, hip, hip, hip, hurray”) by changing
todo. The test on line 13 (todo > done
) is repeated after each line 15.
todo = 3
done = 0
while(todo > done):
print("hip")
done = done + 1
print("hurray")
A few questions to ask yourself:
The figures used in the screencast, showing the state of variables is below:
The for
loop allows us to iterate over lists and dictionaries. It is simpler form of the while loop.
my_list = ["zero", "one", "two", "three"]
for item in my_list:
print(f"The next item is: {item}")
could be rewritten manually using a while loop. We have to point to each item in the list one by one. We know how to point to a single item using my_list[0]
and my_list[1]
so we use a number as a counter in the while loop, comparing it to the length of the list len(my_list)
.
my_list = ["zero", "one", "two", "three"]
counter = 0
while (counter < len(my_list)):
print("The next item is: ")
print(my_list[counter])
counter = counter + 1
Notice we have to increment the counter variable at the end of each loop: counter = counter + 1
.
We can make this even more similar to the for
loop above by using a temporary variable item = my_list[counter]
and f-strings.
my_list = ["zero", "one", "two", "three"]
counter = 0
while (counter < len(my_list)):
item = my_list[counter]
print(f"The next item is: {item}")
counter = counter + 1
While the while
loop helps us understand what the for
loop is doing, we will use the for
structure a lot as we move down through the lines of a csv file (and sometimes across the fields on each line as well).