Flow control is essential for any programming language as this is the means by which a program can make decisions and repeat pieces of code. Without flow control, Python would not be more than a calculator.
Table of contents
We are continuing from the previous lesson in the “work directory” ~/PHY432/03_python
. We will use ipython
and your text editor.
Loops
Computers are excellent at repeating instructions. Looping constructs are used to repeat a block of code for a fixed number of iterations or until a given condition is met.
The for
loop
The for
statement executes a code block (the “body” of the for-loop) once for each item in a sequence:
- White-space indentation demarcates a block: Convention: Use 4 spaces for each indentation level (not TABS)
VARIABLE
is set to each item ofSEQUENCE
in turn. You can useVARIABLE
in the loop body.SEQUENCE
can be a list, tuple, string, … any “iterable”.
Example: Convert all the measured temperatures from Fahrenheit to Kelvin, using a for loop.
Create a python program temperatures.py
and run it1
to yield
288.76111111111106
298.8722222222222
310.26111111111106
309.31666666666666
311.65
316.4833333333333
Conversion complete
Exercise for loop
- create
temperature.py
as above and run it - modify
temperature.py
so that the results are stored in a new listtemp_Kelvin
Loops with range()
The range() “function” provides “standard” (as in e.g., C) for-loop behavior:
range(stop)
iterates through 0, 1, 2, …, stop-1
and gives 0 1 ... 5 6
whereas range(start, stop, step)
iterates through start, start+step, start+2*step, … with the last element less than stop:
yielding 1 3 5
.
Exercise range()
Add code to temperatures.py
(below) to print a table of the temperature in F and in K side by side by iterating through the lists temperatures
and temp_Kelvin
simultaneously.
Hints:
- You can use
len()
andrange()
2 - Assign individual values to variables
T_K
andtheta_F
and print each line with the formatprint("{0:6.1f} F {1:6.1f} K".format(theta_F, T_K))
.
List comprehensions (advanced)
We often have to generate a list from computed values as the example T
above. Instead of starting with an empty list and appending values, we can also use a list comprehension: the following code does the same as the one above:
We write a for
loop inside the list brackets where the “body” of the loop consists of a single expression that typically uses the list variable. It results in concise code that also generally executes much faster.
Exercise Generate values from -10 to 10 in steps of 0.25
Perform this activity using pair programming3.
We often need a range of values at arbitrary step size, e.g., for plotting a function. The range()
function only provides integer numbers. Find a way to generate a list of numbers from start=-10
to stop=10
(exclusive) with step=0.25
, i.e., [-10., -9.75, -9.5, ..., 9.5, 9.75]
.
Put your code into file grid.py
and store your list in the variable x_grid
.
(You can use list comprehensions. Think about how to use range()
and how to use the integers to generate the floating point numbers.)
The while
loop
The while loop (see also the while loop in the Python Tutorial) iterates over a code block until an expression (the “condition”) evaluates to False
:
The while loop may execute zero times if condition
is immediately False
. It may also run an infinite number of times when the condition is always True
.
Example: The program countup.py
will run until t
has exceeded a preset value tmax
:
Exercise reading input
A common problem is to read multiple inputs from a user until a specific input signals the end of input. The while
loop works well in this case:
Write a program alternatesum.py
that computes the “alternating sum”
from user input of \(N\) numbers \(a_k, 0 \le k < N\). Your program should
- read numbers from input until an empty line is encountered;
- store the numbers in a list
numbers
; - calculate; the “alternating sum”
numbers[0] - numbers[1] + numbers[2] - numbers[3] + ...
and store it in a variableasum
. (You can also printasum
.)
Example (optional): Fibonacci sequence
The program fibonacci.py
will compute the Fibonacci series
up to Fmax
:
Before running fibonacci.py
, let’s try to anticipate what the code will do. We will then run the code. If we get the expected results then, great!, otherwise: try to figure out why.
Anticipating results and comparing to the run afterwards is the scientific method applied to programming. It is essential if you want to become a good programmer.
Conditionals
Any programming language needs to be able to make decisions. Python has the if statement.
Example: Consider an implementation of the Heaviside step function
\[\Theta(x) = \begin{cases} 0 & x < 0 \\ \frac{1}{2} & x = 0\\ 1 & x > 0 \end{cases}\]as heaviside.py
Activity (optional): Step function
Run heaviside.py
for various values of x
(at least -3, 0, 3) and test the output against the mathematical definition.
Exercise Guessing game I
Write a program guessinggame.py
that takes a single number guess
as input and compares it to a preset integer number secret_number
. Tell the player if their guess was “too low”, “too high”, or that they “guessed the number”. You can start with the code below:
BONUS: If you want to enhance this code you can try to generate a random number using the random.randint() function. (You will learn more about importing modules and functions later in this lesson.)
Activity (optional): Guessing game II
Perform this activity using pair programming3.
Enhance the simple guessing game from the previous exercise so that the player can guess repeatedly. Count the number of guesses and print them once the player guesses correctly.
You can use the following incomplete code as a starting point:
break
and continue
The break
statement ends a for
or while
loop.
The following example reads data from input until the special word “STOP” is encountered:
The for
loop can also have an else
clause that is only executed when the loop exits normally (i.e., not via a break
). We will use this feature when checking for convergence of algorithms, using a pattern similar to the following:
The continue
statement immediately starts the next loop iteration.
The following example only prints something every 10 steps:
We can use it in parsing content to skip empty lines:
Footnotes
Remember, in
ipython
you can also run the program with%run temperatures.py
. ↩If you already know how to solve the problem with
len()
,range()
, and indexing, try to figure out how to do it more elegantly with the zip() function ↩We will try a software engineering technique called pair programming (borrowed from agile/extreme programming)
Each of the two programmers takes on one of the following roles:
- driver: has keyboard & types, runs the code
- navigator reads code, provides directions, catches bugs
Both constantly talk to each other: comment on what you’re typing, comment on what is being typed. Roles are not static but are supposed to be switched frequently.
- Split into teams of 2. (Be nice. Introduce yourselves.)
- Sit next to each other at one desk. (When doing this exercise virtually: Share a Zoom Breakout room.)
- Flip a coin to decide who will start out as the navigator and who will be the driver.
- Decide whose laptop you are going to use. (Virtually: The driver will share their screen and type and run code.)
- Switch roles every ~5 minutes. (Virtually: Maintain roles for one exercise (or switch if feasible, e.g., if you can quickly exchange code via a remote repository).)