Conditional statements

Conditional statements#

if statements#

Sometimes, when we loop through a list, the action we want to perform depends on the value of the current item. For example, let’s suppose the list temp_list represents a sequence of temperatures (in Celsius), and we want to print a warning message when the temperature is above 35°C. The for loop may look like the following:

# list of temperature in Celsius
temp_list = [28, 34, 19, 22, 25, 37, 15, 18, 9, 4, 17]
# print a warning message ONLY IF the temperature is above 35
for temp in temp_list:
    if temp > 35:
        print("Temperature " + str(temp) + " is above the threshold of 35 deg C")
Temperature 37 is above the threshold of 35 deg C

The new structure of the above code is the if conditional. You can also use it outside of the for loop (although, for now, this usage is pretty unnecessary):

# set the temperature variable
temp = 16

# conditional print
if temp > 35:
    print("Temperature " + str(temp) + " is above the threshold of 35 deg C")

Note that nothing is printed out in the above code, since the value of the variable temp is, indeed, below 35

As with the for loop, the indent and the colon are parts of the syntax. The indentation tells python which block of codes should be executed only conditionally. Also note that in the example where the if conditional is nested inside a for loop, the inner-most statement is doubly-indented, since it is the block that executes only if the condition is true (if), and it is checked for every value in temp_list (for).

There is nothing wrong about putting in multiple if statement inside a for loop. For example, if we want to also print out a warning if the temperature is too low, we may do:

# print a warning message ONLY IF the temperature is above 35
for temp in temp_list:
    
    if temp > 35:
        print("Temperature " + str(temp) + " is above the threshold of 35 deg C")

    if temp < 10:
        print("Temperature " + str(temp) + " is below the threshold of 10 deg C")
Temperature 37 is above the threshold of 35 deg C
Temperature 9 is below the threshold of 10 deg C
Temperature 4 is below the threshold of 10 deg C

if-else statements#

Returning to the case where we print out a warning message only if the temperature is too high. What if we also want to print out temperature that are normal, just without the warning? We can tag an else statement after the if:

for temp in temp_list:
    if temp > 35:
        print("Temperature " + str(temp) + " is above the threshold of 35 deg C")
    else:
        print("Temperature " + str(temp) + " deg C is normal")
Temperature 28 deg C is normal
Temperature 34 deg C is normal
Temperature 19 deg C is normal
Temperature 22 deg C is normal
Temperature 25 deg C is normal
Temperature 37 is above the threshold of 35 deg C
Temperature 15 deg C is normal
Temperature 18 deg C is normal
Temperature 9 deg C is normal
Temperature 4 deg C is normal
Temperature 17 deg C is normal

Some notable observations from the above code:

  • Both the if and the else are at the same indentation level

  • Both the if line and the else line are followed by further indented blocks

  • At every step of the loop, either the if block is executed or the else block is executed

if-elif-else statements#

What if we also want to warn that the temperature is too low? Well, at any given step of the loop, we want exactly one of three things below to happen:

  • Temperature is printed out with no warning

  • Temperature is printed out with an “above threshold” warning

  • Temperature is printed out with a “below threshold” warning

To make sure that exactly one of the 3 cases is executed, we need an if-elif-else statement (“elif” here means “else if”)

for temp in temp_list:
    if temp > 35:
        print("Temperature " + str(temp) + " is above the threshold of 35 deg C")
    elif temp < 10:
        print("Temperature " + str(temp) + " is below the threshold of 10 deg C")
    else:
        print("Temperature " + str(temp) + " deg C is normal")
Temperature 28 deg C is normal
Temperature 34 deg C is normal
Temperature 19 deg C is normal
Temperature 22 deg C is normal
Temperature 25 deg C is normal
Temperature 37 is above the threshold of 35 deg C
Temperature 15 deg C is normal
Temperature 18 deg C is normal
Temperature 9 is below the threshold of 10 deg C
Temperature 4 is below the threshold of 10 deg C
Temperature 17 deg C is normal

Note that the order matters here. Python will first check the if condition. If that condition is true, it executes the if block and omit the remaining codes. Similarly, if the if condition is false and the elif condition is true, the elif block is executed and the else block is completely ignored.

This hierarchy is sometimes useful when you have to stack condition in the same direction. Example:

for temp in temp_list:
    if temp > 35:
        print("Dire warning: Temperature " + str(temp) + " is above the threshold of 35 deg C")
    elif temp > 30:
        print("Warning: Temperature " + str(temp) + " is close to the threshold of 35 deg C")
    else:
        print("Temperature " + str(temp) + " deg C is normal")
Temperature 28 deg C is normal
Warning: Temperature 34 is close to the threshold of 35 deg C
Temperature 19 deg C is normal
Temperature 22 deg C is normal
Temperature 25 deg C is normal
Dire warning: Temperature 37 is above the threshold of 35 deg C
Temperature 15 deg C is normal
Temperature 18 deg C is normal
Temperature 9 deg C is normal
Temperature 4 deg C is normal
Temperature 17 deg C is normal

Note that even though the value 37 satisfies both the if conditional and the elif conditional, only the if block is executed.

By the way, by inserting multiple elif you can check an arbitrary hierarchy of conditions. For example:

for temp in temp_list:
    if temp > 35:
        print("Dire warning: Temperature " + str(temp) + " is above the threshold of 35 deg C")
    elif temp > 30:
        print("Warning: Temperature " + str(temp) + " is close to the threshold of 35 deg C")
    elif temp < 5:
        print("Dire warning: Temperature " + str(temp) + " is below the threshold of 5 deg C")
    elif temp < 10:
        print("Warning: Temperature " + str(temp) + " is close to the threshold of 5 deg C")
    else:
        print("Temperature " + str(temp) + " deg C is normal")
Temperature 28 deg C is normal
Warning: Temperature 34 is close to the threshold of 35 deg C
Temperature 19 deg C is normal
Temperature 22 deg C is normal
Temperature 25 deg C is normal
Dire warning: Temperature 37 is above the threshold of 35 deg C
Temperature 15 deg C is normal
Temperature 18 deg C is normal
Warning: Temperature 9 is close to the threshold of 5 deg C
Dire warning: Temperature 4 is below the threshold of 5 deg C
Temperature 17 deg C is normal