range函数python
范围: (Range:)
The range type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops.
范围类型表示数字的不可变序列,并通常用于在for循环循环的特定次数。
range(stop)
range(start,stop,step)
start
The value of the start parameter (or 0
if the parameter was not supplied)
start
start参数的值(如果未提供该参数,则为0
)
stop
The value of the stop parameter
stop
stop参数的值
step
The value of the step parameter (or 1
if the parameter was not supplied).If the step is 0, it will raise ValueError.
step
step参数的值(如果未提供该参数,则为1
)。如果step为0,则将引发ValueError。
The arguments to the range function should be integers. (either built-in int
or any object that implements the __index__
special method)
范围函数的参数应为整数。 (内置int
或实现__index__
特殊方法的任何对象)
Example 1:Only the stop parameter is given.
示例1:仅给出stop参数。
range(10)
range(10)
start by default will be 0 and step by default will be 1
默认情况下, start为0 ,默认情况下, step为1
stop
is given as 10.stop
为10 。- stop value is excluded. It generates value until 9 only. 停止值被排除。 它直到9才产生值。
It will return a range object containing numbers starting from 0 to 9.
它将返回一个范围从0到9的数字的范围对象。
We can convert the range object to list using list() constructor.
我们可以使用list()构造函数将范围对象转换为列表。
We can also iterate using for loop
我们还可以迭代使用for循环
r=range(10)
print (r)#Output:range(0, 10)print (type(r))#Output:<class 'range'>print (list(r))#Output:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Example 2:Only the start and stop parameter is given.
示例2:仅给出start和stop参数。
range(1,10)
range(1,10)
step by default will be 1
默认情况下, step为1
It will generate a sequence of numbers starting from 1 to 9.
它将生成一个从1到9的数字序列。
r=range(1,10)
print (r)#Output:range(1, 10)
#Converting range object to listprint (list(r))#Output:[1, 2, 3, 4, 5, 6, 7, 8, 9]
Example 3:start, stop and step parameter is given
<