Cyber Services / All lessons / Python Programming / Conditional Statements

Conditional Statements

Module 6 of 31 · 2 min read · Free to read

MODULES ↓ Command blocks have a COPY button
Program Control Flow

Conditional statements allow a program to make decisions based on whether a condition is True or False. They use **operators** to compare values and combine logic.

Operators Used in Conditions

1. Comparison Operators

Used to compare two values. Result is always a bool (True or False).

OperatorDescriptionExampleResult
==Equal to5 == 5True
!=Not equal to3 != 4True
>Greater than10 > 2True
<Less than1 < 5True
>=Greater than or equal7 >= 7True
<=Less than or equal6 <= 5False

2. Arithmetic Operators (Used in Expressions)

Often used inside conditions to compute values before comparing.

  • + (addition), - (subtraction)
  • * (multiplication), / (division)
  • // (floor division), % (modulo), ** (exponent)
  • total = 10 + 5
    if total > 12:
        print("Enough")

    Types of Conditional Statements

    1. Unidirectional: if

    Runs a block **only if** the condition is True.

    age = int(input("Enter your age: "))
    if age >= 18:
        print("You are an adult.")

    2. Bidirectional: if-else

    Chooses **between two paths**.

    temperature = float(input("Current temperature: "))
    if temperature > 30:
        print("It's hot outside!")
    else:
        print("It's cool outside.")

    3. Multidirectional: if-elif-else

    Handles **multiple conditions** in order.

    score = int(input("Enter your score (0-100): "))
    if score >= 90:
        print("Grade: A")
    elif score >= 80:
        print("Grade: B")
    elif score >= 70:
        print("Grade: C")
    else:
        print("Grade: F")

    Real-World Scenarios

    Scenario 1: Login Age Check

    user_age = int(input("How old are you? "))
    if user_age < 13:
        print("Access denied: Must be 13+.")
    else:
        print("Welcome to the platform!")

    Scenario 2: Discount Eligibility

    total_bill = float(input("Total bill amount: "))
    is_member = input("Are you a member? (yes/no): ") == "yes"
    
    if total_bill > 100 and is_member:
        print("You get a 20% discount!")
    else:
        print("No discount applied.")

    Scenario 3: Grade Classifier (Multi-path)

    marks = int(input("Enter marks: "))
    if marks == 100:
        print("Perfect score!")
    elif marks >= 80:
        print("Excellent")
    elif marks >= 60:
        print("Good")
    elif marks >= 40:
        print("Pass")
    else:
        print("Fail")
    Modules (31) ← Python Programming