Flow control is an aspect of python (and programming in general) that dictates the "flow" of data in an algorithm. The most fundamental of flow controls are the if
and else
statements.
How flow controls allow us to manipulate date is best illustrated with an example.
Imagine there is a chemist in a lab. The chemist is given a sample of lithium and a sample of sodium, but they do not know which sample is which. They are asked to determine which sample is lithium and which sample is sodium. The chemists remembers from their earlier studies that lithium burns red when placed in a flame, but cannot recall the flame colour when sodium is added. Therefore, the chemist thought of the following algorithm to determine which sample is which:
1) Conduct a flame test experiment to determine the flame colour of a sample
2) If the flame is red, then the chemist knows that sample is lithium.
3) If the flame is not red, then the sample must be sodium.
The above algorithm can be converted to the python code below. Using the colour_of_flame variable they are able to determine the which sample is which. The chemist burns a sample at random and the flame is yellow. The chemist uses the algorithm above to determine the alkali metal in this sample.
colour_of_flame = "yellow" # Step 1 of the algorithm
if colour_of_flame == "red": # Step 2 of the algorithm
print("The alkali metal is lithium")
else: # Step 3 of the algorithm
print("The alkali metal is sodium")
They determine that this sample, with a yellow flame, is sodium. Therefore the other sample is lithium. The chemist can now use these alkali metals in their experiments as they please.
The clumsy chemist does, however, forget to label the samples.
They come back three months later and have the same issue, but this time their equally clumsy lab partner has also put some potassium in the cupboard. How can flow controls help the chemist solve this problem? Well for more than two potential options they can use the elif
statement. The chemist knows lithium burns red and, from the last time they had this issue, they know sodium burns yellow. The chemist writes the following algorithm to solve their predicament:
1) Conduct a flame test experiment to determine the flame colour of a sample
2) If the flame is red, then the chemist knows that sample is lithium.
3) If the flame is yellow, then the chemist knows that the sample is sodium.
4) If the flame is neither red or sodium, then the sample must be potassium.
The chemist's algorithm can be converted to the python code below. They choose a sample at random and the colour of the flame is yellow. They use this code to determine the alkali metal is sodium like so
colour_of_flame = "yellow" # Step 1 of the algorithm
if colour_of_flame == "red":# Step 2 of the algorithm
print("The alkali metal is lithium")
elif colour_of_flame == "yellow": # Step 3 of the algorithm
print("The alkali metal is sodium")
else: # Step 4 of the algorithm
print("The alkali metal is potassium")
Following this, they choose a sample at random from the two remaining samples and the colour of the flame produced is violet. Using the code above, they determine
colour_of_flame = "violet" # Step 1 of the algorithm
if colour_of_flame == "red": # Step 2 of the algorithm
print("The alkali metal is lithium")
elif colour_of_flame == "yellow": # Step 3 of the algorithm
print("The alkali metal is sodium")
else: # Step 4 of the algorithm
print("The alkali metal is potassium")
Again, they do not remember to label the samples and put all three samples back in the cupboard. The chemist comes back a further three months later and there are now five unlabelled samples in the cupboard for the five Group 1 alkali metals that are present in any significant quantities (Francium is exceedingly rare).
The chemist does not remember the flame colours of rubidium and cesium. They ask for you to help them by writing an algorithm to help determine which sample is which using a flame test like previously illustrated. By conducting a bit of research you determine that the alkali metal flame colours are:
- lithium : "red"
- sodium : "yellow"
- potassium : "violet"
- rubidium : "dark red"
- cesium : "blue"
Exercise: using the if
, else
, and elif
flow controls write an algorithm and accompanying code, similar to the ones above, to help the chemist determine which sample is which.
### Write your code here
colour_of_flame =
if colour_of_flame == "red":
print("The alkali metal is lithium")
else:
print("The alkali metal is sodium")
This can be represented by the flow chart below
The if
statement in the algorithm is represented by the green diamond box. In flowcharts, diamond boxes represent when a decision has to be made. At the if
statement the data can flow either right or down depending on the colour of the flame. In other words, the if
statements dictates the flow of the algorithm. Similarly the second algorithm,
colour_of_flame =
if colour_of_flame == "red":
print("The alkali metal is lithium")
elif colour_of_flame == "yellow":
print("The alkali metal is sodium")
else:
print("The alkali metal is potassium")
, can be represented by the following flowchart
There are two decisions to be made in this algorithm: one at the if statement and one at the elif statement. Therefore, there are two diamonds in this algorithm. Both the if
and elif
statements control the flow of the algorithm.
Hopefully this tutorial has improved your understanding of flow controls and how to use them. Armed with flow controls you are now ready for the next tutorial on and
and or
statements.