range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number. It is commonly used for looping a specific number of times in
range(start, stop, step)
range() function can be called with one, two, or three arguments:
range(start, stop, step)
start: An integer number specifying at which position to start (inclusive). The default value is 0.
stop: An integer number specifying at which position to end (exclusive).
step: An integer number specifying the incrementation. The default value is 1.
range() function returns an immutable sequence of numbers between the start and stop parameters.
The Workings of range()
range() function is called, it doesn’t create a list of numbers; instead, it creates a
range object that yields the next number in the sequence only when needed (lazy evaluation). This design means that a
range() call can be extremely memory-efficient when dealing with large ranges.
range() is called with a single argument, it generates a sequence from 0 up to but not including the argument value.
for i in range(5): print(i)
0 1 2 3 4
This loop prints numbers from 0 to 4.
When supplied with two arguments,
range() starts the sequence with the first argument and ends with the second argument, without including it.
for i in range(2, 6): print(i)
2 3 4 5
This loop prints numbers from 2 to 5.
The third form of
range() includes a step parameter, which determines the increment between each number in the sequence.
for i in range(0, 10, 2): print(i)
0 2 4 6 8
This will print even numbers from 0 to 8.
Iteration and Performance
range() function is particularly useful because it saves memory. It does this by storing only the start, stop, and step values, regardless of the range size.
range() objects support iteration and are compatible with the
range() object will always have the same small memory footprint, no matter the size of the range it represents.
Common Use Cases
Looping Through an Index
One of the most common uses of
range() is to loop through indexes in a sequence.
colors = ['red', 'green', 'blue'] for i in range(len(colors)): print(colors[i])
red green blue
Using a negative step parameter,
range() can be used to count downwards.
for i in range(10, 0, -1): print(i)
10 9 8 7 6 5 4 3 2 1
This prints numbers from 10 down to 1.
range() returns a
range object, it can be easily converted into a list.
numbers = list(range(5)) print(numbers)
[0, 1, 2, 3, 4]
Beyond simple loops,
range() can be combined with list comprehensions to create complex lists.
squares = [x*x for x in range(10)] print(squares)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
This creates a list of squares of numbers from 0 to 9.
Use _ for Unused Variables
When the index is not used within the loop, it’s common to see
_ as the loop variable.
for _ in range(5): print("Hello, World!")
Hello, World! Hello, World! Hello, World! Hello, World! Hello, World!
Check Membership Efficiently
range() can check if a number is part of the sequence very quickly, without iterating through it.
if 50 in range(100): print("50 is within 0 and 99")
Modifying Lists During Iteration
It’s generally a bad idea to modify a list while iterating over it. Instead, iterate over a copy of the list or iterate over the
Floating Point Arguments
range() function does not support floating-point numbers. If you need to use floats, you might consider using the
numpy library or creating a custom range function.
range() function is a powerful tool in Python’s arsenal, providing an efficient way to generate sequences of numbers for iteration. Whether you’re running simple loops or building complex list comprehensions, understanding the nuances of
range() is key to writing effective Python code. As you move forward in your Python journey, you’ll find
range() to be an indispensable part of your coding toolkit, as it supports a broad range of applications from loop control to sequence generation. Always remember to keep performance considerations in mind and to adhere to best practices to make the most of this fundamental Python feature.