πŸ”„ Quick Recap (Dayβ€―5)

  • You mastered writing clean code with proper naming and formatting.

  • You learned to spot and fix simple syntax errors.

🎯 What You’ll Learn Today

  1. How to use if statements to run code only when a condition is true.

  2. How to chain conditions with elif.

  3. How to provide a fallback with else.

  4. Understanding truthy and falsy values in Python.

πŸ“– Overview: Making Decisions with if

In everyday life, you make choices: β€œIf it’s raining, I take an umbrella.” Code can do the same:

if is_raining:
    print("Take an umbrella!")
  • if checks the condition inside parentheses (or after if).

  • If the condition is True, the indented block runs.

Chaining with elif and else

Sometimes there are multiple options:

temperature = 15
if temperature > 25:
    print("It's hot, wear shorts!")
elif temperature >= 15:
    print("Nice weather, wear something comfortable.")
else:
    print("It's cold, bundle up!")
  • elif stands for β€œelse if.” It checks only if all previous if or elif conditions were False.

  • else runs when none of the above conditions are True.

Truthy and Falsy Values

Python considers certain values as False in conditions:

  • False, 0, 0.0, '' (empty string), [] (empty list), {} (empty dict), None
    All others are True. You can write:

items = []
if items:
    print("We have items!")
else:
    print("No items found.")

Since items is empty ([]), it’s falsy, so the else block runs.

πŸ§™β€β™‚οΈ Take the Wand and Try Yourself

  1. Create a file named check_number.py.

  2. Write code that:

    • Prompts the user to enter a number (use input()).

    • Converts the input to an integer.

    • Uses if / elif / else to print:

      • β€œPositive” if the number is greater than 0.

      • β€œZero” if it equals 0.

      • β€œNegative” if it is less than 0.

  3. Run your script and test with different numbers.

Solution Example (check_number.py):

# check_number.py
num = int(input("Enter a number: "))

if num > 0:
    print("Positive")
elif num == 0:
    print("Zero")
else:
    print("Negative")

Expected interaction:

Enter a number: 5
Positive
Enter a number: 0
Zero
Enter a number: -3
Negative

Run:

python check_number.py

Great work! Conditional logic opens up many possibilities.
Next up: Day 7: Looping Constructs, where you’ll learn how to repeat tasks automatically.

Keep Reading