In this tutorial, you learn how to work with the if-else conditionals in Python using practical examples for better understanding.
The if-else conditionals in Python make it possible to write programs that perform in a certain way under some given conditions.
Let’s say that you want to write a grade book project that determines whether a student passed or failed an examination based on the score of the student.
What if you want a program that determines whether someone is an adult or not based on the age of the person?
If the person is 18 years of age and above, it displays a message like – you are an adult.
Solving problems like this requires the use of if-else conditionals or control statements.
if statements
The if statement is used to instruct the interpreter to perform a given action under a given condition.
A typical if statement has one if statement and one action to perform. All indented lines after the if statement is treated as a block. They are only executed if only the conditional test is passed or otherwise, ignored.
The following program uses the if statement to check whether a student passed an examination.
score = 50 if score >= 40: print("passed")
As you can see from the example above, an if statement consists of a header and a body.
The header starts with the if keyword, a conditional test and a colon at the end, while the body is indented under the header, and contains lines of code to be executed if the conditional test is True.
From the above example, the program can only print passed if the conditional test – score >= 40 is True.
Hence, the if score >= 40: is the header, while the lines of code indented under it are considered the body, and in this case, it is print(“passed”).
Keep in mind that you must indent each line with the same number of spaces as Python treats code with the same level of indentation level or an equal number of whitespaces before them as a single code block.
Conditional tests
Any statement that evaluates to True or False is considered a conditional test.
A conditional test evaluates or compares two or more values to find out whether they are equal, not equal, greater or less than each other.
It can also determine whether an item is a sequence using the membership operator in. Logical operators like ==, !=, >, < >= and <= are used for conditional tests.
In an, if statement where the conditional test evaluates True, the statements inside the if statement block is executed and if it evaluates False, then they are ignored.
Checking for equality
This type of conditional test compares the value of a variable to a specific value of interest (or two operands), using the equality operator == which returns either True or False.
num = 10 num == 2 #Evaluates to False num == 10 #Evaluates to True
Checking for inequality
You can check for inequality using the not equal to != operator. If the two operands are not equal, it returns True and if they are equal, it returns False.
num = 10 num != 2 #Evaluates to True num != 10 #Evaluates to False
Tests for Comparing values
Apart from checking for equality or inequality, you can also compare two operands to determine whether one is greater, less or even equal to the other.
This is usually done with relational operators like > (greater than), < (less than), >= (greater or equal to) and <= (less or equal to) operators.
num = 10 #Evaluates to True num > 2 #Evaluates to False num < 2 #Evaluates to True num >= 2 #Evaluates to True num <= 10
Checking for membership
Using the membership operator in, you can check whether a given value is present in a sequence. The outcome is True if the value exists and False if the value does not exist.
print(5 in [1,2,3,4,5]) #output - True print(6 in [1,2,3,4,5]) #output - False
Checking for multiple conditions
You can check for multiple conditions using the and operator and the or operator as shown below:
num = 10 #Evaluates to False num < 5 and num > 6 #Evaluates to True num < 5 or num > 6
if else statement
The code block under the if statement only runs when the test condition is True.
However, you can decide to include another piece of code to run if the conditional test turns out False, using the else statement.
score = 50 if score >= 40: print('pass') else: print('fail')
In the above example, if the score is greater than 40 or equal to 40 – pass is printed. If not so, fail is printed.
However, if you want to print grades like A, B, C, E and F based on the value of the score, then you will need an elif statement.
if elif statement
The if-elif is similar if-else-if in programming languages like Java and C++. If you want to test more than one condition and have the program perform an action based on the outcomes of the tests, you can use the if elif else statements.
score = 50 if score >= 70: print('A') elif score >= 60: print('B') elif score >= 50: print('C') elif score >= 40: print('E') else: print('F') #output - C
You must know that programs are executed from top to bottom and the order of arrangements of the elif statements matter.
Take a look at the outcome in a different arrangement.
score = 50 if score >= 40: print('E') elif score >= 50: print('C') elif score >= 60: print('B') elif score >= 70: print('A') else: print('F') #output - E