Home Tutorials Python Programs Conditional Statements
Conditional Statements

Conditional Statements


Topic: Conditional Statements (if-else & elif)

Description: This section covers decision-making logic, evaluating conditions, and controlling the flow of a program based on dynamic inputs.

Why: Conditional statements form the backbone of logic building in programming. They allow your software to make decisions, execute different paths based on variable states, and handle user interactions intelligently.


Program 1.1: Odd or Even Number Checker

Problem Statement: Write a function that takes an integer and determines whether it is odd or even.

Expected Input & Output:

  • Input: 7 → Output: "7 is Odd"
  • Input: 12 → Output: "12 is Even"

Core Syntax Hint: Use the modulo operator (%) to verify the remainder when a number is divided by 2.

def check_odd_even(number):
    if number % 2 == 0:
        return f"{number} is Even"
    else:
        return f"{number} is Odd"

# Test Case Execution / Output Example
print(check_odd_even(7))   # Output: 7 is Odd
print(check_odd_even(12))  # Output: 12 is Even

Program 1.2: Student Grading System

Problem Statement: Create a system that assigns a letter grade based on a numerical score from 0 to 100.

Expected Input & Output:

  • Input: 85 → Output: "Grade: B"
  • Input: 42 → Output: "Grade: F"

Core Syntax Hint: Chain multiple evaluations together in sequential order using if, elif, and a final catch-all else.

def calculate_grade(score):
    if score >= 90:
        return "Grade: A"
    elif score >= 80:
        return "Grade: B"
    elif score >= 70:
        return "Grade: C"
    elif score >= 60:
        return "Grade: D"
    else:
        return "Grade: F"

# Test Case Execution / Output Example
print(calculate_grade(85))  # Output: Grade: B
print(calculate_grade(42))  # Output: Grade: F

Program 1.3: Leap Year Detector

Problem Statement: Write a utility to evaluate if a given calendar year qualifies as a leap year.

Expected Input & Output:

  • Input: 2024 → Output: "2024 is a Leap Year"
  • Input: 2025 → Output: "2025 is a Regular Year"

Core Syntax Hint: Combine multiple logic evaluations with and / or constraints to verify divisibility rules.

def is_leap_year(year):
    if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
        return f"{year} is a Leap Year"
    else:
        return f"{year} is a Regular Year"

# Test Case Execution / Output Example
print(is_leap_year(2024))  # Output: 2024 is a Leap Year
print(is_leap_year(2025))  # Output: 2025 is a Regular Year

Program 1.4: Find the Largest of Three Numbers

Problem Statement: Accept three individual numbers and output the absolute highest value among them without helper functions.

Expected Input & Output:

  • Input: 10, 45, 23 → Output: 45

Core Syntax Hint: Directly compare numerical values against each other using comparison operators (>=).

def find_largest(a, b, c):
    if a >= b and a >= c:
        return a
    elif b >= a and b >= c:
        return b
    else:
        return c

# Test Case Execution / Output Example
print(find_largest(10, 45, 23))  # Output: 45

Program 1.5: E-Commerce Discount Calculator

Problem Statement: Process an order total value and calculate the remaining balance after deducting scale-based promotional discounts.

Expected Input & Output:

  • Input: 600 → Output: 480.0
  • Input: 150 → Output: 150.0

Core Syntax Hint: Track modifier float numbers cleanly within local block parameters before executing structural mathematical deductions.

def get_final_bill(bill_amount):
    if bill_amount > 500:
        discount = 0.20
    elif bill_amount > 200:
        discount = 0.10
    else:
        discount = 0.0
    
    final_price = bill_amount - (bill_amount * discount)
    return final_price

# Test Case Execution / Output Example
print(get_final_bill(600))  # Output: 480.0
print(get_final_bill(150))  # Output: 150.0

🏋️ Test Yourself With Exercises

Take our quiz on Conditional Statements to test your knowledge.

Browse Quizzes »