Python Basics — Day 5 Conditional Statements (if / elif / else)
Aspiring Full-Stack Developer | Python · Django · React · SQL | documenting my learning journey
Building skills in Python and full-stack development, with a focus on web apps and system design
Junior developer in training — Python, Django, React — preparing for a career in full-stack engineering
From Python basics to full-stack projects, sharing my progress as I grow into a developer
Future full-stack engineer | Learning in public: Python, APIs, Databases, and Web Development
1️⃣ Basic if Statement
- Code inside the block runs only if the condition is True.
age = 20
if age >= 18:
print("You are an adult.") # Condition is True → executed
⚠️ Note: Indentation matters! In Python, 4 spaces are recommended.
2️⃣ if ~ else
If the condition is True, the if block runs. Otherwise, the else block runs.
age = 15
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
3️⃣ if ~ elif ~ else
Use elif to check multiple conditions in order.
Python executes only the first condition that is True.
score = 85
if score >= 90:
print("Grade A")
elif score >= 80:
print("Grade B")
elif score >= 70:
print("Grade C")
else:
print("Grade F")
4️⃣ Nested if
You can place another if statement inside an existing one.
age = 20
is_student = True
if age >= 18:
if is_student:
print("You are an adult student.")
else:
print("You are an adult, but not a student.")
5️⃣ Using Comparison & Logical Operators
You can combine conditions with and, or, and not.
temp = 25
if temp > 20 and temp < 30:
print("The weather is warm.")
6️⃣ Practice Examples
📝 Example 1: Even or Odd
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even number")
else:
print("Odd number")
📝 Example 2: Age Group
age = int(input("Enter your age: "))
if age < 13:
print("Child")
elif age < 20:
print("Teenager")
else:
print("Adult")
📝 Example 3: Login Simulation
user_id = input("Username: ")
password = input("Password: ")
if user_id == "admin" and password == "1234":
print("Login successful!")
else:
print("Login failed!")