Home >Backend Development >Python Tutorial >What is range in python?

What is range in python?

(*-*)浩
(*-*)浩Original
2019-06-25 13:04:204204browse

The python range() function can create a list of integers and is generally used in for loops.

What is range in python?

##Function Syntax (Recommended Learning: Python Video Tutorial)

range(start, stop[, step])

Parameter description:

start: Counting starts from start. The default is to start from 0. For example, range(5) is equivalent to range(0, 5);

stop: counts to the end of stop, but does not include stop. For example: range (0, 5) is [0, 1, 2, 3, 4] without 5

step: step size, default is 1. For example: range(0, 5) is equivalent to range(0, 5, 1)

Example

>>>range(10)        # 从 0 开始到 10
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1, 11)     # 从 1 开始到 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)
[]
The following is the use of range in for, loop out Each letter of runoob:

>>>x = 'runoob'
>>> for i in range(len(x)) :
...     print(x[i])
... 
r
u
n
o
o
b
>>>
For more Python-related technical articles, please visit the

Python Tutorial column to learn!

The above is the detailed content of What is range in python?. 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