What Is A Built-In Function That Python Uses To Iterate Over A Number Sequence?
Range() generates a list of numbers, which is used to iterate over for loops.
for i in range(5): print(i)
The range() function accompanies two sets of parameters.
- stop: It is the no. of integers to generate and starts from zero. eg. range(3) == [0, 1, 2].
- range([start], stop[, step])
- Start: It is the starting no. of the sequence.
- Stop: It specifies the upper limit of the sequence.
- Step: It is the incrementing factor for generating the sequence.
- Points to note:
- Only integer arguments are allowed.
- Parameters can be positive or negative.
- The range() function in Python starts from the zeroth index.
range is the built-in function to be used for interating the sequence of numbers.
for iter in range (0, 10):
print iter # prints 0 to 9
for iter in range (0, 10, 2): # The last argument is the sequence to use. Default is 1.
print iter # prints 0, 2, 4, 6, 8
Python range() accepts an integer and returns a range object, which is nothing but a sequence of integers. Let’s understand how to use range() function with the help of simple examples.
- print(“range function example”)
- print(“Printing range function result”)
- for i in range(3):
- print(i, end=‘, ‘)
- range function example
- Printing range function result
- 0, 1, 2