Blog Post #35: Generating Numbers on Demand with the range() Function

In Post #34, we learned that the for loop is the perfect tool for iterating over a sequence like a string or a list. But what if you don’t have a pre-existing sequence? What if you simply want to repeat a block of code a specific number of times, like 5 times or 100 times?

Manually creating a list of numbers just to control a loop would be tedious. To solve this, Python provides a powerful and efficient built-in function called range().

In this post, we’ll take a deep dive into the range() function and its three parameters, which give you precise control over your for loops.

range() with One Argument: range(stop)

The simplest and most common way to use range() is with a single number, which acts as the stop value.

The rule is: range(stop) generates a sequence of integers starting from 0 up to, but not including, the stop number. This “up to but not including” behavior is a very common pattern in Python.

For example, range(5) will generate the numbers 0, 1, 2, 3, 4.

print("Looping 5 times:")
for number in range(5):
    print(f"This is loop iteration number {number}")

This is the perfect tool for when you need to perform an action an exact number of times.

range() with Two Arguments: range(start, stop)

If you want your sequence to start at a number other than 0, you can provide two arguments: start and stop.

The rule is: range(start, stop) generates integers from the start number up to, but not including, the stop number.

Let’s say we want to print the numbers from 1 through 5.

print("\nPrinting numbers from 1 to 5:")
for number in range(1, 6):
    print(number)

Notice we have to use 6 as the stop value to ensure that 5 is the last number included in the sequence.

range() with Three Arguments: range(start, stop, step)

The full power of range() is unlocked with its third optional argument: step. The step value determines the increment (or “jump”) between the numbers in the sequence.

Counting Up in Steps

You can use the step argument to skip numbers. For example, to print only the even numbers from 2 to 10:

print("\nPrinting even numbers from 2 to 10:")
for number in range(2, 11, 2):
    print(number)

This starts at 2, stops before 11, and jumps by 2 each time, producing 2, 4, 6, 8, 10.

Counting Down

The step can also be a negative number, which allows you to count down. To count down, you make the start value higher than the stop value and provide a negative step.

print("\nCountdown!")
for number in range(5, 0, -1):
    print(number)
print("Liftoff!")

This starts at 5, stops before 0, and subtracts 1 each time, producing 5, 4, 3, 2, 1.

What’s Next?

The range() function is an essential companion to the for loop. It gives you an easy and efficient way to generate sequences of numbers, allowing you to control exactly how your loops run, whether you’re counting up, down, or in steps.

We have now learned about two different kinds of loops: the while loop, which runs as long as a condition is true, and the for loop, which iterates over a sequence. Which one should you use, and when? In Post #36, we will compare for and while loops directly and provide clear guidelines for choosing the right loop for the job.

Author

Debjeet Bhowmik

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com

Leave a Comment