A conditional statement is a programming construct that allows a program to make decisions and execute different blocks of code depending on whether a given condition is True or False. Without conditional statements, a program would run every line in a fixed order, with no ability to react differently to different situations.
Python uses the keywords if, elif, and else to build conditional logic. These statements rely heavily on the comparison and logical operators covered in the previous lesson, so if any of those felt unclear, review them before continuing here.
1. The if Statement
The if statement executes a block of code only when a specified condition evaluates to True. If the condition is False, the block is skipped entirely.
if condition:
# code to execute if condition is Trueage = 20
if age >= 18:
print("You are eligible to vote.")Here, the condition age >= 18 evaluates to True, so the indented line runs. Change age to 15 and the program prints nothing at all, because the condition becomes False and Python simply skips the block.
Note on indentation: Python uses indentation, not curly braces, to define which lines belong inside the if block. A missing or inconsistent indent is one of the most common errors beginners run into with conditional statements.
2. The if-else Statement
The if-else statement provides an alternative block of code to execute when the condition is False. This ensures that one of the two blocks always runs.
if condition:
# runs if condition is True
else:
# runs if condition is Falseage = 15
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")3. The if-elif-else Statement
The elif (short for “else if”) keyword allows a program to check multiple conditions in sequence. Python evaluates each condition from top to bottom and executes the first block whose condition is True. If none of the conditions are true, the final else block runs.
if condition1:
# runs if condition1 is True
elif condition2:
# runs if condition1 is False and condition2 is True
else:
# runs if none of the above conditions are Truemarks = 72
if marks >= 90:
print("Grade: A+")
elif marks >= 75:
print("Grade: A")
elif marks >= 60:
print("Grade: B")
else:
print("Grade: C")In this example, Python checks marks >= 90 first. Since 72 fails that test, it moves to the next condition, marks>=75, which also fails. It then checks marks >= 60, which is True, so "Grade: B" is printed, and none of the remaining conditions are checked at all.
Important: Once a matching condition is found, Python skips every condition that comes after it, even if those conditions would also have been true. Order matters when writing elif chains.
4. Nested Conditional Statements
A nested conditional statement is an if statement placed inside another if or else block. This allows a program to check a second condition only after the first one has already been satisfied.
age = 25
has_id = True
if age >= 18:
if has_id:
print("Entry allowed.")
else:
print("ID required for entry.")
else:
print("Entry not allowed. Underage.")Here, the inner if-else only runs at all if the outer condition (age >= 18) is True. If age is below 18, Python never even looks at has_id.
5. Combining Conditions with Logical Operators
Rather than nesting, multiple conditions can often be combined directly using and, or, and not, which usually makes the code shorter and easier to read.
age = 25
has_id = True
if age >= 18 and has_id:
print("Entry allowed.")
else:
print("Entry not allowed.")This produces the same result as the nested version above, but in fewer lines. As a general rule, use logical operators when checking multiple conditions together, and reserve nested if statements for cases where the second condition genuinely depends on the outcome of the first.
Frequently Asked Questions
Conditional Statement in Python
What’s the difference between using elif and writing separate if statements?
Practice Questions
1. Write a program that checks if a number is positive, negative, or zero using if-elif-else.
2. What is the output of the following code?
x = 10
if x > 15:
print("A")
elif x > 5:
print("B")
else:
print("C")3. Rewrite the following nested if statement using a single condition with a logical operator:
if marks >= 40:
if attendance >= 75:
print("Eligible")4. Write a program that assigns a grade (A, B, C, or F) based on a student’s marks using if-elif-else.
5. Identify the error in the following code and correct it:
age = 20
if age >= 18
print("Adult")6. Write a program to check whether a given year is a leap year (divisible by 4, but not by 100 unless also divisible by 400).
