Conditional Statements in python: if, elif, and else.


Introduction

The conditional statements in programming languages decide the direction of the flow of program execution. It is used for decision-making. In Python, there is no switch or case statement like in other programming languages to make decisions in the program. However, it has if, elif, and else statement to make a decision.

The decision is made based on the condition provided to the if and elif statement(conditional statements). However, if none of the conditions gets fulfilled, else part will be executed.

In this article, you will learn to create decisions in a Python program using different forms of if, elif, and else statements.

if – else statement

An if statement is used for conditional execution. It is used to decide whether a certain statement or block of statements will be executed or not i.e. if a certain condition is true then a block of statement is executed otherwise else statement is executed if present.

To check the condition we use logical operators and comparison operators.

Syntax:

if condition:
    Statement # Executed if the condition is True
else:
    Statement # Executed only if, if statement is False

Example:

age = int(input("Enter your age: "))
if age <= 12:
    print("You are a kid!")
else:
    print("You are an adult!")

Output:

# Output sample 1
Enter your age: 10
You are a kid!
================= 
# Output sample 2
Enter your age: 15
You are an adult!

Note: In the above code, the statements are indented. Python uses indentation to delimiter the block of code. It uses four spaces for indentation. If the statements are not indented, it will give an error “expected an intended block”.

The if statement can be also without else statement. The code is not wrong. However, if the if statement’s condition is False, it will return empty. None of the statements inside an if block will be executed except the code outside the if block will be interpreted.

For example:

age = int(input("Enter your age: "))
if age <= 12:
    print("You are a kid!")

Output:

# Output sample 1
Enter your age: 10
You are a kid!
================= 
# Output sample 2
Enter your age: 17
# Nothing will be printed because if condition is False and there are no else statements. 


Nested if statement

Nested if statements mean an if statement inside another if statement. Python allows us to nest if statements within if statements.

Syntax:

if condition 1:
    Statements
    if condition 1.1:
        Statement
    else:
         Statement # Else here is optional
else:
    Statement      # Else here is optional

Example:

age = int(input("Enter your age:"))
if age <= 12:
  print("You are a kid!")
  if age < 5:               #nested if condition
    print("and also below 5!")
  else:
    print("but not below 5.")
else:
    print("you are above 12!")

Output:

# Three possible output
Enter your age:3
You are a kid!
and also below 5!
================= 
Enter your age:7
You are a kid!
but not below 5.
================= 
Enter your age:20
you are above 12!


What elif statement does?

The keyword ‘elif’ is short for ‘else if’, and is useful to avoid excessive indentation and used when there are multiple conditions to be checked. If the condition is not true, it will check for elif condition. Otherwise, it will directly execute the else statement.

Syntax:

if condition 1:
    Statement
elif condition 2:
    Statement
elif condition 3:
    Statement
elif condition can continue………

Example:

age = int(input("Enter your age:"))
if age <= 12:
    print("You are a kid!")
elif age == 18:
    print("you are 18 and in adulthood.")
elif age >= 19:
    print("You are above 18.")

Output:

# Possible output

Enter your age:10
You are a kid!
================= 
Enter your age:18
you are 18 and in adulthood.
================= 
Enter your age:20
You are above 18.


Multiple condition in if and elif statement

The if and elif statement can execute multiple conditions at the same time. Multiple conditions can be done using logical and comparison operators.

Example:

age = int(input("Enter your age:"))
if age < 12 and age > 0:
    print("You are a kid!")
elif age > 12 and age < 25:
    print("you are a youth!")
elif age > 25 and age < 54:
    print("you are a man!")
else:
    print("You are an old man!")

Output:

# Output
Enter your age:8
You are a kid!
================= 
Enter your age:23
you are a youth!
================= 
Enter your age:45
you are a man!
================= 
Enter your age:70
You are an old man!
When to use pass statement

The pass statement does nothing. It can be used when a statement is required syntactically correct but the program requires no action.

The pass can be also used as a place-holder for a function or conditional body when you are working on a new code, allowing you to keep thinking at a more abstract level. The pass is silently ignored.

Example:

age = 13
if age <= 12:
    pass

In the above example, nothing will be printed and it won’t generate any error. In case, if we don’t use a pass statement when there is no statement to be executed, it will generate an error “Expected EOF while parsing”. For instance;

age = 13
if age <= 12:
# This will generate an error “Expected EOF while parsing”


Ternary Operators or Conditional Expression

This expression is used when you have only one statement to execute, one for if, and one for else, you can put it all on the same line.

Example:

age = 12
print("You are a kid!") if age < 18 else print("you are not kid!")

Output:

You are a kid!

Moreover, it can be also used when you have only one statement to execute, you can put it on the same line as the if statement.

Example:

age = 12
if age == 12: print("You are 12 years old!")

Output:

You are 12 years old!