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)
startThe value of the start parameter (or 0 if the parameter was not supplied)
start start参数的值(如果未提供该参数,则为0 )
stopThe value of the stop parameter
stop stop参数的值
stepThe 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
stopis 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
示例3:给出了start,stop和step参数
range(1,10,2)
range(1,10,2)
It will generate a sequence from 1, increment by 2, and will stop at 9.
它将生成一个从1开始的序列,以2为增量递增,并在9处停止。
r=range(1,10,2)
print (r)#Output:range(1, 10, 2)
#Converting range object to listprint (list(r))#Output:[1, 3, 5, 7, 9]
Example 4:We can also decrement step by mentioning a negative number.
示例4:我们也可以通过提及负数来递减步骤。
range(10,1,-2)
range(10,1,-2)
It will generate a sequence of numbers from 10, decrement by 2, and stop at 1.
它将生成一个从10开始的数字序列,然后递减2并在1处停止。
Iterating through range object using for loop.
使用for循环遍历范围对象。
r=range(10,1,-2)
print (r)#Output:range(10, 1, -2)for i in r:
print (i)'''Output
10
8
6
4
2
'''
Example 5:
范例5:
r=range(0)
print (r)#Output:range(0,0)print (list(r))#Output:[]r1=range(2,2)
print (list(r1))#Output:[]
Example 6: step is given as 0. It will raise ValueError.
示例6:步骤为0。它将引发ValueError。
r=range(1,10,0)
print (r)#Output:ValueError: range() arg 3 must not be zero
Example 7: start, stop, and step can be negative numbers also.
示例7:开始,停止和步进也可以为负数。
r=range(-10,-20,-2)
print (list(r))#Output:[-10, -12, -14, -16, -18]
Example 8: start, stop, and step is given as variables a,b,c.
示例8:开始,停止和步进作为变量a,b,c给出。
a=1
b=5
c=2
r=range(a,b,c)
print (list(r))#Output:[1, 3]
Example 9: range() function doesn’t support float numbers.It will raise TypeError.
示例9:range()函数不支持浮点数,它将引发TypeError。
r=range(2.0,10.0,2)#Output:TypeError: 'float' object cannot be interpreted as an integer
范围对象的通用序列操作: (Common Sequence Operations on range object:)
- Membership test 会员资格测试
- Indexing 索引编制
- Slicing 切片
- len() len()
- min() min()
- max() max()
- index() 指数()
1.会员资格测试 (1. Membership Test)
Membership test can be done by using in and not in operator.
可以通过使用in和not in运算符来完成成员资格测试。
x in s-True if an item of s is equal to x, else False
X在S- True ,如果S的产品等于x,否则False
x not in s-False if an item of s is equal to x, else True
X不是S- False如果s的产品等于x,否则True
Example:
例:
a1=range(5)
print (3 in a1)#Output:Trueprint (5 not in a1)#Output:True
2.索引 (2.Indexing)
Indexing also supported in range objects.
范围对象也支持索引。
Indexing starts from 0. Index 0 represents the first element in the sequence.
索引从0开始。 索引0代表序列中的第一个元素。
Negative indexing starts from -1. Index -1 represents the last element in the sequence.
负索引从-1开始。 索引-1表示序列中的最后一个元素。
r=range(2,10,2)
print (r[0])#Output:2print (r[3])#Output:8print (r[-1])#Output:8
IndexError (IndexError)
Attempting to use an index that is too large will result in an IndexError.
尝试使用太大的索引将导致IndexError。
r=range(2,10,2)
print (r[4])#Output:IndexError: range object index out of range
3.切片: (3. Slicing:)
Slicing is supported by the range object.
范围对象支持切片。
Refer to my story of Indexing and Slicing.
In slicing, we can specify a range of indexes.
在切片中,我们可以指定索引范围。
s[i:j:k] — slice of s from i to j with step k
Example: s[1:3] — Returns element from the first index to the third index(excluded).
示例: s[1:3] —从第一个索引到第三个索引(不包括)返回元素。
r=range(2,10,2)
print (list(r[1:3]))#Output:[4, 6]
4.len() (4.len())
Returns the number of elements in the range object.
返回范围对象中的元素数。
Example:
例:
r=range(2,10,2)
print (len(r))#Output:4
5分钟() (5.min())
Returns the smallest element in the range object.
返回范围对象中的最小元素。
r=range(2,10,2)
print (min(r))
#Output:2
r1=range(-10,-20,-2)
print(min(r1))#Output:-18
6.max() (6.max())
Returns the largest element in the range object.
返回范围对象中的最大元素。
r=range(2,10,2)
print (max(r))#Output:8r1=range(-10,-20,-2)
print(max(r1))#Output:-10
7.index() (7.index())
Returns the index of the specified element in the range object. If the element is not in the range object means, it will raise a ValueError.
返回范围对象中指定元素的索引。 如果该元素不在范围对象范围内,则将引发ValueError。
r=range(2,10,2)
print (r.index(4))#Output:1print (r.index(10))#Output:ValueError: 10 is not in range
串联和重复运算符: (Concatenation and repetition operator:)
The concatenation and repetition operator is not supported in the range object.
范围对象不支持串联和重复运算符。
Range object only supports item sequences that follow specific patterns, and hence don’t support sequence concatenation or repetition.
范围对象仅支持遵循特定模式的项目序列,因此不支持序列串联或重复 。
Concatenation is not supported in range objects.
范围对象不支持串联 。
r1=range(5)
r2=range(10)
print (r1+r2)#Output:TypeError: unsupported operand type(s) for +: 'range' and 'range'
The repetition operator is not supported in the range object.
范围对象不支持重复运算符。
r1=range(5)
print (r1*2)#Output:TypeError: unsupported operand type(s) for *: 'range' and 'int'
通过使用itertools.chain()进行串联 (Concatenation by using itertools.chain())
Python doesn’t have a built-in function to concatenate two or more range objects. We can achieve this by using itertools.chain().
Python没有内置函数来连接两个或多个范围对象。 我们可以通过使用itertools.chain()实现此目的。
chain():Makes an iterator that returns an element from the first iterable until its exhausted, then proceeds to the next iterable. It will treat consecutive sequences as a single sequence.itertools.chain(*iterables)
chain():使迭代器从第一个可迭代的元素返回直到元素用尽,然后继续进行下一个可迭代的元素。 它将连续序列视为单个序列。 itertools.chain(*iterables)
Example:
例:
import itertools
r=range(2,10,2)
r1=range(-10,-20,-2)
r2=itertools.chain(r,r1)
print (list(r2))#Output:[2, 4, 6, 8, -10, -12, -14, -16, -18]
范围功能的优点: (Advantage of range function:)
The advantage of the range type over a regular list or tuple is that a range object will always take the same (small) amount of memory, no matter the size of the range it represents (as it only stores the
start,stopandstepvalues).与常规列表或元组相比, 范围类型的优势在于,范围对象将始终占用相同(少量)的内存,无论其表示的范围大小如何(因为它仅存储
start,stop和step值)。
结论: (Conclusion:)
The range function supports only integers. (either built-in
intor any object that implements the__index__special method)范围函数仅支持整数。 (内置
int或实现__index__特殊方法的任何对象)If the step is 0, it will raise ValueError.
如果步长为0,则将引发ValueError。
The range function doesn’t support concatenation and repetition.
范围函数不支持串联和重复 。
Python doesn’t have a built-in function to concatenate two or more range objects. We can achieve this by using itertools.chain().
Python没有内置函数来连接两个或多个范围对象。 我们可以通过使用itertools.chain()实现此目的。
翻译自: https://medium.com/dev-genius/an-introduction-to-the-python-range-function-8dc8047161ef
range函数python




