Conditional statements


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.

  1. 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 if and else statements to determine what parts of the code to execute. Note that both lines containing if or else end with a : and the text beneath is indented. What do these tests do? The if test checks to see whether the variable value for temperature is greater than 25. If so, ‘it is hot’ would be written to the screen. Since 17 is smaller than 25, the code beneath the else is executed. The else statement code will run whenever the if test is false.


    As it turns out, we all use logic similar to if and else conditional 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.

  2. The combination of if and else is 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 if statement, and because 13 is not greater than 25, nothing is printed to the screen.

  3. We can also have a second test for an if statment by using the elif (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


    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:
    elif today != tomorrow:
    elif yesterday > tomorrow:
    elif today == today:

    Which of the letters A, B, C, and D would be printed to the screen? Select your answer from the poll options at

  1. We can also use and and or to 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.


    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.