Python Conditional Statements
Conditional statements let your program make decisions — running different code depending on whether a condition is True or False. Python uses if, elif, and else for this. They're essential for any program that needs to react to data.
if Statement
Runs the block only if the condition is True.
age = 20
if age >= 18:
print("You are an adult")
# Output: You are an adult
Python uses indentation (4 spaces) instead of curly braces. Everything indented under if belongs to it. Missing indentation raises IndentationError.
if-else Statement
Runs one block if True, another if False.
age = 15
if age >= 18:
print("You can vote")
else:
print("You cannot vote yet")
# Output: You cannot vote yet
if-elif-else Ladder
Checks multiple conditions in order. First True wins; the rest are skipped.
marks = 78
if marks >= 90:
grade = "A+"
elif marks >= 80:
grade = "A"
elif marks >= 70:
grade = "B"
elif marks >= 60:
grade = "C"
else:
grade = "F"
print(f"Grade: {grade}")
# Output: Grade: B
Nested if
An if inside another if. Use nested conditionals when the second condition only matters if the first is True.
age = 25
has_license = True
if age >= 18:
if has_license:
print("You can drive")
else:
print("Get a license first")
else:
print("Too young to drive")
Ternary Operator (One-Line if-else)
Python allows a compact if-else in a single line.
age = 20
# Regular if-else
if age >= 18:
status = "Adult"
else:
status = "Minor"
# Same using ternary
status = "Adult" if age >= 18 else "Minor"
print(status) # Adult
Truthy and Falsy Values
In Python, some values evaluate to False in a condition even if they aren't literally False. These are called falsy values:
False0(integer zero)0.0(float zero)""(empty string)[],(),{}(empty collections)None
name = ""
if name:
print("Hello", name)
else:
print("Name is empty")
# Output: Name is empty
Combining Conditions — and, or, not
age = 22
has_id = True
if age >= 18 and has_id:
print("You can enter")
if age < 18 or not has_id:
print("Cannot enter")
if not has_id:
print("Please bring your ID")
Real-World Example — Login Validation
username = "admin"
password = "python123"
if username == "admin" and password == "python123":
print("Login successful")
elif username != "admin":
print("Invalid username")
else:
print("Wrong password")
Keep conditions simple and readable. If a condition has 3+ and/or clauses, consider breaking it into smaller variables with clear names.
Write a program that takes a number from the user. Print whether it's positive, negative, or zero. Then extend it: if positive, also print whether it's even or odd. Use if, elif, else, and nested conditions.
Quick Revision
if— runs block if condition is True.elif— checks additional conditions.else— runs if nothing matched.- Indentation is mandatory (4 spaces).
- First True condition wins in an if-elif ladder.
- Ternary:
x if cond else y. - Falsy values:
0,"",[],None,False.
Interview Questions
Q: Difference between if and elif?
A: if always checks its condition. elif only checks if the previous if/elif was False. Using elif avoids checking unnecessary conditions.
Q: What are falsy values in Python?
A: Values that evaluate to False in a boolean context: False, None, 0, 0.0, empty strings, empty lists, empty tuples, empty dicts, empty sets.
Q: What is a ternary operator in Python?
A: A one-line if-else expression: value_if_true if condition else value_if_false. Example: "Adult" if age >= 18 else "Minor".
Q: Can you write an if without indentation?
A: No. Python uses indentation to define code blocks. Missing or inconsistent indentation raises IndentationError.
Want to Master Python?
Join our Python course in Varanasi — practical classes and career support at our Sarnath centre.