Decision-making statements in Python allow a program to choose different paths of execution based on whether a condition is True or False.
In real life, we make decisions like:
Python provides the following decision-making statements:
| Statement | Purpose |
|---|---|
if | Executes a block only if the condition is True |
if-else | Executes one block if True, another if False |
if-elif-else | Checks multiple conditions in sequence |
Nested if | An if inside another if |
{}.:.True or False.0, 0.0, '', [], (), {}, None, and False are considered False; everything else is True.if Statementif condition:
statement(s)
condition is evaluated.True, the indented block is executed.False, the block is skipped.# Program to check if a number is positive
num = int(input("Enter a number: "))
if num > 0:
print("The number is positive.")
print("Program ended.")
Explanation: If the user enters 7, the condition num > 0 is True, so the message is printed. The last line print("Program ended.") is outside the if block (no indentation), so it always executes.
# Program to check voting eligibility
age = int(input("Enter your age: "))
if age >= 18:
print("You are eligible to vote.")
Note: If age is less than 18, nothing is printed because there is no else block.
if-else Statementif condition:
statement(s) # executes if condition is True
else:
statement(s) # executes if condition is False
How it works: Exactly one of the two blocks will execute — never both, never neither.
# Program to check even or odd
num = int(input("Enter a number: "))
if num % 2 == 0:
print(num, "is even.")
else:
print(num, "is odd.")
Explanation: num % 2 == 0 is True for even numbers → if block runs. Otherwise → else block runs.
# Program to find the larger of two numbers
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
if a > b:
print(a, "is larger.")
else:
print(b, "is larger.")
if-elif-else Statementif condition1:
statement(s)
elif condition2:
statement(s)
elif condition3:
statement(s)
else:
statement(s) # optional
How it works:
True executes its block.True, the else block runs (if present).elif blocks.else is optional.# Program to assign grade based on marks
marks = int(input("Enter marks (0-100): "))
if marks >= 90:
grade = "A+"
elif marks >= 80:
grade = "A"
elif marks >= 70:
grade = "B"
elif marks >= 60:
grade = "C"
elif marks >= 50:
grade = "D"
else:
grade = "F"
print("Your grade is:", grade)
Explanation: If marks = 85, marks >= 90 is False, marks >= 80 is True → grade = "A". The remaining elif and else are skipped.
# Program to classify a character
ch = input("Enter a character: ")
if len(ch) != 1:
print("Please enter exactly one character.")
elif ch.isalpha():
if ch.lower() in "aeiou":
print(ch, "is a vowel.")
else:
print(ch, "is a consonant.")
else:
print(ch, "is not a letter.")
Note: This example uses nested if inside an elif — a preview of the next topic.
# Simple calculator
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
op = input("Enter operator (+, -, *, /): ")
if op == "+":
result = a + b
elif op == "-":
result = a - b
elif op == "*":
result = a * b
elif op == "/":
if b != 0:
result = a / b
else:
result = "Error: Division by zero"
else:
result = "Invalid operator"
print("Result:", result)
if StatementsDefinition: A nested if is an if (or if-else, or if-elif-else) statement placed inside another if, elif, or else block.
if condition1:
if condition2:
statement(s)
else:
statement(s)
else:
statement(s)
When to use:
# Program to find the largest of three numbers
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a >= b:
if a >= c:
largest = a
else:
largest = c
else:
if b >= c:
largest = b
else:
largest = c
print("The largest number is:", largest)
Explanation: First, compare a and b. If a >= b, then compare a with c. Otherwise, compare b with c.
# Program to check leap year
year = int(input("Enter a year: "))
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print(year, "is a leap year.")
else:
print(year, "is not a leap year.")
else:
print(year, "is a leap year.")
else:
print(year, "is not a leap year.")
Logic: A year is a leap year if:
# Nested if: sign and parity
num = int(input("Enter a number: "))
if num > 0:
print("Positive number")
if num % 2 == 0:
print("Even")
else:
print("Odd")
elif num < 0:
print("Negative number")
if num % 2 == 0:
print("Even")
else:
print("Odd")
else:
print("Zero")
| Statement | Use Case | Blocks Executed |
|---|---|---|
if | Single condition | 0 or 1 |
if-else | Two-way decision | Exactly 1 |
if-elif-else | Multiple mutually exclusive conditions | Exactly 1 (or 0 if no else) |
Nested if | Hierarchical / dependent conditions | Depends on conditions |
| Mistake | Correction |
|---|---|
Forgetting the colon : | Always end the condition with : |
| Incorrect indentation | Use consistent 4 spaces for each block |
Using = instead of == | = is assignment; == is comparison |
Writing elif as else if | Python uses elif |
Overlapping conditions in elif | Order matters — put the most specific first |
| Empty block | Use pass if a block must be empty |
passif x > 0:
pass # to be implemented later
else:
print("Non-positive")
| Concept | Syntax | Example |
|---|---|---|
if | if cond: | if x > 0: print("+") |
if-else | if cond: ... else: ... | if x%2==0: ... else: ... |
if-elif-else | if c1: ... elif c2: ... else: ... | Grade calculator |
Nested if | if c1: if c2: ... | Largest of 3 numbers |
if.ax² + bx + c = 0 (real or complex).if-elif-else.if → single path; if-else → two paths; if-elif-else → multiple paths.if handles hierarchical conditions.== for comparison, and : after each condition.Module III — Flow Control · Topic (a): Decision Making · Detailed Teaching Notes