Table of Contents
Introduction
In writing a program, we often need to execute some statements only if certain condition(s) hold. The Python compound statement if
, which uses if
, elif
, and else
clauses, allows us to conditionally execute blocks of statements.
Syntax
Theif
statement.
1if test expression:
2 statement(s)
The program evaluates the test expression and will execute the statement(s) only if the test expression is True
. If the test expression is False
, the statement(s) are not executed. The statement(s) reside within what we call a code block that is indented (by the Tab key). Note also that the test expression is typically a Boolean expression with comparison and/or logical operators and ends with a colon.
Example
Theif
statement.
1x = 2
2if x > 0:
3 print(f"{x} is a positive number.")
4print("This is no longer part of the if block.")
2 is a positive number.
This is no longer part of the if block.
In the above example, since x
is positive, the test expression (x>0
) evaluates to True
and the code block (first print statement) is executed. The second print statement falls outside the if
code block since it is not indented and is therefore always executed.
The if... else Statement
The if... else
statement evaluates the test expression and will execute the if
code block only when the test expression is True
. If the expression is False
, the else
code block is executed instead. Both code blocks are indented with the tab key. The syntax is as follows:
Syntax
Theif... else
statement.
1if test expression:
2 if code block
3else:
4 else code block
Example
Theif... else
statement.
1x = 2
2if x >= 0:
3 print(f"{x} is a non-negative number.")
4else:
5 print(f"{x} is a negative number.")
2 is a non-negative number.
The test expression does not need to be a Boolean expression. It can be any Python expression that evaluates to either True
or False
.
Example
Test expression may not be Boolean expression.1y = []
2if y:
3 print(f"y is non-empty and is give by {y}.")
4else:
5 print(f"y is an empty list.")
y is an empty list.
In the above example, an empty list is used as the test expression and it evaluates to False
. Indeed, False
values include the following:
- the number zero (0)
- an empty string
- empty data structures (e.g. lists, tuples or dictionaries)
- keyword
None
ADVERTISEMENT
The if... elif... else Statement
What if you are given a number and told to write a program that determines if it is positive, zero or negative and print out the result? The preceding if... else
statement allows for only 2 possibilities. On the other hand, the if... elif... else
statement allows for mutiple possibilities.
Syntax
Theif... elif... else
statement.
1if test expression:
2 if code block
3elif test expression:
4 elif code block 1
5elif test expression:
6 elif code block 2
7 ...
8else:
9 else code block
The elif
keyword is short for else if. The statement allows for multiple elif
code blocks. If the condition for if
is False
, it checks the condition of the next elif
block and so on. If all the conditions are False, the last else
code block is executed.
Remember that only one code block is executed eventually.
Example
Theif... elif... else
statement.
1x = 0
2if x > 0:
3 print(f"{x} is a positive number.")
4elif x < 0:
5 print(f"{x} is a negative number.")
6else:
7 print(f"{x} is zero.")
0 is zero.
Example
Theif... elif... else
statement.
1score = 82
2if score >= 85:
3 print(f"You get an 'A' for your score of {score}.")
4elif score >= 75:
5 print(f"You get a 'B' for your score of {score}.")
6elif score >= 65:
7 print(f"You get a 'C' for your score of {score}.")
8elif score >= 55:
9 print(f"You get a 'D' for your score of {score}.")
10else:
11 print(f"You fail your exam! with a score of {score}.")
You get a 'B' for your score of 82.
In the above example, the grade ‘A’ is awarded to a score of 85 and above, the grade ‘B’ is awarded to a score of between 75 and 84, inclusive and so on. Note that we had used f-strings in the above example.
Nested if Statements
We can have an if... else
statement inside another if...elif...else
statement, or if... elif... else
statement inside another if... else
statement, etc. These are called nested if
statements. Any number of these statements can be nested inside one another with the appropriate indentation. As such, nested if
statements can become rather confusing and should be avoided unless absolutely necessary.
Nested if statements can be rather confusing. Therefore, they should be avoided unless absolutely necessary.
The following code does the same as a previous example of determining if a given number is positive, negative or zero. In that example, we had used the if... elif... else
statement. In this example, we use nested if
statements.
Example
Nestedif
statements.
1x = 0
2if x < 0:
3 print(f"{x} is a negative number.")
4else:
5 if x > 0:
6 print(f"{x} is a positive number.")
7 else:
8 print(f"{x} is zero.")
0 is zero.
Answer
The if
clause in the outer if... else
statement evaluates to False
. Therefore the else
code block is executed. Thereafter, the if
clause in the inner if... else
statement evaluates to False
once again. Therefore the else
code block is executed, resulting in the printing of the statement “0 is zero”.