Python If..Elif..Else Conditional

If..Elif..Else conditionals are an important part of the Python. If..Elif..Else conditionals are used to make decisions and change the flow of the application according to the given conditions. If..Elif..Else conditional can be used in different ways and under different conditions.

if Statement

The most basic usage of the If..Elif..Else conditional using single if statement where single condition will be checked. If the checked condition is true the if statement code block will be executed and if not true the if statement code block will be not executed.

if CONDITION:
   CODE_BLOCK
   CODE_BLOCK
   ...
  • CONDITION is a condition that can also consist of multiple sub-conditions to check. If the CONDITION is true the CODE_BLOCK is executed.
  • CODE_BLOCK is the single or more lines that will be executed if the CONDITION is true.

Let’s make an example where we will check if the age variable is higher then 20. If the age is higher than 20 we will print some message.

age = 25

if age > 20:
   print("Age is higher then 20.")

The condition is true because age is 25 and this is higher then 20. So the if statement code block will be executed and the following will be the output.

Age is higher then 20.

Lets change the age variable value and execute again the following code.

age = 18

if age > 20:
   print("Age is higher then 20.")

As the age>20 condition will return false because 18 is not higher then 20. So the if code block will not executed and there will be no output.


if else Statement

if else statement is used to check two conditions. If the specified condition is true the if code block is executed if not true the else code block executed. The syntax of if else is like below.

if CONDITION:
   IF_CODE_BLOCK
else:
   ELSE_CODE_BLOCK
  • CONDITION is a condition that can also consist of multiple sub-conditions to check. If the CONDITION is true the IF_CODE_BLOCK is executed. If the CONDITION is not true the ELSE_CODE_BLOCK is executed.
  • IF_CODE_BLOCK is a code block that can consist of single or multiple lines executed if the CONDITION is true.
  • ELSE_CODE_BLOCK is a code block that can consist of single or multiple lines executed if the CONDITION is false.

Lets make an example to understand better the if and else statements. We will use out previous example where we will check if the age variable value is higher then 20 or not and print a message accordingly.

age = 25

if age > 20:
   print("Age is higher then 20.")
else:
   print("Age is lower then 20.")

As age is 25 which is higher then 20 the if condition will be true and its code block will be executed. The output will be like below.

Age is higher then 20.

Lets make another example where the age value is 18 which is lower then 20. age > 20 will return false and the else code block will be executed.

age = 18

if age > 20:
   print("Age is higher then 20.")
else:
   print("Age is lower then 20.")

The output will be like below.

Age is lower then 20.

if elif else Statement

If there is more than one conditions we can use the elif statement between the if and else. We can add more conditions with the elif statement by providing the conditions. The syntax of if..elif..else is like below. Multiple elif conditions can be added with their code blocks if required.

if CONDITION1:
   CODE_BLOCK1
elif CONDITION2:
   CODE_BLOCK2
else:
   CODE_BLOCKN
  • CONDITION1 is a condition that can also consist of multiple sub-conditions to check. If the CONDITION1 is true the CODE_BLOCK1 is executed. If the CONDITION is not true condition of the CONDITION2 is checked.
  • CONDITION2 is checked and if true the CODE_BLOCK2 is executed if not the following condition is checked if exist. When the last elif condition checked and it’s not true the else CODE_BLOCK executed.

In the following example we will check the age variable value for conditions higher then 20 and higher then 60.

age = 25

if age > 60:
   print("Age is higher then 60.")
elif age > 20:
   print("Age is between 20 and 60.")
else:
   print("Age is lower then 20.")

The age > 60 will return false as age is 25 and the age>20 will be evaluated which will be true as 25 is higher then 20. So the matched condition code block will be executed and following output will be printed. When a condition is matched other conditions are not checked.

Age is between 20 and 60.

Single Line If Else

Python is a practical language where if..else can be used in a single line in a easily readable way. This single line if..else is also called as short hand if..else.

age = 25

print("Higher then 20.")
 if age > 20 else print("Lower then 20.")

AND Logic Use Multiple Conditions

Boolean logic contains different operations to make calculations. AND logic operation is popular and important. AND logic is used to check if given conditions are true. Take a look following AND logic calculation table.

1st2ndResult
TRUETRUETRUE
FALSEFALSEFALSE
FALSETRUEFALSE
TRUEFALSEFALSE

We can use the and logic to create multiple conditions in a single if..else statement. In the following example we will check both age and gender for different conditions. We will use and logic operator in order to met both conditions age>20 and gender=="male" etc.

age = 25
gender = "male"

if age > 20 and gender == "male":
   print("Male over 20")
elif age > 20 and gender == "female":
   print("Female over 20")
else:
   print("Age is lower then 20.")

The output will be like below.

Male over 20

OR Logic Use Multiple Conditions

OR is another boolean logic operation where check both conditions and return true if both of them or one of them is true. Look the following table for OR logic calculation.

1st2ndResult
TRUETRUETRUE
FALSEFALSEFALSE
FALSETRUETRUE
TRUEFALSETRUE
age = 25

if age < 20 or age > 60 :
   print("Age is less then 20 or over 60")
else:
   print("Age is between 20 and 60")

The output will be like below.

Age is between 20 and 60

Leave a Comment