← Back to list

Python Day 2: Mastering Control Flow — Ifs, Loops, and Decisions Like a Pro Chef

Introduction

Rajkumar Kumawat · 2025-08-24 15:41 · 51 claps · 2.8 min read
#python #statement #control-flow #if-else #python-loop
Open on Medium ↗
Wiki topics: 🍳 · Food & Cooking

Python Day 2: Mastering Control Flow — Ifs, Loops, and Decisions Like a Pro Chef

Python Day 2: Mastering Control Flow — Ifs, Loops, and Decisions Like a Pro Chef

Python Day 2: Mastering Control Flow — Ifs, Loops, and Decisions Like a Pro Chef

Introduction

On Day 1, you learned how to set up Python and write basic code. Now, we’ll teach Python how to make choices and repeat tasks — just like a chef decides:

  • If the soup is salty → add water.
  • Else → serve it. And they can repeat tasks — like chopping 10 onions without stopping.

This is Control Flow — telling Python what to do, when to do it, and how many times.

1. Understanding Conditions (if / elif / else) 🥗

Syntax Structure

if condition:
    # do something
elif another_condition:
    # do something else
else:
    # fallback action

Breakdown:

  • **if** → Checks if a condition is true.
  • **elif** → (“else if”) Checks another condition if the first one was false.
  • **else** → Runs if none of the conditions were true.
  • **:** → Means “now do the following indented code block”.
  • Indentation (spaces) → Shows Python which lines belong together.

Example:

temperature = 180

if temperature > 200:  # If oven is hotter than 200°C
    print("Oven is too hot! Turn it down.")
elif temperature == 180:  # Else if oven is exactly 180°C
    print("Perfect baking temperature!")
else:  # Anything else
    print("Oven is too cool, turn it up.")

Output:

Perfect baking temperature!

Analogy: Like a kitchen rulebook — check one rule at a time, do the first one that applies.

2. The for Loop — Repeating a Known Number of Times 🔁

Syntax Structure

for variable in range(start, stop, step):
    # repeat this code

Breakdown:

  • **for** → Tells Python to loop.
  • **variable** → The name for the current item in the loop.
  • **range()** → Creates a sequence of numbers to loop over.
  • **start** → Where to begin (default is 0).
  • **stop** → Where to end (not included).
  • **step** → How much to increase by (default is 1).

Example:

for slice in range(4):
    print("Nom! Slices left:", 4 - slice)

Output:

Nom! Slices left: 4
Nom! Slices left: 3
Nom! Slices left: 2
Nom! Slices left: 1

Analogy: “Stir the soup 4 times” — you know exactly how many times to repeat.

3. The while Loop — Repeat Until a Condition Changes 🔄

Syntax Structure

while condition:
    # repeat this code until condition is false

Breakdown:

  • **while** → Keeps running as long as the condition is true.
  • Condition → A statement that’s true/false.
  • Stops → When the condition becomes false.

Example:

slices = 3
while slices > 0:
    print("Eating slice:", slices)
    slices -= 1  # Decrease by 1

Output:

Eating slice: 3
Eating slice: 2
Eating slice: 1

Analogy: “Keep washing dishes until they are all clean.”

4. Breaking or Skipping Loops (break & continue) 🛑

**break → Stop the loop completely. `continue`** → Skip the rest of the code for this loop and move to the next one.

Example:

for item in ["bread", "butter", "moldy_cheese", "tomato"]:
    if item == "moldy_cheese":
        print("Throwing away bad cheese!")
        continue  # Skip the rest, go to next item
    if item == "tomato":
        print("All done, stop cooking!")
        break  # Exit loop
    print("Using", item)

Output:

Using bread
Using butter
Throwing away bad cheese!
All done, stop cooking!

Analogy:

  • continue = “Skip this ingredient, move on.”
  • break = “Close the kitchen early.”

5. Hands-On Practice 🏋️‍♂️

Exercise 1: Positive/Negative Checker

num = int(input("Enter a number: "))
if num > 0:
    print("Positive")
elif num < 0:
    print("Negative")
else:
    print("Zero")

Exercise 2: Countdown

for i in range(5, 0, -1):
    print(i)
print("Blast off!")

Exercise 3: Sandwich Maker

for sandwich in range(1, 11):
    if sandwich == 5:
        print("Skipping sandwich 5")
        continue
    if sandwich == 8:
        print("Out of bread! Stopping early.")
        break
    print(f"Making sandwich {sandwich}")

Wrap-Up 🎁

You learned:

  • How to make Python decide what to do (if / elif / else).
  • How to make Python repeat tasks (for, while).
  • How to stop or skip parts of loops (break, continue).

Homework: Write a program that:

  • Ask for your favorite 3 fruits.
  • Prints them one by one using a loop.
  • Stops if the fruit is "banana".

Next Stop (Day 3): Functions — reusable recipes for your code so you can stop rewriting the same instructions every time.


메타데이터
post_id
ca890cf23599
slug
python-day-2-mastering-control-flow-ifs-loops-and-decisions-like-a-pro-chef-ca890cf23599
url
https://medium.com/@rajkumarkumawat/python-day-2-mastering-control-flow-ifs-loops-and-decisions-like-a-pro-chef-ca890cf23599
canonical_url
https://medium.com/@rajkumarkumawat/python-day-2-mastering-control-flow-ifs-loops-and-decisions-like-a-pro-chef-ca890cf23599
author_url
https://medium.com/@rajkumarkumawat
status
ok
fetched_at
2026-07-12 00:22:36