Home >Backend Development >Python Tutorial >How to use python range function
How to use the python range function?
The python range() function creates a list of integers and is generally used in for loops.
Function syntax
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 >>>
Related recommendations: "Python Tutorial"
The above is the detailed content of How to use python range function. For more information, please follow other related articles on the PHP Chinese website!