Python Conditional Statements

Conditional Statements or decision-making is a practice to allow or disallow the execution of code according to certain conditions.

Have a look at this piece of code-

num = 4

if num < 10:
    print(“The number is less than 10”)

Output

The number is less than 10.

But if you change the number to a number greater than 10 suppose 15, then nothing would be printed to the output screen.

num = 15

if num < 10:
    print(“The number is less than 10”)

The if statement in both the code pieces is a conditional statement which checks whether the number stored in the variable ‘num’ is greater than 10 or not. If the condition is true, the control goes inside the if block and execute the print statement. If the condition is false, the control will jump at the end of the if statement.

There are different types of conditional statements in python like if, if else, if…elif…else.

The if statement

We have already seen an example of if statement. An if block executes only if the written condition returns true or the Boolean value of the condition is true.

Another example-

userage = 17

if userage < 18:
    print(“You cannot vote”)

Output

You cannot vote

In the above example, since the value of userage variable is 17 (less than 18), the Boolean value of the expression “userage < 18” is true. Hence, the control will enter the if block and execute the print statement.

If else statement

The if else statements exists as a pair. If the condition written in ‘if’ is true, the code of if block will execute. If the condition is false, then the else block will be executed.

Note that only one of these two blocks will be executed at a time. Moreover, no condition is written in the else block.

userage = 17

if userage >= 18:
    print(“You can vote”)
else:
    print(“You cannot vote”)

Output

You cannot vote.

In the above example, since the condition returns false, the else block will be executed

What is the need of “else” statement?

Consider this example-

num = 1

if num < 10:
    print(“The number is lesser than 10”)
print(“This is a test”)

Output

The number is lesser than 10

This is a test

In this example, the second print is independent of any block. Therefore it will be executed every time the program runs. This means whether the condition of if statement is true or false, the second print statement will always be executed. So the user will always see the message “This is a test”. But what if you only want the user to see this message if the condition of the if statement is false. In that case, you need to use the else statement as well.

The if…elif…else statements

If you have two situations then you can use the if else statements. But what if you have more than two situations. For example, you have users with multiple age groups and you want to print separate messages for them. In that case, you should use the if…elif…else statements.

Consider this example-

userage = 40

if userage < 18:
    print(“You are a kid!”)
elif userage == 18:
    print(“You are an adult now”)
else:
    print(“You have crossed 18 years of your life”)

Output

You have crossed 18 years of your life

You can use multiple elif statement within if and else. Something like this-

if condition1:
    #some code
elif condition2:
    #some code
elif condition3:
    #some code
else:
    #some code

Notice that you cannot write any condition in the else part of the if…elif…else statements.

Logical operators “and” and “or”

If you want to merge conditions, you can use the logical operators “and” and “or”.

Consider this example-

userage = 21

if userage < 13:
    print(“You are a kid”)
elif userage > 12 and userage <= 19:
    print(“You are a teenager”)
elif userage > 19 and userage < 40:
    print(“You are an adult and under 40”)
else:
    print(“You are over 40 years”)

Output

You are an adult and under 40

If  two conditions are joined using “and” operator, then the overall expression will return true only if both the conditions are true. If any one of them is false, then the overall expression is false.

On the other hand, if two conditions “or” joined using “or” operator, then the overall expression will be true only if any one of them is true. If both the conditions are false, the overall expression is false.