Mastering While Loops in Python | Lesson 8

Last updated on March 15th, 2026 at 08:26 pm

Grammarly Writing Support

A while loop in Python is a construct that repeatedly executes a block of code as long as the condition remains true. When the condition becomes false, the loop immediately terminates, and the next statement in the program is executed.

The Power of Repetition in Programming | While Loops in Python

A while loop is one of the most intuitive and versatile looping structures in programming. It is a conditionally controlled loop. This basically means that so long as the provided condition remains true, the code will keep executing. This condition may be hard-coded or user-defined.

Real-world analogies of while loops in Python

Here are some real-world analogies that can help you understand while loops.

  • Keep stirring the mixture until it becomes smooth. This means that while the mixture is not smooth, keep stirring.
  • Add 2 every month while the total is less than 10. The while loop interpretation is while total is < 10 keep adding 2 monthly.
  • Play the piano until you can play without mistakes. The while loop interpretation is while mistakes exist: keep practising.

Breaking down the syntax of a while loop

As defined, basic syntax executes a block of code as long as the condition specified by the user remains true. Here is an example:

count = 1

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

So here is what this while loop is doing:

  • The first line count = 1 means store 1 in the variable called count.
  • The second line: while count <=1 marks the beginning of the loop with the condition count<=5.
  • The line print(count) begins with an indentation. This marks it as a line governed by the while condition. This line will repeat as long as the condition while count <=1 remains true. It will print the value of the variable count.
  • The next line, count=count+1 is also indented. This means that this line will also be executed as long as the while condition remains true.

For vs while loop

Both for and while loops will complete the repetition process, but they typically have different strengths and are used in different cases. Additionally, while the while loop uses a condition, the for loop does not.

A Simple While Loop Example

A step-by-step breakdown of a basic counter

I will create a step-by-step breakdown of a basic counter:

  • The first step is to initialize, or, in layman’s terms, insert a value in a variable upon declaration. eg,
letter = "A"
  • The second step is to add a condition to the loop. The loop will continue to run as long as the condition you specify is true. Once the loop exceeds the condition, it automatically stops running.
while letter <= "E":
  • The third step is to display the current value of count before the string, using the f function, so the user can insert a variable value using count.
print(f"count is: {count}")
  • The final line of code is used to increase the count by one each time the condition given is proven true; without this line, you would have a loop that continues to print the same thing over and over forever.
letter = chr(ord(letter) + 1)

Below is the output expected when the code runs:

A
B
C
D
E

Here is the practice code below for you to try

letter = "A"

while letter <= "E":
    print(letter)
    letter = chr(ord(letter) + 1)

What are while loops in Python?

A while loop is a loop that executes repeatedly as long as a condition remains true

How do I end a while loop in Python?

In Python, a while loop can be concluded in several ways. Below are two common ways to stop a while loop.

The first is for a condition to become false. Every while loop has a condition. The program runs until the condition is no longer true.

Another way is by using break. When a break is encountered, the loop stops even if the condition is true.

Before you go

I hope you enjoyed this lesson. If you have any questions, please leave them in the comments section below. If you are serious about learning Python, be sure to read lesson 9, where you will learn about nested loops.

Resources and additional Reading

Python Software Foundation. (2024). The Python Tutorial: Control Flow Tools.
https://docs.python.org/3/tutorial/controlflow.html

Python Crash Course. Matthes, E. (2023). Python Crash Course: A Hands-On, Project-Based Introduction to Programming. No Starch Press.

Leave a Comment