Python while Loop with range() Function – A Beginner Guide
Introduction
A while loop is another way to repeat a block of code in Python. It keeps running the code as long as a specified condition is true.
In this tutorial, we’ll learn the basics of the while loop and use it to perform simple counting tasks similar to those we previously performed using for loops with range().
Let’s start by understanding what a while loop is and how it works.
1. What is a while Loop?
A while loop is used to repeat a block of code as long as a specified condition is true.
Before each repetition, Python checks the condition. If the condition is True, the code inside the loop runs. When the condition becomes False, the loop stops.
2. Python checks whether i <= 5 is true.
3. If true, the loop prints i.
4. i is increased by 1.
5. The process continues until the condition becomes false.
Next, let’s look at the basic syntax of a while loop.
2. Explain the Basic Syntax of a while Loop
The basic syntax of a while loop is simple. It consists of a condition followed by the statements that should be repeated.
- while – the keyword used to start the loop.
- condition – an expression that is checked before each repetition.
- : – the colon indicates the beginning of the loop body.
- statement – the code that is executed while the condition is True.
In this example, i starts at 1 and increases by 1 after each repetition. When i becomes 6, the condition i <= 5 becomes false, so the loop stops.
for loop with range() handles the sequence automatically.
With a while loop, you usually need to initialize, check, and update the loop variable yourself.
3. Understanding Counting with a while Loop
In the previous for loop article, we used the range() function
to generate a sequence of numbers automatically.
A while loop does not use range() directly.
Instead, we control the counting ourselves using a starting value,
a condition, and an update.
- for + range() → Python manages the sequence of numbers.
- while → We manage the starting value, condition, and update.
- Initialize – choose the starting value.
- Condition – decide when the loop should continue.
- Update – change the value after each repetition.
range() is a feature commonly used with for loops.
With a while loop, you can achieve similar counting by controlling
the loop variable yourself.
4. Counting from a Start Value to a Stop Value
In a for loop, range(start, stop) allows us to start
counting from a particular value and continue up to, but not including,
the stop value.
With a while loop, we can achieve the same result by setting the
starting value, checking the condition, and increasing the value after each repetition.
- Initialize:
i = 2sets the starting value. - Check:
i < 6determines whether the loop should continue. - Print: The current value of
iis displayed. - Update:
i += 1increases the value by 1. - When
ibecomes 6, the condition becomes false and the loop stops.
range(start, stop), the stop value is not included.
In this example, the loop prints 2, 3, 4, 5, but not 6.
while, there is no start and stop parameter.
We create the same counting behavior using an initial value, a condition, and an update.
5. Counting with a Step Value Using a while Loop
In a for loop, range(start, stop, step) allows us to
control how much the value changes after each repetition.
A while loop does not have a step parameter.
Instead, we control the step ourselves by changing the loop variable inside the loop.
- Initialize:
i = 2sets the starting value. - Check:
i < 11checks whether the loop should continue. - Print: The current value of
iis displayed. - Update:
i += 2increases the value by 2. - When
ibecomes 12, the condition becomes false and the loop stops.
The statement i += 2
means that 2 is added to i after every repetition.
Therefore, the values are: 2 → 4 → 6 → 8 → 10
while loop, the step is controlled by the update statement.
For example, i += 1 increases the value by 1,
while i += 2 increases it by 2.
range(start, stop, step) is specific to range().
With a while loop, we create similar counting behavior by
controlling the initial value, condition,
and update ourselves.
6. Three Common Ways to Count Using a while Loop
With range(), the for loop provides different ways
to control a sequence of numbers. With a while loop, we can
create similar counting patterns by controlling the starting value,
condition, and update.
Here are three common ways to count using a while loop.
- Start from a value: Begin counting from any suitable number.
- Count with a step: Use an update such as
i += 2to skip numbers. - Count backward: Use
i -= 1to decrease the value after each repetition.
The range() function provides start, stop, and step
values directly. In a while loop, we control these ideas ourselves
using the variable initialization, condition, and update statement.
while gives us more direct control over how the loop variable
changes. We decide where to start, when to stop,
and how much to change after each repetition.
7. Simple Programs Using a while Loop
Now that we understand the basic syntax and counting logic of a
while loop, let’s look at some simple programs.
These examples are suitable for beginners and help you understand how a
while loop can be used for different counting tasks.
- The loop starts with an initial value.
- The condition decides whether the loop continues.
- The update changes the loop variable after each repetition.
- Changing the update allows us to count by 1, 2, 5, or another value.
- For counting backward, we use an update such as
i -= 1.
while loop, identify three things:
Where should the counting start?
When should it stop?
and How should the value change?
8. Remember This
while loop, always ask:
“Will the condition eventually become False?”
If yes, the loop can stop normally.
9. Practice Questions
Try to solve the following programs using a while loop.
These questions will help you practise initialization, conditions,
updates, step values, and counting in different directions.
while
loop to print the numbers from 1 to 10.
while
loop to print the numbers from 10 to 1.
while loop.
while loop.
while loop.
while loop.
while
loop to calculate the sum of numbers from 1 to 10.
while
loop to print the multiples of 3 from 3 to 30.
Try writing the programs without looking at the previous examples. Pay special attention to the update statement because it controls how the loop variable changes.
10. Practice Questions – Solutions
Here are the solutions to the practice questions. Compare your programs
with these solutions and try to understand how the while loop
variable is initialized, checked, and updated.
- Increasing a variable using
i += 1. - Decreasing a variable using
i -= 1ori -= 2. - Counting with different step values.
- Using a
whileloop to calculate a sum. - Controlling exactly when a loop starts and stops.
while loop, always check the
initial value, condition, and
update. These three parts control how the loop works.
11. Conclusion
The while loop is an important Python looping statement used to repeat a block of code as long as a condition remains True.
Unlike for + range(), where Python manages the sequence of values,
a while loop gives the programmer direct control over the
starting value, condition, and
update.
while loop, remember:
Start → Check → Execute → Update → Repeat.
