DEV Community

PremPrema
PremPrema

Posted on Fully Autonomous

Day 10: Python While Loop: Don't Think About the Whole Output, Think About the Next Step

Hi everyone 👋

I am learning Python in an online class with Muthu Sir. Today was Day 10, and the topic was Looping. Sir asked us to write a blog every day, so here is what I learned today, in the same order Sir taught it.


1. The Problem: 300 Times?

Sir started with this:

print(1)
print(1)
print(1)
Enter fullscreen mode Exit fullscreen mode

Easy. Three lines, three outputs.

Then Sir asked: "What if I want to print 1 three hundred times?" 🤔

Should we type print(1) 300 times? No!

So Sir explained what looping really means:

Looping is not about what we are doing.
Looping is about how many times we are doing a particular activity.


2. Before the Code: The Mindset

Before writing any code, Sir talked about the mindset. I liked this part a lot.

  • Looping is a choice. If you feel comfortable with it, it will open many doors:
    1. DSA → Looping
    2. Product-based companies → Looping
    3. Genius / Brilliant people → Looping
  • Looping is like walking, cycling, drawing, swimming. Nobody is born with it. We learn it by practice.
  • Don't compare yourself with others.
  • Keep a free mind, and keep a pen and paper ready.

3. Ground Rules ✍️

These are the rules Sir gave us for solving any looping problem:

  1. Never say "I don't know."
  2. Say "Let me try."
  3. Don't think about the entire output.
  4. Think only about the very next step.
  5. If you feel it is necessary, introduce a variable.

Rule 3 and 4 are the most important for me. When I look at the full output, I get confused. When I think only about the next step, it becomes easy.

Sir also gave us a small puzzle: 1 3 7 12 19. What comes next? (Try it with pen and paper! 😉)


4. Step by Step: From Repeated Lines to a Loop

Step 1: The simple way

print("1 1 1 1 1")
Enter fullscreen mode Exit fullscreen mode

It works, but it is not a loop. It is just one big string.

Step 2: Introduce a variable (Rule 5!)

Sir added a count variable to remember how many times we printed:

count = 0
print(1)
count = 1
print(1)
count = 2
print(1)
count = 3
print(1)
count = 4
print(1)
count = 5
Enter fullscreen mode Exit fullscreen mode

Step 3: Make every line look the same

Before going further, Sir showed this small example:

amount = 10
amount = amount + 20
print(amount)   # 30
Enter fullscreen mode Exit fullscreen mode

👉 Python first calculates the right side (amount + 20), then stores the result in the left side (amount).

So instead of count = 1, count = 2, count = 3... we can write the same line every time:

count = 0
print(1)
count = count + 1   # count = 1
print(1)
count = count + 1   # count = 2
print(1)
count = count + 1   # count = 3
Enter fullscreen mode Exit fullscreen mode

Now every pair of lines is exactly the same. And when lines are the same, we can loop them! 🎉

Step 4: Say it in your own language first

Sir wrote the condition in Tamil first:

"எப்போதெல்லாம் count இன் மதிப்பு 5ஐ விடக் குறைவாக இருக்கிறதோ..."
(Whenever the value of count is less than 5...)

And that sentence becomes Python:

count = 0
while count < 5:
    print(1, end=' ')
    count = count + 1
Enter fullscreen mode Exit fullscreen mode

Output:

1 1 1 1 1
Enter fullscreen mode Exit fullscreen mode

💡 end=' ' prints a space instead of a new line, so all the 1s come in one line.

Tracking with pen and paper

Sir tracked the value of count in comments, step by step:

Step count count < 5? Prints
1 0 0 < 5 ✅ 1
2 1 1 < 5 ✅ 1
3 2 2 < 5 ✅ 1
4 3 3 < 5 ✅ 1
5 4 4 < 5 ✅ 1
6 5 5 < 5 ❌ stop

5. Let's Experiment: Change One Thing at a Time 🧪

This was the best part. Sir changed one small thing each time and asked us to guess the output before running it.

Try guessing before you read the answer!

Change 1: Start count from 1

count = 1
while count < 5:
    print(1, end=' ')
    count = count + 1
Enter fullscreen mode Exit fullscreen mode

Output

1 1 1 1 → only 4 times (1, 2, 3, 4 are less than 5)

Change 2: Use <= instead of <

count = 0
while count <= 5:
    print(1, end=' ')
    count = count + 1
Enter fullscreen mode Exit fullscreen mode

Output

1 1 1 1 1 1 → 6 times (0 to 5)

Change 3: Use > instead of <

count = 0
while count > 5:
    print(1, end=' ')
    count = count + 1
Enter fullscreen mode Exit fullscreen mode

Output

Nothing! Is 0 greater than 5? No. So the loop never starts.

Change 4: count + 2 instead of count + 1

count = 0
while count < 5:
    print(1, end=' ')
    count = count + 2
Enter fullscreen mode Exit fullscreen mode

Output

1 1 1 → count goes 0 → 2 → 4 → 6. Only 3 times.

Change 5: count - 1 instead of count + 1

count = 0
while count < 5:
    print(1, end=' ')
    count = count - 1
Enter fullscreen mode Exit fullscreen mode

Output

Infinite loop! 😱 count goes 0 → -1 → -2 → -3... and -1 is less than 5, -2 is less than 5... so the condition is always true. It never stops. (Press Ctrl + C to stop it.)


6. What I Learned Today

  1. A loop answers "how many times?"
  2. First make every line look the same (count = count + 1), then put it in a while loop.
  3. Say the condition in your own language first, then write it in Python.
  4. Don't think about the whole output. Think about the very next step, with pen and paper.
  5. Change one small thing and guess the output. This is the best way to really understand a loop.

Thank you, Muthu Sir, for teaching in such a simple way! 🙏

See you on Day 11! 🐍

Top comments (0)