range函数python
Python range()函数 (Python range() function)
The range() is a built-in function in Python which returns the sequence of values. It is used where we need to perform a specific action for a limited number of times. In general, if we write range starting from a value i and end up to the value j then we will get a sequence i, i+1 up to j-1.
range()是Python中的内置函数,它返回值的序列。 它用于需要有限次数执行特定操作的地方。 通常,如果我们写一个从值i开始到值j的范围,那么我们将得到一个序列i , i + 1到j-1 。
Syntax of range() function:
range()函数的语法:
range(start, end, step)
It generally takes three arguments which are the following,
通常需要以下三个参数,
start: It is an integer from which the sequence has to start that is starting integer of the sequence of the integer.
start :它是必须从其开始的整数,它是整数序列的起始整数。
end: It is an integer before which the sequence of integers is to be generated. Generally, if we provide an integer j then it generates sequence up to j-1.
end :它是一个整数,在此之前将生成整数序列。 通常,如果我们提供整数j,则它会生成最多j-1的序列。
step: It is used to determine the difference or increment between each integer in the sequence.
step :用于确定序列中每个整数之间的差或增量。
There are three-way by which we can call the range in the program which is the following,
我们可以通过以下三种方式在程序中调用范围:
range(end):
范围(结束) :
We will give only one argument ending in the
我们只会给出一个以
range() function when we want to start the sequence of the integer with 0. If we give the value of end is j the this is seen as a sequence whose upper limit is j and the lower limit is 0 and step is 0.
当我们想以0开头整数序列时,会使用range()函数。如果给定end的值为j,则这将被视为一个序列,其上限为j,下限为0,step为0。
range(start, end):
范围(开始,结束) :
When the user decides to generate the sequence starting with a specific integer and also end before a specific integer then they call the range function with the two-argument. When the user gives the value of start then it works like the range function with one argument i.e
当用户决定生成以特定整数开头并以特定整数结尾的序列时,他们将使用两个参数调用range函数。 当用户提供start的值时,它的工作方式类似于带有一个参数的range函数,即
range(end) because the range() function by default starts the sequence with zero.
range(end),因为默认情况下range()函数从零开始序列。
range(start, end, step):
范围(开始,结束,步骤) :
When we want to generate a sequence where we skip or increment the sequence integer by a value other than 1 that is we want a kind of arithmetic progression which have a common difference greater than 1 then we call the
当我们要生成一个序列时,我们将序列整数跳过或增加一个非1的值,即我们想要一种算术级数,其公有差异大于1,则我们称
range() function with the three arguments. If we call a range() function with no step then by default it goes for 1.
具有三个参数的range()函数 。 如果我们不带任何步长调用range()函数 ,则默认情况下它为1。
Let's see an example by which we understand it in a better way.
让我们看一个例子,通过它我们可以更好地理解它。
Program:
程序:
#call range() with one argument
print('First sequence:')
for k in range(10):
print(k,end=' ')
print()
#call range() with two argument
print('Second sequence:')
for k in range(2,10):
print(k,end=' ')
print()
#call range() with two argument
print('Third sequence:')
for k in range(2,20,1):
print(k,end=' ')
print()
#call range() with negative step
print('Fourth sequence:')
for k in range(20,1,-2):
print(k,end=' ')
print()
Output
输出量
First sequence:
0 1 2 3 4 5 6 7 8 9
Second sequence:
2 3 4 5 6 7 8 9
Third sequence:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Fourth sequence:
20 18 16 14 12 10 8 6 4 2
翻译自: https://www.includehelp.com/python/range-function-with-example.aspx
range函数python