How to use the range function in python3.5

silencement
Release: 2019-07-08 10:18:00
Original
2589 people have browsed it

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]
Copy after login
>>> list(range(0,10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Copy after login
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Copy after login

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]
Copy after login
>>> 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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template