The range()
function in Python generates an immutable sequence of integers within a specific range.
The general syntax of range()
is:
range(start, stop, step)
start
: The starting value of the sequence (inclusive). If not specified, it defaults to 0.stop
: The ending value of the sequence (exclusive). This value is never part of the generated sequence.step
: The increment between each number in the sequence. If not specified, it defaults to 1.
The range()
function returns a range
object, which is memory efficient and suitable for generating large sequences of numbers.
range()
is especially useful in contexts where controlled and efficient generation of numeric sequences is needed, such as in for
loops, mathematical operations, and data manipulation.
Range Examples
Basic Usage
In this example, we use range to generate a sequence of numbers from 0 to 4, where the specified final value is not included in the sequence.
# Generate a sequence from 0 to 4 (exclusive)
for i in range(5):
print(i)
# Output: 0, 1, 2, 3, 4
Specifying Start and Stop
Here, range is used to generate a sequence that starts at 2 and ends at 8 (exclusive), showing how to define both the start and end of the sequence.
# Generate a sequence from 2 to 8 (exclusive)
for i in range(2, 9):
print(i)
# Output: 2, 3, 4, 5, 6, 7, 8
Specifying Step
In this example, we show how to generate a sequence that starts at 1 and ends at 10, with an increment of 2 at each step.
# Generate a sequence from 1 to 10 with a step of 2
for i in range(1, 11, 2):
print(i)
# Output: 1, 3, 5, 7, 9
Iterating in Reverse Order
This example demonstrates how to use range to iterate in reverse order, starting from 10 and ending at 1.
# Iterate in reverse order
for i in range(10, 0, -1):
print(i)
# Output: 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
Common Uses of Tuples
Create List from range()
In this example, we show how to use range to create a list of even numbers from 0 to 9, using the list function to convert the sequence generated by range into a list.
# Create a list of even numbers from 0 to 9
even_numbers = list(range(0, 10, 2))
print(even_numbers)
# Output: [0, 2, 4, 6, 8]
Generate Indices in a Loop
In this example, range is used to generate indices in a loop, which is useful when needing to access elements of a sequence along with their indices.
# Use range() to generate indices in a loop
word = "Python"
for i in range(len(word)):
print(f"Index {i}: {word[i]}")
# Output: Index 0: P, Index 1: y, Index 2: t, Index 3: h, Index 4: o, Index 5: n
Combine with zip()
to Iterate in Parallel
This example shows how to combine range with the zip function to iterate in parallel over two lists, allowing access to the corresponding elements of both lists in each iteration.
# Use range() along with zip() to iterate in parallel over two lists
names = ["Ana", "Juan", "María"]
ages = [30, 25, 35]
for i in range(len(names)):
print(f"{names[i]} is {ages[i]} years old.")
# Output:
# Ana is 30 years old.
# Juan is 25 years old.
# María is 35 years old.