Home > Backend Development > Python Tutorial > How to use the range() function in python

How to use the range() function in python

angryTom
Release: 2020-02-29 13:48:21
Original
29075 people have browsed it

How to use the range() function in python

How to use the range() function in python

The range() function in python can create a list of integers, Generally used in for loops.

range() function syntax:

range(start,stop[,step])
Copy after login

Recommendation:Python tutorial

Parameter description:

star: Counting starts from star. The default is to start from 0.

stop: Counting ends at stop, but does not include stop.

step: Step Long, the default is 1.

Example:

>>>range(10)        # 从 0 开始到 10,没有10
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1, 11)     # 从 1 开始到 11,没有11
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> range(0, 30, 5)  # 步长为 5
[0, 5, 10, 15, 20, 25]
>>> range(0, 10, 3)  # 步长为 3
[0, 3, 6, 9]
>>> range(0, -10, -1) # 负数
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
>>> range(0)
[]
>>> range(1, 0)
[]
Copy after login

Many python video tutorials, all on the PHP Chinese website, welcome to learn online!

The above is the detailed content of How to use the range() function in python. 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