Conditional statements¶
Sources¶
This lesson is based on the Software Carpentry group’s lessons on Programming with Python.
Basics of conditional statements¶
Conditional statements can change the code behaviour based on meeting certain conditions.
Let’s take a simple example.
In [1]: temperature = 17 In [2]: 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
ifandelsestatements to determine what parts of the code to execute. Note that both lines containingiforelseend with a:and the text beneath is indented. What do these tests do? Theiftest checks to see whether the variable value fortemperatureis greater than 25. If so, ‘it is hot’ would be written to the screen. Since 17 is smaller than 25, the code beneath theelseis executed. Theelsestatement code will run whenever theiftest is false.Note
As it turns out, we all use logic similar to
ifandelseconditional 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 [3]: weather = 'Rain' In [4]: 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
ifandelseis very common, but both are not strictly required.In [5]: temperature = 13 In [6]: 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 theelif(else-if) statement.In [7]: temperature = -3 In [8]: 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.Operator Meaning <Less than <=Less than or equal to ==Equal to >=Greater than or equal to >Greater than !=Not equal to Attention
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
A,B,C, andDwould be printed to the screen? Select your answer from the poll options at https://geo-python.github.io/poll/.
We can also use
andandorto have multiple conditions.In [9]: if (1 > 0) and (-1 > 0): ...: print('Both parts are true') ...: else: ...: print('One part is not true') ...: One part is not true
In [10]: 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.
Note
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 [11]: weather = 'Rain' In [12]: wind = 'Windy' In [13]: 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.