Python range Function
Python range Function
Definition:
• The range() function in Python generates a sequence of integers, which is commonly used in for loops to
iterate over a block of code multiple times.
• The range() function has the following syntax:
range(start, stop[, step])
start: The starting integer of the sequence (default is 0).
stop: The ending limit, not inclusive.
step: The difference between each number in the sequence (default is 1).
Basic Usage Examples:
Example 1: Using range() with One Argument
for i in range(5):
print(i, end=', ')
Output:
0, 1, 2, 3, 4,
Explanation: Only the stop value is provided, so it defaults to starting from 0 and increments by 1.
Reversing a Range
You can reverse a range using a negative step or the reversed() function:
My_list = list(reversed(range(5)))
print()
Output:
[4, 3, 2, 1, 0]