Conditionals

if and if-else Statements

This page will cover the basics on if/else statements in Python.

What are if/else statements?

if/else statements are decision making statements that only execute if a certain condition is met.

Decision making is a very useful tool when it comes to programming because it allows to control the execution of our program given certain event or conditions.

The if statement

To write and if statement in Python, all you have to do is type if followed by the conditional statement, with an optional parentheses.

if (condition): # The condition must evaluate to True in order to execute the code within the block below
    # execute code in here

It is important to note the colon and the indentation after the if statement since Python will only execute the code within the block right underneath A block in python is the equivalent to 4-spaces and in most cases 1-tab.

Let’s look at some more examples.

day = "monday"

if day == "monday":
    print("Today is Monday")

# prints Today is Monday
days_in_month = 28

if (days_in_month < 30):
    print("The month is February")

# prints The month is February
number_12 = 12

if number_12 != 12:
    number_12 = 45

print(number_12)

# prints 12

Let’s breakdown each example:

  • In the first example, we set a variable called day to be equal to monday. Then, by using an if statement, we check whether day is equal to “monday.” Since that results in a True statement, we execute the code inside the block underneath and print out “Today is Monday.”
  • In the second example, we set variable days_in_month to be 28. We then check if the days_in_month has less than 30 days, which results in a rue statement, thus we print out The month is February.
  • In the final example, we have a variable called number_12 set to equal 12. We then check if our number_12 is NOT Equal to 12, which results in a False statement, thus the code in the block below does not get executed and we do not print out the number_12 variable.

Note: I’ve introduced a new ‘command’ in these examples called print. Print is special type of function that allows us to output (print) whatever is inside the parentheses.

If/elif/else Statements

There comes times when we need multiple condition to test before we can continue the execution of our program. Python allows us to do this by the use of elif statements.

elif, a contraction of else if, follows the exact same syntax as the if statement and there can be as many as you need. Lets see an example:

number = 5

if number == 3:
    print("The number is 3")
elif number == 4:
    print("The number is 4")
elif number == 5:
    print("The number is 5")
elif number == 6:
    print("The number is 6")

# Prints The number is 5

In this example, we have a variable number set to 5. Below we check whether if the number is 3, 4, 5, or 6 using if and elif statements. Since the equality evaluates to True in the 2nd elif statement, the program prints out "The number is 5"

What if no condition is satisfied by the input, but we still want to output or handle this unplanned event? This is where the else statement comes in.

The else statement will only get executed if no other if or elif statement is satisfied.

Unlike the if and elif statements, the else statement takes no condition and there can be only one at the end of any given if statement.

Here’s and example:

number = 33

if number % 2 == 0:
    print("The number is divisible by 2.")
elif number % 5 == 0:
    print("The number is divisible by 5.")
else:
    print("The number is not divisible by 2 or 5.")

# Prints The number is not divisible by 2 or 5.

In the example above, we have a variable called number set to 33. Below we check whether the number is divisible by 2 then 5 by using the modulo operator. Since 33 is neither divisible by 2 or 5, the else statement is invoked and the program prints out "The number is not divisible by 2 or 5."

Closing Remark

You may have noticed through either the examples given or your own examples that sometimes the input will satisfy more than one of the conditions specified. In that case, Python will only execute the first satisfied condition and then exit the conditional statement block.

number = 10

if number % 2 == 0:
    print("The number is divisible by 2.")
elif number % 5 == 0:
    print("The number is divisible by 5.")
else:
    print("The number is not divisible by 2 or 5.")

# Prints The number is divisible by 2.

Even though the number 10 is divisible by 2 and 5, Python will only execute the first if statement.