Python Course in 4 to 6 Weeks – Lesson 6: Conditional Statements (if, elif, else)

🧭 Python Course in 4 to 6 Weeks – Lesson 6: Conditional Statements (if, elif, else)

Welcome to Lesson 6 of our Python Course in 4 to 6 Weeks! Today we explore one of the most important concepts in programming: conditional statements. These allow your program to make decisions and react to different situations.

✅ What You'll Learn

  • What are conditional statements?
  • How to use if, elif, and else
  • Indentation rules and logic flow
  • Common errors and debugging tips
  • Real-life decision-making with code

🧠 What Are Conditional Statements?

Conditional statements let your program choose between actions based on conditions (True or False).

age = 18

if age >= 18:
    print("You are an adult.")
else:
    print("You are a minor.")

🔁 Using if, elif, and else

score = 75

if score >= 90:
    print("Grade: A")
elif score >= 80:
    print("Grade: B")
elif score >= 70:
    print("Grade: C")
else:
    print("Grade: D or below")

📏 Indentation Rules

  • Python uses indentation (spaces) to define blocks of code.
  • Typically, you use 4 spaces after if, elif, or else.

⚠️ Common Mistakes

  • ❌ Missing indentation after if
  • ❌ Using = instead of == in conditions
  • ❌ Forgetting the colon : at the end of if, elif, or else

🎯 Real-Life Example

temperature = int(input("Enter temperature: "))

if temperature > 30:
    print("It's hot outside.")
elif temperature > 20:
    print("Nice weather.")
elif temperature > 10:
    print("Cool day.")
else:
    print("It's cold.")

🧪 Practice Challenge

Create a Python file named login_checker.py that:

  • Asks the user for a username and password
  • Checks if they match stored credentials
  • Prints success or error message

📥 Tools You'll Need

📌 Final Words

Conditional statements are the building blocks of decision-making in Python. Mastering them opens the door to writing smart, responsive programs. Next, we’ll explore loops – a way to repeat actions automatically in your code.

🔗 Coming Next:

Lesson 7: Loops in Python (for, while) – Automate Repetitive Tasks 🔁

Post a Comment