In this tutorial we look at how to use the if, else and elif statements in Python.

When coding in any language, there are times when we need to make a decision and execute some code based on the outcome of the decision.

In Python, we use the if statement to evaluate a condition.

Python If Statement

The syntax of the if statement in Python is:

if condition:
    statement

Pay particular attention to the semi-colon : and the indentation.

We use the logical operators to evaluate a condition. The logical operators are:

  • Equals: a == b
  • Not Equals: a != b
  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b

The code following the if statement is executed only if the condition evaluates to true.

Example if statement in Python:

password = 'Hello'

if len(password) < 6:
    print('password too weak - should be at least 6 characters')

Output:

password too weak - should be at least 6 characters

In the above code, we are evaluating the length of a password. The condition is that, the length should not be less than 6 characters long.

This is denoted by the less than operator <.

Since the string “Hello” is less than 6 characters, then the condition evaluates to true and hence we see the print statement.

Python If…Else Statement

If the result of an evaluation is false and we want to action on the result, then we include an else statement.

The syntax of the if...else statement looks like:

if condition:
    statement_1
else:
    statement_2

So, continuing with the same example above, if we wanted to let the user know that their password has met the required length, we put that in the else block.

Example:

password = 'Mission'

if len(password) < 6:
    print('password too weak - should be at least 6 characters')
else:
    print('your password was accepted')

Output:

your password was accepted

In this case, the word “Mission” has 7 characters so our if condition evaluates to false. Because we have an else block, then the second print() statement is executed.

Multiple If…Else With Elif

When a program needs to handle more than two cases, we need to use multiple if and else blocks. The keyword elif means else if.

For example, we have a program that needs to determine the type of a triangle based on 3 integer inputs.

  • Scaling triangle is one where all three sides have different lengths
  • Isosceles triangle has two sides of the same length
  • Equilateral triangle is one where all the sides are equal
a = 5
b = 5
c = 5

if a != b and b != c and a != c:
    print('This is a scalene triangle')
elif a == b and b == c:
    print('This is an equilateral triangle')
else:
    print('This is an isosceles triangle')

Output:

This is an equilateral triangle

This example illustrates how to handle more than two cases. Like before, remember the : and the indentations.

There is no limit on the number of elifs that we can use. There must only be one else statement which serves as a catch-all. If all the if statements fail, then the else statement is executed.

Python Ternary Operator (Shorthand If…Else)

If we have an if...else block, we can use the ternary operator and write the if...else block in one line.

The syntax is:

condition_if_true if condition else condition_if_false

Example:

a = 100
b = 200
print('A') if a > b else print('B')

Output:

B

Conclusion

  • The if...else and elif statements control the program flow.
  • The if statement in programming is used for decision making.
  • The if statement is evaluated on the basis of the specified condition(s).
  • The else block can be attached with the if statement and it is executed if the condition is false.
  • The else block cannot exist with the if statement.
  • The elif statement(s) can be attached with the if statement if there are multiple conditions.
#python