Conditional Statements in Python
Conditional statements allow your program to make decisions. They help the program choose different actions based on certain conditions. In real-world applications—billing systems, login validation, recommendation engines, and more—conditions play a major role.
In Python, the most commonly used conditional keywords are if, elif, and else. Together, these let you create logic that responds to different situations.
Why Do We Need Conditional Statements?
Imagine a program checking if you are old enough to vote, or a banking system deciding whether your withdrawal is allowed. These decisions happen using conditional statements.
age = 20
if age >= 18:
print("You are eligible to vote!")
else:
print("You are not eligible to vote.")
Basic if Statement
The if statement checks whether a condition is True. If yes, the code inside it runs.
num = 10
if num > 5:
print("Number is greater than 5")
if–else Statement
When you want two possible outcomes—one for True, one for False—you use if–else.
marks = 45
if marks >= 50:
print("Pass")
else:
print("Fail")
if–elif–else Ladder
Use this when you have more than two possible cases. Each elif condition is checked one by one.
score = 82
if score >= 90:
print("Grade A")
elif score >= 75:
print("Grade B")
elif score >= 60:
print("Grade C")
else:
print("Grade D")
Indentation in Python
Python uses indentation (spaces before a line) to define which statements belong to a block. Unlike other languages, Python does not use curly braces. Incorrect indentation will cause errors.
# Correct
if True:
print("Hello")
# Incorrect
if True:
print("Hello")
Nested if Statements
A nested if is an if inside another if. Useful when a decision depends on multiple conditions.
age = 25
citizen = True
if age >= 18:
if citizen:
print("You can vote")
else:
print("You must be a citizen to vote")
else:
print("You must be 18 or older")
Using Logical Operators with Conditions
Combine multiple conditions using and, or, and not.
age = 22
has_id = True
if age >= 18 and has_id:
print("Entry allowed")
else:
print("Entry denied")
Real-World Example: Login Validation
A simple login system checks the username and password before granting access.
username = "admin"
password = "1234"
user = input("Enter username: ")
pwd = input("Enter password: ")
if user == username and pwd == password:
print("Login successful")
else:
print("Invalid username or password")
Real-World Example: Shopping Discount Logic
Stores often give discounts based on purchase amount. This example shows how conditional statements help manage such situations.
amount = float(input("Enter total amount: "))
if amount >= 5000:
discount = 20
elif amount >= 3000:
discount = 10
else:
discount = 5
final_amount = amount - (amount * discount / 100)
print("Discount:", discount, "%")
print("Final Amount:", final_amount)
📝 Practice Exercises
- Write a program that checks whether a number entered by the user is positive, negative, or zero.
- Ask the user for their age and print whether they are a child, teenager, adult, or senior.
- Create a program that checks if a password is strong (length ≥ 8).
- Write a program that checks if a student passed the exam (marks ≥ 40) and also whether they scored distinction (≥ 75).
✅ Practice Answers
Answer 1:
num = float(input("Enter a number: "))
if num > 0:
print("Positive")
elif num < 0:
print("Negative")
else:
print("Zero")
Answer 2:
age = int(input("Enter age: "))
if age < 13:
print("Child")
elif age < 20:
print("Teenager")
elif age < 60:
print("Adult")
else:
print("Senior")
Answer 3:
password = input("Enter password: ")
if len(password) >= 8:
print("Strong password")
else:
print("Weak password")
Answer 4:
marks = int(input("Enter marks: "))
if marks >= 75:
print("Distinction")
elif marks >= 40:
print("Pass")
else:
print("Fail")