Python Course
Conditional Statements in Python
Programs become useful when they can make decisions. A decision in Python is based on a condition that becomes True or False.
Conditional statements power real-world logic like:
- Login success or failure
- Discount applied or not
- Access allowed or denied
- Pass or fail results
How Conditions Work
A condition is usually built using comparison operators like >, <, ==, !=.
The result is always a boolean value: True or False.
# Conditions return True or False
print(10 > 5) # True
print(10 == 5) # False
print(7 != 7) # False
True False False
- Python checks each comparison and produces a boolean result.
- Those True/False results are used inside conditional statements.
The if Statement
The if statement runs its block only when the condition is True.
If the condition is False, Python skips the block.
# if example: runs only when the condition is True
age = 20
if age >= 18:
print("Access granted")
Access granted
- The condition is
age >= 18. - Because it is True, the message prints.
Indentation is the Block
Python uses indentation to define blocks.
Everything indented under if belongs to that block.
# Indentation defines which lines are part of the if block
marks = 75
if marks >= 50:
print("Pass") # inside if block
print("Good job") # inside if block
print("End of program") # outside if block
Pass Good job End of program
- Both indented lines run only when the condition is True.
- The last line runs always because it is not indented.
The if...else Statement
Use else when you want a second path for the False case.
This guarantees one of the blocks will run.
# if-else example: one of the two blocks will run
temperature = 25
if temperature > 30:
print("Weather is Hot")
else:
print("Weather is Pleasant")
Weather is Pleasant
- If the condition is True →
ifblock runs. - If the condition is False →
elseblock runs.
The if...elif...else Statement
Use elif when you have multiple possible conditions.
Python checks conditions from top to bottom and stops at the first True match.
# if-elif-else example: multiple conditions
score = 82
if score >= 90:
print("Grade: A")
elif score >= 75:
print("Grade: B")
elif score >= 50:
print("Grade: C")
else:
print("Grade: Fail")
Grade: B
- Conditions are checked in order.
- Only one block runs in an if-elif-else chain.
- Always keep the strongest condition first (example: 90 before 75).
Logical Operators in Conditions
Real rules often require multiple conditions.
Use and, or, and not to combine conditions.
# Logical operators in conditions
age = 22
has_id = True
# and: both must be true
if age >= 18 and has_id:
print("Entry allowed")
# or: any one true is enough
day = "Sunday"
if day == "Saturday" or day == "Sunday":
print("Weekend")
# not: reverses True/False
is_blocked = False
if not is_blocked:
print("User is active")
Entry allowed Weekend User is active
andis strict: all conditions must be True.oris flexible: one True condition is enough.notflips the boolean result.
Nested Conditional Statements
A nested condition is an if inside another if.
This is useful when one decision depends on another.
# Nested conditions example
age = 20
has_license = False
if age >= 18:
if has_license:
print("You can drive")
else:
print("You are eligible, but license is required")
else:
print("You are underage")
You are eligible, but license is required
- Outer condition checks age first.
- Inner condition checks license only if age condition is True.
Chained Comparisons
Python supports a clean style called chained comparisons. It makes range checks very readable.
# Chained comparison example
x = 7
if 1 <= x <= 10:
print("x is in the range 1 to 10")
else:
print("x is outside the range")
x is in the range 1 to 10
- This is equivalent to:
x >= 1 and x <= 10. - Chaining improves readability.
Truthy and Falsy Values (Important for Real Python)
In Python, some values behave like False even if they are not literally False.
These are called falsy values.
Common falsy values:
0""(empty string)[](empty list){}(empty dictionary)None
# Truthy/falsy examples
name = "" # empty string (falsy)
items = [] # empty list (falsy)
count = 0 # zero (falsy)
if not name:
print("Name is missing")
if not items:
print("No items found")
if not count:
print("Count is zero")
Name is missing No items found Count is zero
- This pattern is common in real code validation.
- It avoids long comparisons like
if name == "".
Real-World Example: Discount Rule
This example checks total purchase amount and applies a discount only when eligible. This is how e-commerce systems commonly work.
# Discount example using conditions
subtotal = 1499.0
discount_rate = 0.10
# Rule: discount applies only if subtotal is 1000 or more
if subtotal >= 1000:
discount = subtotal * discount_rate
final_total = subtotal - discount
print("Discount Applied:", discount)
print("Final Total:", final_total)
else:
print("No discount applied")
print("Final Total:", subtotal)
Discount Applied: 149.9 Final Total: 1349.1
- The condition checks eligibility using
>=. - If eligible, we calculate discount and subtract it.
- Otherwise, subtotal remains the final total.
Common Beginner Mistakes
- Forgetting colon after if/elif/else
- Incorrect indentation (Python is strict)
- Using = instead of == for comparisons
- Wrong ordering in elif ladder (example: checking 50 before 90)
Practice
Which keyword is used for checking multiple conditions after if?
In Python, what defines a code block under an if statement?
Which block runs when the if condition becomes False?
Which logical operator requires all conditions to be True?
Quick Quiz
In an if-elif-else chain, how many blocks will execute?
What do we call values like 0, "", and [] when used in conditions?
Which symbol must appear after every if, elif, and else line?
Recap:
You learned all major conditional statement types in Python:
if, if-else, if-elif-else, nested conditions,
logical combinations, chained comparisons, and truthy/falsy behavior.
Next up: Loops in Python — repeating tasks without rewriting code.