python中范围函数
介绍(Introduction)
The range()
is an in-built function in Python. It returns a sequence of numbers starting from zero and increment by 1 by default and stops before the given number.
range()
是Python中的内置函数。 它返回一个从零开始的数字序列,默认情况下以1递增,并在给定数字之前停止。
Now that we know the definition of range, let’s see the syntax:
现在我们知道范围的定义,让我们看一下语法:
range(start, stop, step)
It has three parameters, in which two are optional:
它具有三个参数,其中两个是可选的:
start:
It’s an optional parameter used to define the starting point of the sequence. By default, it’s zero.start:
这是一个可选参数,用于定义序列的起点。 默认情况下为零。stop:
It’s a mandatory parameter, used to define the stopping point of the sequencestop:
这是一个强制性参数,用于定义序列的停止点step:
It’s also an optional parameter used to specify the incrementation on each iteration; by default, the value is one.step:
这也是一个可选参数,用于指定每次迭代的增量; 默认情况下,该值为1。
一般用法 (General Usage)
As it returns a sequence of numbers, most of the developers use the range to write loops. This comes handy when you don’t have a list or tuple, but only a specific value to implement the loop.
当它返回一个数字序列时,大多数开发人员都使用该范围来编写循环。 当您没有列表或元组时,只有一个特定的值来实现循环,这很方便。