11
JulDecision Making Statements: If, If..else, Nested If..else and if-elif-else Ladder
Whether you're preparing for Python interviews, working on a Python project, or pursuing a Data Science or AI certification, understanding these conditional structures like `if`, `if-else`, `elif`, and `nested if` statements will help you write clear and efficient code.
In this Python Tutorial, we will discuss how to make use of these statements and how they can help your coding and make it easier to maintain and read.
Types of decision-making statements in Python
There are four types of decision-making statements in the Python language those are
- If statements in python
- If else statement in python
- Nested if else statement in Python
- if-elif-else Ladder in Python
- Read More: Explore all Python keywords, including if, else, and elif
- Read More: Difference between For Loop and While Loop in Python
1. If statements in Python
- If statements in Python are conditional statements that only permit the execution of a code block if a predetermined condition is met.
- The if keyword is followed by an indented block of statements that are executed if the condition is assessed and found to be true.
- If not, the code skips the if block.
Syntax of the If Statement in Python
if expression:
statement(s)
Example of an If Statement in Python Editor
num = 8
if num > 0:
print("The number is positive.")
Output
The number is positive.
Explanation:
- Variable num is assigned the value 8.
- The condition num > 0 checks if the number is positive:
- Since 8 > 0 is True, the if block executes.
- It prints "The number is positive."
Read More - 50 Python Interview Questions and Answers
2. If-else statement in Python
- If-else statements in Python are conditional statements along with the else block.
- If the condition in the if block evaluates to false, the else statement executes an alternate block of code.
Syntax of If-Else Statement in Python
if expression:
statement(s)
else:
statement(s)
Example of If If-Else Statement in Python
num = 11
if num % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
Output
The number is odd.
Explanation:
- Variable num is assigned the value 11.
- The condition num%2 == 0 checks if the number is even:
- 11 % 2 equals 1, so the condition is False.
- The else block executes and prints "The number is odd."
Read More - Python Developer Salary in India
3. Nested if-else statement in Python
- Nesting an if-else statement refers to placing one or more if-else statements inside another if statement.
- Python enables the nesting of decision-making statements, which facilitates the development of more complex decision logic.
Syntax of Nested If-Else Statement in Python
if expression1:
statement(s)
if expression2:
statement(s)
elif expression3:
statement(s)
elif expression4:
statement(s)
else:
statement(s)
else:
statement(s)
Example of Nested If-Else Statement in Python Compiler
num = 3
if num > 0:
if num % 2 == 0:
print("The number is positive and even.")
else:
print("The number is positive but odd.")
else:
print("The number is not positive.")
Output
The number is positive but odd.
Explanation:
- Variable num is assigned the value 3.
- A nested if-else structure is used:
- First, it checks if num > 0 → True.
- Then, it checks if num % 2 == 0:
- Since 3 is not divisible by 2 → False.
- Executes the else block and prints "The number is positive but odd."
4. Python if-elif-else Ladder
- An if-elif-else ladder is an order of if statements connected by elif statements.
- This enables you to check for numerous conditions and run separate code blocks based on which condition is met.
Syntax of Python if-elif-else Ladder
if condition1: # code block 1
elif condition2:
# code block 2
elif condition3:
# code block 3
else:
# default code block
Example of Python if-elif-else ladder
x = 11
if x == 2:
print("x is equal to 2")
elif x == 3:
print("x is equal to 3")
elif x == 4:
print("x is equal to 4")
else:
print("x is not equal to 2, 3, or 4")
Output
x is not equal to 2, 3, or 4
Explanation:
- Variable x is assigned the value 11.
- An if-elif-else ladder checks multiple conditions:
- If x == 2 → print "x is equal to 2"
- Else if x == 3 → print "x is equal to 3"
- Else if x == 4 → print "x is equal to 4"
- If none match → print "x is not equal to 2, 3, or 4"
- Since x is 11, none of the conditions match.
Shorthand if Statement
Shorthand if-else statements are a shorter way to write an if-else statement in Python.Syntax of the Short-Hand if Statement
expression if condition else None
Example of a Short-Hand if Statement
i = 21
if i > 15: print("i is greater than 15")
Output
i is greater than 15
Explanation:
- A variable i is assigned the value 21.
- A shorthand if statement is used to check if i > 15.
- Since the condition is true, it directly prints "i is greater than 15".
Shorthand if-else statement
If-else statements in Python are written in a clearer manner using short-hand if-else statements. They are useful when there is just one expression to evaluate for each branch of the if-else statement.Syntax of the Short Hand if-else statement
expression if condition else alternative_expression
Example of a Short Hand if-else statement
age = 12
message = "You can vote" if age >= 18 else "You cannot vote yet"
print(message)
Output
You cannot vote yet
Explanation:
- A variable age is assigned the value 12.
- The shorthand if-else (also called the ternary operator) is used to check if age >= 18.
- If true, the message becomes "You can vote".
- If false, the message becomes "You cannot vote yet".
- print(message) displays the result.
Summary
In this tutorial, we explored that Decision-making statements in Python are essential tools that allow programs to make logical choices and perform different actions based on specific conditions. These conditional constructs help in controlling the flow of execution and are foundational to writing effective and dynamic Python code. Join our Free Python Programming Course to learn more interesting concept like this in Python.
FAQs
Take our Python skill challenge to evaluate yourself!

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.