In Python, range() is a built-in function that generates a sequence of numbers. It's commonly used in loops like for and while to specify the range of values to iterate over.
Syntax of range():
range(start, stop, step)
start (optional): The value where the sequence starts. If not provided, it defaults to 0.
stop: The value where the sequence ends (but does not include this value).
step (optional): The difference between each pair of consecutive numbers. If not provided, it defaults to 1.
Example 1: Basic range() Usage
for i in range(5):
print(i)
Output:
0
1
2
3
4
Here:
The range(5) generates a sequence starting from 0 up to (but not including) 5.
The loop iterates over the numbers 0, 1, 2, 3, 4.
Example 2: Specifying the start Value
You can specify where the sequence starts by providing the start parameter.
for i in range(2, 6):
print(i)
Output:
2
3
4
5
Here:
The range(2, 6) generates a sequence starting from 2 up to (but not including) 6.
The loop iterates over the numbers 2, 3, 4, 5.
Example 3: Using the step Parameter
The step parameter determines the difference between each consecutive number in the range.
for i in range(0, 10, 2):
print(i)
Output:
0
2
4
6
8
Here:
The range(0, 10, 2) generates numbers from 0 to 9 with a step size of 2, i.e., 0, 2, 4, 6, 8.
Example 4: Negative step Value
You can also use a negative step to generate a sequence that goes backwards.
for i in range(10, 0, -2):
print(i)
Output:
10
8
6
4
2
Here:
The range(10, 0, -2) generates numbers starting from 10 down to (but not including) 0 with a step size of -2.
Example 5: range() with for Loop and else Clause
You can use the else clause with a for loop to execute code after the loop finishes its iteration.
for i in range(3):
print(i)
else:
print("Loop finished!")
Output:
0
1
2
Loop finished!
Here:
The loop runs from 0 to 2 and then the else block is executed after the loop finishes.
Example 6: Converting range() to a List
The range() function returns a range object, which is an iterable, not a list. However, you can convert it to a list to see all the numbers in the sequence.
numbers = list(range(5))
print(numbers)
Output:
[0, 1, 2, 3, 4]
Key Points About range():
range() is often used in for loops to iterate over a sequence of numbers.
The start value is inclusive, meaning it’s included in the sequence, while the stop value is exclusive, meaning it’s not included.
The step value determines how much the number changes in each iteration. A positive step moves forward, while a negative step moves backward.
range() works with both positive and negative steps, and it can be used for both ascending and descending sequences.
Good Practices:
range() is efficient in terms of memory usage because it does not generate a list, it creates an iterable object that generates values on the fly.
Always ensure the stop value is greater than or equal to the start value when using a positive step, and vice versa when using a negative step, to avoid empty sequences.
Let me know if you'd like to see more examples or need further clarification! 😊
0 Comments