Basics of conditional statements¶
Conditional statements can change the code behaviour based on meeting certain conditions.
Let’s take a simple example.
In : temperature = 17 In : if temperature > 25: ...: print('it is hot') ...: else: ...: print('it is not hot') ...: it is not hot
What did we do here? First, we used the
elsestatements to determine what parts of the code to execute. Note that both lines containing
elseend with a
:and the text beneath is indented. What do these tests do? The
iftest checks to see whether the variable value for
temperatureis greater than 25. If so, ‘it is hot’ would be written to the screen. Since 17 is smaller than 25, the code beneath the
elseis executed. The
elsestatement code will run whenever the
iftest is false.
As it turns out, we all use logic similar to
elseconditional statements daily. Imagine you’re getting ready to leave your home for the day and want to decide what to wear. You might look outside to check the weather conditions. If it is raining, you will wear a rain jacket. Otherwise, you will not. In Python we could say:
In : weather = 'Rain' In : if weather == 'Rain': ...: print('Wear a raincoat') ...: else: ...: print('No raincoat needed') ...: Wear a raincoat
Note here that we use the
==to test if a value is exactly equal to another.
The combination of
elseis very common, but both are not strictly required.
In : temperature = 13 In : if temperature > 25: ...: print('13 is greater than 25') ...:
Note that here we use only the
ifstatement, and because 13 is not greater than 25, nothing is printed to the screen.
We can also have a second test for an
ifstatment by using the
In : temperature = -3 In : if temperature > 0: ...: print(temperature, 'is above freezing') ...: elif temperature == 0: ...: print(temperature, 'is freezing') ...: else: ...: print(temperature, 'is below freezing') ...: -3 is below freezing
Makes sense, right? Note here that we again use the
==to test if a value is exactly equal to another. The complete list of these comparison operators is given in the table below.
Less than or equal to
Greater than or equal to
Not equal to
Time to check your understanding. Let’s assume that yesterday it was 14°C, it is 10°C outside today, and tomorrow it will be 13°C. The following code compares these temperatures and prints something to the screen based on the comparison.
yesterday = 14 today = 10 tomorrow = 13 if yesterday <= today: print('A') elif today != tomorrow: print('B') elif yesterday > tomorrow: print('C') elif today == today: print('D')
Which of the letters
Dwould be printed to the screen? Select your answer from the poll options at https://geo-python.github.io/poll/.
We can also use
orto have multiple conditions.
In : if (1 > 0) and (-1 > 0): ...: print('Both parts are true') ...: else: ...: print('One part is not true') ...: One part is not true
In : if (1 < 0) or (-1 < 0): ....: print('At least one test is true') ....: At least one test is true
These are just simple examples, but concepts that can be quite handy.
Again, making decisions based on multiple conditions is something we regularly do. Imagine that we consider not only the rain, but also whether or not it is windy. If it is windy and raining, we’ll just stay home. Otherwise, we need appropriate clothing to go out. We can again handle this kind of decision with Python.
In : weather = 'Rain' In : wind = 'Windy' In : if (weather == 'Rain') and (wind == 'Windy'): ....: print('Just stay home') ....: elif weather == 'Rain': ....: print('Wear a raincoat') ....: else: ....: print('No raincoat needed') ....: Just stay home
As you can see, we better just stay home if it is windy and raining.