Skip to main content

Command Palette

Search for a command to run...

Python Basics — Day 5 Conditional Statements (if / elif / else)

Updated
2 min read
S

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!")

Python Basics

Part 5 of 21

A collection of study notes from my university and self-learning journey. This series covers Python fundamentals step by step — from setup to core concepts — to help both myself and others build a solid foundation in coding.

Up next

Python Basics — Day 6 Loops (for / range)

Day 6 – Loops (for / range) 01. What is a Loop? Loops are used when you want to repeat the same action multiple times.In Python, the two main types of loops are for and while.(Today we focus on for loops.) 02. Basic for Loop A for loop iterates over...

More from this blog

S

Sabin’s DevLog

21 posts

I plan to upload everything I study and prepare here. I hope it can be helpful to others, and I also want my future self to see the learning journey I’ve been through.