1.3 Conditional Statements (if elif else)
Module 3 — Conditional Statements 🔀 | Python for ML 🐍 Module 3 • Python for Machine Learning Teach your programs to make decisions — just like you choose what to wear based on the weather! ☀️🌧️ 🤔 What are Conditional Statements? Every day you make decisions based on conditions: ☔ If it's raining → take an umbrella 🛒 If the price < ₹500 → buy it 🔋 If battery < 20% → charge your phone Your brain checks a condition and picks an action. Python does the same thing! 🧠 A conditional statement tells Python to run a block of code only when a certain condition is True. If the condition is False, Python either skips it or runs a different block. 🎯 📝 Key Term: Boolean Condition A condition that evaluates to either True or False. Example: age >= 18 → True or False. ✅ True or False — Activity 1 / 15 Conditional statements allow Python to make decisions and run different code based on conditions. Before we dive in, let's learn the words you'll see everywhere in this module. 🗂️ Checks a condition — runs the block only if True Runs when the if condition is False "Else if" — checks another condition when previous was False 4 spaces at the start of a line — tells Python what's inside the block An expression that is either True or False Non-boolean values treated as True or False Which keyword lets you check a second condition only if the first was False? 🎯 Analogy: The Bouncer at a Club Imagine a bouncer at a club entrance. He checks one thing: "Are you 21 or older?" 🕴️ ✅ If yes → you enter the club ❌ If no → nothing happens, you just walk away The if statement works exactly the same way. It checks a condition. If it's True, the indented code runs. If not, Python skips it. 🏃 if condition: # ← colon is REQUIRED print("Runs if True") # ← 4 spaces indent ⚠️ Don't forget the colon! Every if, elif, and else line must end with a colon :. Forgetting it is the #1 beginner mistake! print("Program continues...") Python uses 4 spaces (not curly braces like Java/C++) to group code. Everything indented under if is part of that block. The un-indented line print("Program continues...") runs regardless of the condition. 🔧 🧠 Quick Check — Activity 3 / 15 What happens if the condition in an if statement is False? ✓ Python skips the indented block and continues ✗ Python runs the indented block anyway ✗ Python stops the entire program print("Stay hydrated! 💧") # ← SKIPPED (25 is NOT > 35) print("Have a nice day! 😊") # ← Always runs ✍️ Fill in the Blank — Activity 4 / 15 Complete the code to print "Discount!" when price is less than 100: You walk into an ice cream shop and ask for chocolate 🍫. The shop says: ✅ If we have chocolate → "Here you go!" ❌ Else → "Sorry, have vanilla instead!" The else block runs when the if condition is False. It's the backup plan. 🛡️ 📌 else does NOT have a condition 📌 else must come right after an if block 📌 else also needs a colon : 📌 Only ONE else per if block else = "Do this when everything above was False" 💳 Real-world: ATM Withdrawal balance = balance - withdraw print(f"Withdrawal successful! 💵 Balance: ₹{balance}") print("Insufficient funds! 🚫") Withdrawal successful! 💵 Balance: ₹2000 🧠 Quick Check — Activity 5 / 15 🎬 Animated Flowchart — Grade Calculator Enter marks and watch the decision path light up! The ball travels through the flowchart. 🎱 👆 Enter marks (0-100) and press Play to watch the decision process! 🍽️ Analogy: Restaurant Menu You walk into a restaurant and ask the waiter: 🤵 🥩 "Do you have steak?" → No 🍝 "What about pasta?" → No 🍕 "Pizza?" → Yes! → "I'll have pizza!" You check options one by one, in order. The moment you find a match, you stop checking. That's exactly how elif works! 🎯 🎓 Grade Calculator — Step by Step If True → Grade A 🌟. Stop here! If True → Grade B 👍. Stop here! If True → Grade C ✓. Stop here! If True → Grade D ⚡. Stop here! Grade F ❌. This is the default. print(f"Your grade: {grade}") 🔥 Why not just use multiple if statements? With elif, Python stops checking after finding the first True condition. With separate if blocks, Python checks ALL of them — slower and can cause bugs! Example: marks=95 would match BOTH marks>=90 AND marks>=80. 📊 Calculate — Activity 6 / 15 Using the grade calculator above, what grade does marks = 72 get? (Enter just the letter: A, B, C, D, or F) 🌡️ Real-world: Temperature Advisor print("🔥 Extreme heat! Stay indoors.") print("☀️ Very hot! Stay hydrated.")
Subject: Python for Machine Learning | Chapter: Chapter 1: Python Essentials