Home  >  Article  >  Backend Development  >  How to use the range function in python3.5

How to use the range function in python3.5

silencement
silencementOriginal
2019-06-26 11:30:552599browse

How to use the range function in python3.5

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: &#39;float&#39; 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn