Function
Generate a series of integers and return a range object
Syntax:
range(start,end,step)
range(start,end)
range(end)
The range function has three parameters: start, end, step.
For example: generate a list of numbers 0-1:
>>> list(range(0,10,1)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(0,10)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(10)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
The output results in the three cases are the same
start is the starting value (inclusive), end is the terminal value (exclusive), and step is the step size.
range(start,end)——When step is omitted, the default step size is 1; range(end)——When step and start are omitted, the default step size is 1 and the starting value is 0
Note: The value of step cannot be 0 or a floating point number
>>> list(range(2,10,2)) [2, 4, 6, 8]
>>> list(range(2,10,2.5)) Traceback (most recent call last): File "<pyshell#16>", line 1, in <module> list(range(2,10,2.5)) TypeError: 'float' object cannot be interpreted as an integer
The above is the detailed content of How to use the range function in python3.5. For more information, please follow other related articles on the PHP Chinese website!