DEV Community

Cover image for Python basics - Day 05
Sabin Sim
Sabin Sim

Posted on

Python basics - Day 05

Day 5 – Conditional Statements (if / elif / else)

Project: Create a “Smart Login & Grade Evaluator”


01. Learning Goal

By the end of this lesson, you will be able to:

  • Understand and use Python conditional statements
  • Combine comparison and logical operators in conditions
  • Build nested conditions for complex logic
  • Create a mini project that checks grades and simulates login

02. Problem Scenario

You are designing a Smart Evaluator App that decides different outcomes:

whether someone is an adult, what their grade is, and whether login credentials are correct.


03. Step 1 – Basic if Statement

A condition executes only when it is True.

age = 20

if age >= 18:
    print("You are an adult.")
Enter fullscreen mode Exit fullscreen mode

⚠️ Indentation matters!
Python relies on consistent indentation (recommended: 4 spaces).


04. Step 2 – if ~ else

If the condition is true → run the if block.
If false → run the else block.

age = 15

if age >= 18:
    print("Adult")
else:
    print("Minor")
Enter fullscreen mode Exit fullscreen mode

05. Step 3 – if ~ elif ~ else

Used for checking multiple conditions in sequence.
Python executes the first True condition only.

score = 85

if score >= 90:
    print("Grade A")
elif score >= 80:
    print("Grade B")
elif score >= 70:
    print("Grade C")
else:
    print("Grade F")
Enter fullscreen mode Exit fullscreen mode

06. Step 4 – Nested if (Conditions within Conditions)

You can place one if statement inside another for more complex logic.

age = 20
is_student = True

if age >= 18:
    if is_student:
        print("Adult student.")
    else:
        print("Adult but not a student.")
Enter fullscreen mode Exit fullscreen mode

07. Step 5 – Logical Operators in Conditions

You can combine multiple conditions using and, or, not.

temp = 25

if temp > 20 and temp < 30:
    print("The weather is warm.")
Enter fullscreen mode Exit fullscreen mode

08. Step 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.")
Enter fullscreen mode Exit fullscreen mode

Example 2: Age Group Classifier

age = int(input("Enter your age: "))

if age < 13:
    print("Child")
elif age < 20:
    print("Teenager")
else:
    print("Adult")
Enter fullscreen mode Exit fullscreen mode

Example 3: Login Simulation

user_id = input("Enter ID: ")
password = input("Enter password: ")

if user_id == "admin" and password == "1234":
    print("Login successful!")
else:
    print("Login failed!")
Enter fullscreen mode Exit fullscreen mode

09. Step 7 – Mini Project: Smart Login & Grade Evaluator

Combine multiple conditions into one program that evaluates both login and performance.

user_id = input("Enter ID: ")
password = input("Enter password: ")

if user_id == "admin" and password == "1234":
    print("✅ Login successful!")
    score = int(input("Enter your score: "))

    if score >= 90:
        print("Excellent performance! Grade A")
    elif score >= 75:
        print("Good job! Grade B")
    elif score >= 60:
        print("You passed. Grade C")
    else:
        print("Failed. Try again next time.")
else:
    print("❌ Invalid credentials. Access denied.")
Enter fullscreen mode Exit fullscreen mode

10. Reflection

You have learned how to:

  • Use conditional statements to control program flow
  • Combine logical and comparison operators
  • Apply nested conditions for complex decisions
  • Build a working Smart Evaluator program

In the next lesson, you’ll learn how to repeat actions automatically using loops (for, while) — the foundation of all automation and data processing tasks.

Top comments (0)