mailwal

Python From Scratch #27 - While Loops

Shefali Jangid

Python From Scratch #27 - While Loops

Learn how to use while loops to repeat code as long as a condition is True.

Hey 👋

Welcome back to the Python from Scratch series.

In the last lesson, we learned about for loops and how to use range() to repeat code.

But what if you don’t know exactly how many times you want the loop to run?

That’s where while loops come in.

What Is a while Loop?

A while loop repeats a block of code as long as a condition is True.

Here’s the basic syntax:

while condition:
    # code to repeat

For example:

count = 1

while count <= 5:
    print(count)
    count += 1

# Output:

# 1
# 2
# 3
# 4
# 5

The loop checks the condition before each repetition:

  • Is count <= 5? Yes → run the code.

  • Increase count by 1.

  • Check again.

  • Keep repeating while the condition is True.

  • When count becomes 6, the condition is False, so the loop stops.

Why Do We Need to Update the Variable?

Look at this example:

count = 1

while count <= 5:
    print(count)
    count += 1

The line: count += 1 is important.

It changes the value of count each time the loop runs.

Without it:

count = 1

while count <= 5:
    print(count)

count will always remain 1.

The condition count <= 5 will always be True, so the loop will keep running.

This is called an infinite loop.

Tip: If you accidentally create an infinite loop while testing, press Ctrl + C in the terminal to stop it.

while Loop With User Input

while loops are useful when you want to keep asking the user for something until a condition is met.

For example:

password = ""

while password != "python":
    password = input("Enter the password: ")

print("Access granted!")

This keeps asking for the password for as long as the user enters the wrong one. Once they type python, the condition becomes False and the loop stops.

while vs for

So when should you use a for loop and when should you use a while loop?

for loop

Use a for loop when you usually know what you’re looping over or how many times you want to repeat something.

for number in range(5):
    print(number)

while loop

Use a while loop when you want to keep repeating something until a condition becomes False.

number = 0

while number < 5:
    print(number)
    number += 1

Both examples produce similar results, but they work differently.

A Simple Example

Let’s print numbers from 1 to 10 using a while loop:

number = 1

while number <= 10:
    print(number)
    number += 1

The important parts are:

number = 1: This gives us the starting value.

while number <= 10: This tells Python when to continue the loop.

number += 1: This changes the value so the loop can eventually stop.

Try It Yourself

Now, try these examples yourself.

Use a while loop to print:

  1. numbers from 1 to 5

  2. numbers from 10 to 1

  3. Your name 5 times

That’s all for today!

Tomorrow, we’ll learn about break and continue and see how they can control a loop.


If you enjoyed this newsletter, consider sharing it with another developer. And if you have any feedback, I’d love to hear it. It would genuinely mean a lot to me. ❤️


Want to sponsor my work? Let’s talk!

See you tomorrow ✨

- Shefali

Thanks for reading DevBits by Shefali! Subscribe for free to receive new posts and support my work.

If you enjoyed this edition of DevBits, you can support my work here ☕

Buy Me a Coffee

Your support helps me keep creating more developer tips, UI snippets, and helpful content for the community.

© 2026 Shefali Jangid
548 Market Street PMB 72296, San Francisco, CA 94104