Conditional Statements

Conditional Statements in Python: In the real world, we make decisions every second: "If it's raining, I'll take an umbrella.

Concept Explanation

In the real world, we make decisions every second: "If it's raining, I'll take an umbrella. Else, I'll wear sunglasses."

In programming, we use Conditionals to give our code the ability to make decisions. Without them, programs would simply execute the same lines of code every single time. Conditionals allow your application to react dynamically to different user inputs, sensor data, or game states.


Syntax or Core Structure

The if, elif, and else logic

  • if: The first check. If True, run this code.
  • elif (else if): If the first check was False, try this check instead.
  • else: If ALL previous checks were False, run this code as a last resort.

CRITICAL REMINDER: Indentation is mandatory! Everything inside the if block must be indented by 4 spaces.

age = 18

if age >= 18:
    print("Welcome to the adult section.")
elif age >= 13:
    print("Welcome, teen!")
else:
    print("Access Denied.")

Code Example #1 (Basic)

A simple "Check-in" system using Boolean logic.

is_logged_in = True

if is_logged_in:
    print("Loading your personalized dashboard...")
else:
    print("Please log in to continue.")

Code Example #2 (Intermediate)

You can use comparison operators (==, !=, >, <, >=, <=) and logical operators (and, or, not) to create complex conditions.

temperature = 25
is_sunny = True

if temperature > 30 and is_sunny:
    print("It's a hot sunny day! Wear sunscreen.")
elif temperature > 20 or is_sunny:
    print("Nice weather for a walk.")
else:
    print("Maybe stay inside today.")

Real World Example

Imagine a simple discount system for a movie theater.

# 🎫 Movie Ticket Logic
customer_age = int(input("Enter your age: "))
is_student = input("Are you a student? (yes/no): ").lower() == "yes"

ticket_price = 15.00

if customer_age < 12:
    ticket_price = 8.00
    print("Child discount applied!")
elif customer_age >= 65:
    ticket_price = 10.00
    print("Senior discount applied!")
elif is_student:
    ticket_price = 12.00
    print("Student discount applied!")

print(f"Your final ticket price is: $${ticket_price}")

Common Mistakes

  1. Using Single Equals (=): This is the most common error. if x = 5: will crash because = is for assignment. You must use double equals == for comparison.
  2. Forgetting the Colon (:): Every if, elif, and else line must end with a colon.
  3. Indentation Errors: If you don't indent the code below the if statement, Python won't know which lines belong to that condition.

Best Practices

  • Keep Conditions Simple: If you find yourself writing an if statement with 5 different and/or checks, consider breaking it into nested if-statements or helper variables.
  • Order Matters: Python checks conditions from top to bottom. Once it finds a True condition, it executes that block and skips everything else in that specific if/elif chain. Put your most specific conditions at the top.

Practice Exercises

  1. Pass/Fail: Ask the user for their exam score (0-100). Print "Pass" if it's 50 or higher, and "Fail" otherwise.
  2. Even or Odd: Ask for a number. Use the modulo operator (%) to check if it's even or odd, and print the result.
  3. The Multi-Friend Checker: Set three variables friend_a_score, friend_b_score, and friend_c_score. Write an if-statement that prints "Everyone passed!" only if all three scores are above 60.
  4. Nested Weather: Write a script that checks if it's "Raining". If it is, check if the "Wind Speed" is over 50. Print "Stormy" for high wind and "Light Rain" for low wind.