Table of Contents | |
What is the range()
Function in Python?
Python range() function lets you enter the starting and ending numbers and creates all the numbers within that specified range. The range()
function is commonly used in loops or other parts of your code.
Python range()
Function Syntax
Python range()
Function Example
# Generate numbers from 0 to 4 (stop value is not included)
numbers = range(5)
# Print the sequence of numbers
print(numbers) # Output: range(0, 5)
# Generate numbers from 2 to 10 with a step of 2 (even numbers from 2 to 8)
even_numbers = range(2, 10, 2)
# Print the sequence of even numbers
print(list(even_numbers)) # Output: [2, 4, 6, 8]
Code Explanation
- Line 2: Creates a sequence of numbers from 0 to 4 (excluding 4) using
range(5)
. - Line 5: Prints the generated sequence object, which isn’t directly printable but shows the start and stop range.
- Line 8: Creates a sequence of even numbers from 2 to 9 (excluding 10) with a step of 2 using
range(2, 10, 2)
. - Line 11: Converts the sequence to a list using
list()
for printing. This is becauserange()
itself returns an iterable object, not a list.
Output
range(0, 5)
[2, 4, 6, 8]
Python range(stop)
The simplest form of range() requires only one argument, the stop value. This defines the upper limit (not included) for the generated sequence.
Syntax
number_sequence = range(stop)
It starts at 0 and goes up to (but not including) stop.
Example
# Generate a sequence of numbers from 0 to 4 (stop value is not included)
numbers = range(5)
# Print the sequence of numbers
print(numbers) # Output: range(0, 5)
Code Explanation
- Line 2: Creates a sequence of numbers from 0 to 4 (excluding 4) using
range(5)
. Here, stop is set to 5, but the sequence stops before 5. - Line 5: Prints the generated sequence object, which isn’t directly printable but shows the start and stop range.
Python range(start, stop)
In Python, the range() function offers more control over the generated sequence using start and stop arguments.
Syntax
number_sequence = range(start, stop)
It starts at the start, and goes up to (but not including) the stop.
Example
# Generate numbers from 3 to 8 (stop value is not included)
numbers = range(3, 8)
# Print the sequence of numbers
print(numbers) # Output: range(3, 8)
Code Explanation
- Line 2: Creates a sequence of numbers from 3 to 7 (excluding 8) using
range(3, 8)
. Here, start is set to 3 (included) and stop is set to 8 (not included). - Line 5: Prints the generated sequence object, which shows the start and stop range.
Python range(start, stop, step)
You can use all three arguments—start, stop, and step—to comprehensively control the sequence of numbers generated by range().
Syntax
number_sequence = range(start, stop, step)
It starts at start, goes up to stop (not including it), and increments by the step value.
Example
# Generate even numbers from 2 to 10 (stop value is not included)
even_numbers = range(2, 11, 2)
# Print the sequence of even numbers
print(even_numbers) # Output: range(2, 10, 2)
# Print the list of even numbers (converted from the sequence)
print(list(even_numbers)) # Output: [2, 4, 6, 8]
Code Explanation
- Line 2: Creates a sequence of even numbers from 2 to 10 (excluding 10) with a step of 2 using
range(2, 11, 2)
. Here, start is 2 (included), stop is 11 (not included), and step is 2 (adding 2 to get the next number in the sequence). - Line 5: Prints the generated sequence object, which shows the start, stop, and step values.
- Line 6: Converts the sequence to a list using
list()
for printing. This is becauserange()
itself returns an iterable object, not a list directly.
The range()
Function with Python for Loop
The range() function is a super helpful partner to for loop. It generates a sequence of numbers to control how many times your loop runs. Here’s a breakdown of the different ways to use it:
1. range(stop)
for i in range(5): # Generates 0, 1, 2, 3, 4
print(i)
Generates numbers from 0 up to (but not including) the stop value.
2. range(start, stop)
for i in range(2, 7): # Generates 2, 3, 4, 5, 6
print(i)
Starts at the start number and goes up to (but not including) the stop value.
3. range(start, stop, step)
for i in range(1, 10, 2): # Generates 1, 3, 5, 7, 9
print(i)
Starts at the start value, goes up to (but not including) stop, and increments by the step value.
Positive Step And Negative Step Values
Python Range function allows you to control how the sequence progresses using a step value. A positive step creates an incrementing sequence, while a negative step generates a decrementing sequence.
Example
# Positive step: Incrementing by 2 (start at 0, stop before 10)
for num in range(0, 10, 2):
print(num, end=" ")
# Negative step: Decrementing by 3 (start at 10, stop before 1, exclude 1)
for num in range(10, 0, -3):
print(num, end=" ")
Code Explanation
- Lines 2-3: Loop through a
range(0, 10, 2)
sequence, which starts at 0, stops before 10, and increments by 2. Theend=" "
argument inprint
keeps numbers on the same line. - Lines 6-7: Loop through a
range(10, 0, -3)
sequence, which starts at 10, stops before 1 (excluding 1), and decrements by 3.
Output
0 2 4 6 8 10 7 4 1
Accessing range()
with an Index Value
While range() in Python is excellent for generating sequences for loops, you cannot directly access elements within the sequence using indexing like a list. However, you can combine range() with a loop to achieve this.
Example
# Generate a sequence from 0 to 4
numbers = range(5)
# Loop through the indices and access elements using the index
for idx in range(len(numbers)):
print(f"At index {idx}, value is {numbers[idx]}")
Code Explanation
- Line 2: Creates a
range(5)
sequence containing numbers from 0 to 4 (excluding 4). - Lines 5-6: Loop through a
range(len(numbers))
sequence. This creates a loop that iterates the same number of times as the number of elements innumbers
. Inside the loop:idx
represents the current index (0, 1, 2, 3, 4).numbers[idx]
accesses the value at the corresponding index within thenumbers
sequence.
Output
0 index, value is 0
1 index, value is 1
2 index, value is 2
3 index, value is 3
4 index, value is 4
Convert range()
to list
In Python, the range() function is an efficient way to generate sequences of numbers. However, range() itself returns an iterable object, not a list. You can easily convert a range () object to a list to use the elements in various list functions or access them by index.
Example
# Generate a sequence of numbers from 0 to 4 (stop value not included)
number_sequence = range(5)
# Convert the sequence to a list using list()
number_list = list(number_sequence)
# Print the elements of the list
print(number_list) # Output: [0, 1, 2, 3, 4]
Code Explanation
- Line 2: Creates a sequence of numbers from 0 to 4 (excluding 4) using
range(5)
. - Line 5: Converts the
number_sequence
(which is a range object) to a list using thelist()
function. This creates a new listnumber_list
containing the same elements as the original sequence. - Line 8: Prints the
number_list
, which now behaves like a regular Python list.