Introduction: Python range() Function
When working with Python, you often need to generate a sequence of numbers for repeating tasks. Whether you’re controlling loop iterations, processing a fixed number of values, or creating numeric sequences dynamically, doing this manually can make code longer and less efficient.
Without a built-in function, you would need to create number sequences yourself or write extra logic to generate them, making programs longer and harder to maintain.
The Python range() Function provides a simple and efficient solution for these tasks.
What it is: The range() function is a built-in Python function that generates a sequence of numbers within a specified range. It is commonly used with loops to repeat a block of code a specific number of times, eliminating the need to manually create numeric sequences.
Take a look at a quick example to understand how the function works.
You can also review its practical use cases across different programming scenarios.
Now let’s understand its syntax, parameters, and return value before exploring practical examples.
Syntax, Parameters, Return Value and Examples: Python range() Function
The following section explains the syntax, parameters, return value, and a quick example of the Python range() Function.
Syntax
range(stop)
range(start, stop)
range(start, stop, step)Parameters
| Parameter | Description |
|---|---|
start (optional) | The starting value of the sequence. The default value is 0. |
stop | The ending value of the sequence. The specified value is not included in the generated sequence. |
step (optional) | The difference between each number in the sequence. The default value is 1. |
Return Value
| Return Value | Description |
|---|---|
| range object | Returns a range object that represents an immutable sequence of numbers. |
Quick Example
The following example generates a sequence of numbers using the range() function.
numbers = range(5)
print(list(numbers))
# Output:
[0, 1, 2, 3, 4]
The range() function generates numbers starting from 0 up to, but not including, the specified stop value.
How the Python range() Function Works
- The
range()function generates a sequence of numbers based on the specified parameters. - If only one argument is provided, it is treated as the
stopvalue and the sequence starts from0. - The
startparameter defines the beginning value of the sequence. - The
stopparameter determines where the sequence ends, excluding the specified value. - The optional
stepparameter controls the difference between consecutive values. - The function returns a range object instead of directly creating a list.
Examples: Python range() Function
The following examples show how the Python range() Function works in different programming scenarios.
Example 1: Using range() with Stop Value
for number in range(5):
print(number)
# Output:
0
1
2
3
4
Explanation: When a single value is provided, the range() function starts from 0 and generates numbers up to, but not including, the specified value.
Example 2: Using Start and Stop Values
for number in range(2, 6):
print(number)
# Output:
2
3
4
5
Explanation: The range() function starts the sequence from the specified start value and stops before reaching the stop value.
Example 3: Using Step Value
for number in range(0, 10, 2):
print(number)
# Output:
0
2
4
6
8
Explanation: The step parameter controls the difference between numbers generated in the sequence.
Example 4: Generating a Reverse Sequence
for number in range(5, 0, -1):
print(number)
# Output:
5
4
3
2
1
Explanation: A negative step value allows the range() function to generate numbers in descending order.
Example 5: Creating a List Using range()
numbers = list(range(1, 6))
print(numbers)
# Output:
[1, 2, 3, 4, 5]
Explanation: The range() object can be converted into a list to store and display the generated sequence of numbers.
Example 6: Using range() with User Input
limit = int(input("Enter the limit: "))
for number in range(1, limit + 1):
print(number)
# Sample Output:
Enter the limit: 5
1
2
3
4
5
Explanation: The user-provided value determines the limit of the sequence generated by the range() function.
Use Cases: When to use the range() Function
Below are some common situations where the Python range() Function becomes useful:
- Generating number sequences for loop iterations.
- Repeating a block of code a specific number of times.
- Creating sequences with custom start, stop, and step values.
- Traversing indexes of lists, tuples, and other sequences.
- Generating increasing or decreasing numerical patterns.
Key Takeaways: range() Function
Before wrapping up, here are the key points to remember about the Python range() Function:
- The
range()function generates a sequence of numbers within a specified range. - It supports
start,stop, andstepparameters for creating flexible sequences. - The
stopvalue is excluded from the generated sequence. - It returns a range object instead of directly creating a list.
- It is commonly used with loops to repeat tasks and control iterations.
- It can generate both increasing and decreasing sequences using positive or negative step values.
In short, the Python range() Function makes it easier to generate number sequences and control loop execution, helping developers write cleaner and more organized Python code.