*Memo:
A range cannot be enlarged with * and a number as shown below:
v = range(5) * 3
# TypeError: unsupported operand type(s) for *: 'range' and 'int'
A range and other ranges cannot be concatenated with + as shown below:
v = range(3) + range(3, 5) + range(5, 9)
# TypeError: unsupported operand type(s) for +: 'range' and 'range'
A range and other range cannot return:
- all the integers in them with
'|'(Union: A ∪ B). - their common integers with
'&'(Intersection: A ∩ B). - the integers in the range which aren't in other range with
'-'(Difference: A - B). - the integers in either the range or other range but not both with
'^'(Symmetric Difference: A Δ B).
v = range(0, 5, 4) | range(0, 5, 2)
# TypeError: unsupported operand type(s) for |: 'range' and 'range'
v = range(4) & range(0, 5, 2)
# TypeError: unsupported operand type(s) for &: 'range' and 'range'
v = range(4) - range(0, 5, 2)
# TypeError: unsupported operand type(s) for -: 'range' and 'range'
v = range(4) ^ range(0, 5, 2)
# TypeError: unsupported operand type(s) for ^: 'range' and 'range'
A range can be iterated with a for statement as shown below:
for x in range(5):
print(x)
# 0
# 1
# 2
# 3
# 4
A range can be unpacked with an assignment and for statement, function and * but not with ** as shown below:
v1, v2, v3 = range(3)
print(v1, v2, v3)
# 0 1 2
v1, *v2, v3 = range(6)
print(v1, v2, v3) # 0 [1, 2, 3, 4] 5
print(v1, *v2, v3) # 0 1 2 3 4 5
for v1, v2, v3 in [range(3), range(3, 6)]:
print(v1, v2, v3)
# 0 1 2
# 3 4 5
for v1, *v2, v3 in [range(6), range(6, 12)]:
print(v1, v2, v3)
print(v1, *v2, v3)
# 0 [1, 2, 3, 4] 5
# 0 1 2 3 4 5
# 6 [7, 8, 9, 10] 11
# 6 7 8 9 10 11
print(*range(4), *range(4, 6))
# 0 1 2 3 4 5
print([*range(4), *range(4, 6)])
# [0, 1, 2, 3, 4, 5]
def func(p1='a', p2='b', p3='c', p4='d', p5='e', p6='f'):
print(p1, p2, p3, p4, p5, p6)
func()
# a b c d e f
func(*range(4), *range(4, 6))
# 0 1 2 3 4 5
def func(p1='a', p2='b', *args):
print(p1, p2, args)
print(p1, p2, *args)
print(p1, p2, ['A', 'B', *args, 'C', 'D'])
func()
# a b ()
# a b Nothing
# a b ['A', 'B', 'C', 'D']
func(*range(4), *range(4, 6))
# 0 1 (2, 3, 4, 5)
# 0 1 2 3 4 5
# 0 1 ['A', 'B', 2, 3, 4, 5, 'C', 'D']
Even a big range doesn't get MemoryError as shown below:
v = range(100000000)
print(v)
# range(0, 100000000)
print(v[0], v[1], v[2])
# 0 1 2
A range can be read indexing and slicing as shown below:
*Memo:
- Indexing can be done with one or more
[index]in the range[The 1st index, The last index]:-
index(Required-Type:int):- Don't use
index=.
- Don't use
-
indexcan be signed indices(zero and positive and negative indices). - Error occurs if
indexis out of range. -
[slice][index]can be used.
-
v = range(8)
print(v, v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7])
print(v, v[-8], v[-7], v[-6], v[-5], v[-4], v[-3], v[-2], v[-1])
print(v, v[0:1][0], v[1:2][0], v[2:3][0], v[3:4][0],
v[4:5][0], v[5:6][0], v[6:7][0], v[7:8][0])
print(v, v[-8:-7][0], v[-7:-6][0], v[-6:-5][0], v[-5:-4][0],
v[-4:-3][0], v[-3:-2][0], v[-2:-1][0], v[-1:10][0])
# range(0, 8) 0 1 2 3 4 5 6 7
Top comments (0)