Control Structures¶
In this lesson, we will introduce fundamental control structures (if
statements, for
loops, and while
loops) for Python. By the end of this lesson, students will be able to:
- Evaluate expressions involving arithmetic and boolean operators.
- Apply for loops to iterate over a range of numbers increasing/decreasing by a fixed step size.
- Apply while loops to iterate until reaching a desired program condition.
Jupyter Notebooks¶
Before we learn about control structures, let's learn a bit about the Jupyter Notebook data programming environment that we'll be using throughout this course. Jupyter Notebooks allow you to write formatted text descriptions (Markdown cells) alongside runnable Python code (Code cells). The text you're reading now is an example of a Markdown cell. You can double-click this text to edit it. Feel free to edit the contents of any cells that we give you: your changes only apply to your own copy.
Python code cells, like the one that appears below, can be run by clicking on the cell and then using the ▶️ play button at the top to run it. The keyboard shortcut Ctrl + Enter allows you to run the current cell, which comes in handy if you make some edits to a cell and your fingers are already on your keyboard.
Try editing the following cell to replace Kevin with your own name and then run it using the keyboard shortcut.
"Hello, my name is Zoe"
'Hello, my name is Zoe'
When running this code cell, notice how Jupyter Notebook produces an output. Usually, the output is the value of the last expression that was run. (There are some fancy ways to override the output but we won't learn how to do this.)
"Hello, my name is Kevin"
"My favorite course is CSE 163"
'My favorite course is CSE 163'
Print function¶
How can I have Python display both of the lines of text? To do this, we will need to print
out lines of text. The print
function has the side-effect of outputting the arguments as text.
print("Hello, my name is Kevin")
print("My favorite course is CSE 163")
"I also teach CSE 373"
Hello, my name is Kevin My favorite course is CSE 163
'I also teach CSE 373'
# In Python, string concatenation requires strings
"I also teach CSE" + 373 + "!"
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[21], line 1 ----> 1 "I also teach CSE" + 373 + "!" TypeError: can only concatenate str (not "int") to str
"I also teach CSE" + str(373) + "!"
'I also teach CSE373!'
"I also teach Univeresity of Washington's CSE 373"
"I also teach Univeresity of Washington's CSE 373"
"I also teach \"University of Washington's\" CSE 373"
'I also teach "University of Washington\'s" CSE 373'
'I also teach CSE 373'
'I also teach CSE 373'
This helps us display more than one line of text in a cell, but take a closer look and you'll notice more subtle details happening behind the scenes.
Before we introduced print
, the cell produced as the last line of output
'My favorite course is CSE 163'
After we introduced print
, the cell produced as the last line of output
My favorite course is CSE 163
What is different between these two outputs? Try to explain the cause for the difference.
The best explanation is:
The print function only prints the string's value.
The difference with running a code cell and seeing the output includes quotes around the string is that Jupyter Notebook wants to tell you that the result is a string.
# How do I make multiple print calls that all go on the same line?
print("Hello, my name is Kevin", end="")
print("My favorite course is CSE 163")
Hello, my name is KevinMy favorite course is CSE 163
Assignment statements¶
Variables store values. Each Python value is composed of a value and its type. Unlike Java, Python doesn't require you to define the type of a variable. Variables are assigned using assignment statements.
JupyterHub has a built-in debugger that enables you to pause the program on any line of code and inspect its state. Let's try it out now by clicking the Enable Debugger 🐞 (beetle or bug) icon at the top and then setting a breakpoint by clicking on the line number that you want to pause before running.
x = 2.4
y = 1.2
x = x / y
y = 5
Assignment statements don't produce values, so there's no output displayed. If you want to display an output, ask for the value of a variable. This expression evaluates to the current value stored in the variable y
.
y
5
Comparison operators¶
In addition to the string and numeric values that we see above, Python also has comparison operators that produce boolean (True
or False
) values.
x = 3
print(x < 4) # Is x less than 4?
print(x >= 5) # Is x greater than or equal to 5?
print(x == 2) # Is x equal to 2?
print(x != 2) # Is x not equal to 2?
True False False True
Let's try putting together everything you've seen so far. Before running the follow code cell, predict the output that will appear. (x ** 2
computes x
raised to the second power.)
x = 2.4
y = 1.2
x = x / y
y = 5
x ** 2 <= y # Is x^2 less than or equal to y?
True
The most effective way to learn all the key programming details is to predict the output of every code cell before running it. Then, you can compare your predicted output to the real output and use that to plan your next steps.
Many of our in-class coding polls will provide an opportunity to pause and predict, but you can actually apply this learning strategy in any class. Even in classes that don't involve coding, comparing what you predict to what actually happens next in a lecture is an effective way to think and stay engaged.
Conditional statements¶
Comparison operators are frequently used together with conditional statements (if
statements). Whereas Java uses curly braces {
and }
to indicate a block of code, Python relies primarily on indentation to indicate blocks of code.
if x ** 2 < y:
print("x-squared is less than y")
elif x ** 2 == y:
print("x-squared is equal to y")
else:
print("x-squared is greater than y")
x-squared is less than y
# What do we do about long lines?
if x ** 2 < y:
print("x-squared is less than y.......................",
end="")
"abc" "def" # Programming language course to understand how this works...
'abcdef'
("abc"
"def")
'abcdef'
Loops¶
Think about how you might write the following program so that it counts down from one minute decrementing by 10 seconds, producing the following predicted output.
One minute countdown
60
50
40
30
20
10
0
Done!
We can certainly write this out using a lot of print statements, but perhaps we can write a more general solution using loops instead. Lets practice converting this code into a loop.
print("One minute countdown")
print(60)
print(50)
print(40)
print(30)
print(20)
print(10)
print(0)
print("Done!")
One minute countdown 60 50 40 30 20 10 0 Done!
print("One minute countdown")
# The end point of a range is exclusive!
for i in range(60, -1, -10):
print(i)
print("Done!")
One minute countdown 60 50 40 30 20 10 0 Done!
for i in range(10): # Like range(0, 10, 1)
print(i)
0 1 2 3 4 5 6 7 8 9
for i in range(5, 10): # Like range(5, 10, 1)
print(i)
5 6 7 8 9