![]() |
VOOZH | about |
range() function in Python is used to generate a sequence of numbers. It is widely used in loops or when creating sequences for iteration.
Let’s look at a simple example of the range() method.
0 1 2 3 4
Explanation:
0 up to 4 (excluding 5) and prints them.range() Methodrange(start, stop, step)
0.1.range object, which can be converted to a list or used directly in loops.range() MethodWhen range() function called without arguments defaults to starting at 0 and ending at a specified value (exclusive). It generates a sequence of numbers from 0 to that value.
[0, 1, 2, 3, 4]
Explanation:
0 by default and ends at 5 (exclusive).We can specify both the starting and stopping points of the range. The range(start, stop) generates numbers starting from the start value up to (but not including) the stop value.
[2, 3, 4, 5, 6, 7, 8, 9]
Explanation:
2 and ends at 10 (exclusive).range(start, stop, step) allows us to specify a step value, which controls the difference between consecutive numbers in the range. A positive step generates an increasing sequence, while a negative step generates a decreasing one.
[1, 3, 5, 7, 9]
Explanation:
2 in each step.By specifying a negative step value, the range() function creates a sequence that decrements from the starting point down to the stopping point, providing reverse order numbers.
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
Explanation:
range()range() does not store all values in memory but generates them on demand, making it ideal for large sequences.range object cannot be changed after creation.