PYTHON BASICS • LESSON 14

while Loops in Python

Learn how Python repeats code while a condition remains True. Practice counters, conditions, output prediction, debugging, validation, and data analytics examples.

1. What is a while Loop?

A while loop repeatedly executes a block of code as long as its condition is True.

count = 1

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

The condition is checked before every iteration.

CHECK YOUR UNDERSTANDING

When does a while loop continue running?

2. Basic while Loop Syntax

while condition:
    # code to repeat

The condition is checked before each repetition. Like an if statement, a while statement requires a colon and an indented block.

Fill in the Blank

Complete the syntax: ___ count < 5:

3. The Counter Pattern

A common while-loop pattern uses a counter that changes during every iteration.

count = 1

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

The counter changes from 1 → 2 → 3 → 4. When it reaches 4, the condition becomes False and the loop stops.

CHECK YOUR UNDERSTANDING

What will this loop print?

4. Predict the Output

number = 5

while number > 2:
    print(number)
    number = number - 1

CHECK YOUR UNDERSTANDING

What is the last number printed?

5. Updating the Variable

The variable controlling a while loop normally needs to change. Otherwise, the condition may remain True forever.

count = 1

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

count += 1 is a shorter way to write count = count + 1.

CHECK YOUR UNDERSTANDING

Why must count be updated?

6. Avoiding Infinite Loops

An infinite loop occurs when the condition never becomes False.

count = 1

while count <= 5:
    print(count)

Here, count never changes, so the condition remains True forever.

CHECK YOUR UNDERSTANDING

What is the problem with this loop?

7. Complete the Code

Fill in the Blank

count = 1; ___ count <= 5:

Fill in the Blank

Inside the loop: count ___ 1

Fill in the Blank

A while loop stops when its condition becomes ___

8. while Loop with an if Statement

number = 1

while number <= 5:
    if number % 2 == 0:
        print(number)
    number += 1

The loop checks each number and prints only the even numbers.

CHECK YOUR UNDERSTANDING

What numbers will be printed?

9. Validation with while Loops

while loops are useful when a program needs to continue until a valid condition is reached.

age = 15

while age < 18:
    print("Age is below 18")
    age += 1

print("Age requirement reached")

10. while Loops in Data Analytics

while loops can be useful when processing continues until a specific business condition is reached.

target = 100000
sales = 0
transactions = 0

while sales < target:
    sales += 25000
    transactions += 1

print(transactions)

CHECK YOUR UNDERSTANDING

How many transactions are needed to reach ₹100,000 when each adds ₹25,000?

11. Predict the Output

x = 10

while x > 4:
    x -= 2

print(x)

CHECK YOUR UNDERSTANDING

What will be printed?

12. Debugging a while Loop

number = 1

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

The counter is moving in the wrong direction. It starts at 1 and becomes smaller, so it will never become greater than 5.

CHECK YOUR UNDERSTANDING

How should the counter be changed?

13. Mini Challenge — Reach a Sales Target

A business starts with ₹20,000 and adds ₹15,000 after every transaction. How many transactions are needed to reach at least ₹80,000?

sales = 20000
transactions = 0

while sales < 80000:
    sales += 15000
    transactions += 1

print(transactions)

CHECK YOUR UNDERSTANDING

How many transactions are required?

14. Final Challenge — Process Until Target

A data analyst processes records until cumulative revenue reaches ₹500,000.

revenues = [120000, 90000, 150000, 80000, 110000]

total = 0
index = 0

while total < 500000 and index < len(revenues):
    total += revenues[index]
    index += 1

print(index)
print(total)

CHECK YOUR UNDERSTANDING

How many records are processed before the target is reached?

Lesson 14 Recap

  • ✓ A while loop repeats while a condition is True.
  • ✓ The condition is checked before every iteration.
  • ✓ Counters are commonly used with while loops.
  • ✓ The loop variable should normally be updated.
  • ✓ Failure to update the variable can create an infinite loop.
  • ✓ while loops can contain if statements.
  • ✓ while loops are useful when repetition depends on a changing condition.
  • ✓ Always make sure the loop has a clear stopping condition.