🎉 Welcome to PyVerse! Start Learning Today

Conditional Statements

Friendly introduction

Have you ever made a choice like "If it's raining, I'll take an umbrella, else I won't"? Computers do the same thing using conditional statements. In Python, we use if, elif, and else to help the computer decide what to do based on conditions.

1) What is a condition?

  • A condition is a question that's either True or False.
  • Example conditions:
    • 10 > 5 is True
    • name == "Alex" is True only if name is exactly "Alex"
    • age >= 13 is True if age is 13 or more

2) Step 1: The if statement

Use if to run code only when a condition is True.

Example:

x = 10 if x > 5: print("x is bigger than 5")

What happens: Since x > 5 is True, it prints the message.

3) Step 2: Adding else

Use else for what happens when the if condition is False.

Example:

password = "cat" guess = "dog" if guess == password: print("Welcome!") else: print("Try again!")

What happens: guess == password is False, so it prints "Try again!"

4) Step 3: More choices with elif

elif means "else if." Use it when you have more than two choices. Python checks from top to bottom and runs the first True block.

Example: score to grade

score = int(input("Enter your score (0-100): ")) if score >= 90: print("Grade: A") elif score >= 75: print("Grade: B") elif score >= 60: print("Grade: C") else: print("Grade: D (Keep practicing!)")

Tip: The order matters! Put the highest conditions first (like 90 before 75).

5) Step 4: Comparison and logical operators

Comparison operators:

  • == equal to
  • != not equal to
  • >, <, >=, <= greater/less than (or equal)

Logical operators:

  • and both conditions must be True
  • or at least one condition is True
  • not flips True to False (and vice versa)

Examples:

age = 14 has_ticket = True if age >= 13 and has_ticket: print("You can watch the movie.") color = "red" if color == "red" or color == "blue": print("Primary color!") is_raining = False if not is_raining: print("Enjoy the sunshine!")

6) Step 5: Indentation matters

  • The code "inside" if/elif/else must be indented (spaces at the start of the line).
  • Python typically uses 4 spaces. Be consistent.

Example:

number = 3 if number % 2 == 1: print("Odd number") print("Done")

In this example, "Odd number" prints only if the condition is True. "Done" prints every time because it's not indented under the if.

7) Common beginner tips

  • Use = to assign a value, and == to compare values.
  • input() gives you text (a string). Use int(input(...)) if you need a number.

8) Mini exercise: Weather helper

Goal: Ask the user for the temperature and print a simple message.

Instructions:

  1. Ask the user for the temperature in Celsius.
  2. Convert it to an integer.
  3. Use if/elif/else to print:
    • 30 or more: "It's hot!"
    • 20 to 29: "It's warm."
    • 10 to 19: "It's cool."
    • Below 10: "It's cold."

Starter code:

temp = int(input("Temperature in °C: ")) if temp >= 30: print("It's hot!") elif temp >= 20: print("It's warm.") elif temp >= 10: print("It's cool.") else: print("It's cold.")

Try changing the numbers to test different paths.

Bonus (optional):

  • Ask the user if it's raining ("yes" or "no"). If it's raining and temp < 20, print "Bring a jacket!"

9) Short summary

  • if checks a condition and runs code if it's True.
  • elif adds more conditions, checked in order.
  • else runs when none of the above conditions are True.
  • Use comparison (==, >, <=, etc.) and logical operators (and, or, not) to build smarter decisions.
  • Indentation shows which lines belong to the if/elif/else blocks.

You're now ready to make your programs think and choose like you do!

Loading quizzes...