Conditional if

Conditional if statements allow programs to make decisions and execute different code based on whether certain conditions are True or False. For instance, we may want to perform a different action when a study participant is an adult or not:

IF (participant_age >= 18)
    DO SOMETHING
OTHERWISE
    DO SOMETHING ELSE

The basic syntax for conditional statements is if condition:, where condition is an expression that evaluates to either True or False, i.e. a boolean. If it’s True, the indented code block under the if statement will run. If False, it will be skipped. For example, the below code will print “You are an adult” if age is 18 or higher.

age = 18
if age >= 18:
    print("You are an adult")
You are an adult

You can also add an else clause to specify code to run if the condition is False:

age = 17
if age >= 18:
    print("You are an adult")
else:
    print("You are not an adult")
You are not an adult

For multiple conditions, you can use elif (else if) clauses to execute the first matching block.

age = 14
if age < 13:
    print("You are a child") 
elif age < 18:
    print("You are a teenager")
else:
    print("You are an adult")
You are a teenager
if statement syntax
An if statement has scaffolding as well as user-supplied parts. The scafolding is the word if and the colon : as shown below.
 ↓                ↓
if my_number > 100:
    print(my_number, "is large")
The user-supplied part is the conditional:
          ↓
if my_number > 100:
    print(my_number, "is large")
As before, the body must be indented by four spaces:
if my_number > 100:
    print(my_number, "is large")
  ↑
indentation

You can chain multiple conditions with elif using the same syntax. On the contrary, else provides a default case if no conditions are True and do not require a condition.

Exercise

To explore the behaviour of the if statement, we could edit the below code to change the value of my_number via an input function call. The input function will always return a string, even if you enter digits so we need to explicitly convert the input into an integer with the int function.

my_number = int(input("Enter a number: "))  # We can nest function calls directly

if my_number > 100:
    print(my_number, "is large")

Run the program with different inputs. Does it give you what you expect? What happens if the input is smaller than 100?

# We can nest function calls directly
my_number = int(input("Enter a number: "))  

if my_number > 100:
    print(my_number, "is large")

If my_number is smaller than 100 then the expression is false and so if does not trigger and nothing is printed:

Enter a number:  42
Enter a number:  128
128 is large
Exercise 2

Experiment with editing your code to use some different boolean statements.

Less than:

my_number = int(input("Enter a number: ")) 

if my_number < 100:
    print(my_number, "is less than 100")
Enter a number:  74
74 is less than 100


Equal to:

my_number = int(input("Enter a number: "))  

if my_number == 100:
    print(my_number, "== 100")
if my_number == 74:
    print(my_number, "== 74")
Enter a number:  74
74 == 74


Not equal to:

my_number = int(input("Enter a number: ")) 

if my_number != 100:
    print(my_number, "!= 100")
if my_number != 74:
    print(my_number, "!= 74")
Enter a number:  74
74 != 100

Ordering your options

When working out which lines of code will be run, Python will work down the list of if, elifs and else and will run the first one that matches. Once it’s matched one, it will not bother checking to see if any of those later on would have matched. This means that you should order your questions from most-specific to least-specific.

For example, if you want to do one thing for positive numbers, but something special instead for numbers greater than 100, then you should put the more specific check first:

if.py
my_number = int(input("Enter a number: "))

if my_number > 100:
    print(my_number, "is large")
elif my_number > 1:
    print(my_number, "is positive")
else:
    print(my_number, "negative")

Combining questions

It is possible to ask two or more questions in one go by combining them with and and or. So, if you want to check is a number is smaller than ten (my_number < 10) and is not equal to zero (my_number != 0), you can use:

if my_number < 10 and my_number != 0:
    ...

These combined checks can be used in both if and elif statements.