Skip to content

Beginner · Loops: repeating work · topic 10 of 50 · 20 min

while loops

Explanation

A while loop repeats its block as long as a condition stays True. Python checks the condition before every round. Something inside the loop must change, or the condition stays True forever and the loop never stops.

Repeat while something is true

A while loop has a condition, like an if. Before each round, Python checks the condition. If it is True, the block runs, and then Python goes back and checks again. When the condition becomes False, the loop ends and the program continues below it.

python
count = 1
while count <= 5:
    print("Count is", count)
    count = count + 1
print("Done")
Prints Count is 1 up to Count is 5, then Done. count goes up by 1 each round.
Think of it like this: Think of filling a bucket from a tap. You do not count mugs in advance; you keep filling WHILE the bucket is not full, and you check the level again and again. As soon as it is full, you stop. That is exactly how a while loop works.

Step by step

  1. Python checks the condition: count <= 5.
  2. If it is True, the indented block runs once.
  3. Inside the block, count changes (count = count + 1).
  4. Python jumps back to step 1 and checks again.
  5. When the condition is False, Python skips the block and goes to the next line after the loop.

Saving until a goal

A while loop is useful when you do not know in advance how many rounds you need. Here we do not know how many weeks it takes to save for a cricket bat; the loop works it out.

python
saved = 0
weeks = 0
while saved < 3000:
    saved = saved + 450
    weeks = weeks + 1
print(f"After {weeks} weeks you have Rs {saved}")
Prints After 7 weeks you have Rs 3150.

Halving and digits

python
price = 1000
rounds = 0
while price > 100:
    price = price // 2
    rounds = rounds + 1
print(rounds, price)
The price is halved until it is 100 or less: 500, 250, 125, 62. Prints 4 62.
python
n = 4721
while n > 0:
    digit = n % 10
    print(digit)
    n = n // 10
n % 10 is the last digit, and n // 10 removes it. Prints 1, 2, 7 and 4: the digits from the right.
Careful: If nothing inside the loop changes the condition, the loop never ends. This is called an infinite loop. For example, while count <= 5: print(count) with no count = count + 1 prints 1 forever. If your program seems stuck, stop it and check that the loop variable really changes.
Tip: Use for when you know how many times to repeat (for each letter, for 10 days). Use while when you repeat until something happens (until the goal is reached, until the number becomes 0).

The short forms from Module 1 are very common in loops: count += 1 means count = count + 1, and n //= 10 means n = n // 10.

Watch the lesson

while loops · 1/6

while loops

In this lesson you will learn the while loop, which repeats as long as a condition is true.

Prefer a video by a teacher? Find free videos on “python while loop tutorial for beginners” on YouTube ↗

In real life: Saving for a new phone

Kavya wants a phone that costs Rs 15,000. She can save Rs 1,200 every month from her part-time job. How many months must she wait? A while loop adds one month of savings at a time until she has enough.

python
phone = 15000
monthly = 1200
saved = 0
months = 0
while saved < phone:
    saved += monthly
    months += 1
print(f"{months} months, saved Rs {saved:,}")
Prints 13 months, saved Rs 15,600.

Practice (0/3 done)

Task 1 · Code · Python

Write a function count_digits(n) that returns how many digits the positive whole number n has, using a while loop that removes one digit at a time with // 10. Example: count_digits(12345) returns 5.

Task 2 · Code · Python

Write a function months_to_goal(goal, monthly) that returns how many months of saving monthly rupees it takes to reach at least goal rupees. Example: months_to_goal(15000, 1200) returns 13.

Task 3 · Code · Python

Write a function digit_sum(n) that returns the sum of the digits of the positive whole number n. Example: digit_sum(4721) is 4 + 7 + 2 + 1 = 14.

Quick quiz

Question 1

1.When does Python check the condition of a while loop?

Question 2

2.x = 1, then: while x < 5: print(x). What happens?

Question 3

3.n = 10, then: while n > 0: n = n - 3. What is n after the loop?

Question 4

4.What does 4721 % 10 give?

Question 5

5.Which loop fits best when you repeat until something happens, without knowing how many rounds?

Read more (free)

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

← Previous: for loops and range()