*Memo:
A range can be used with len() to get the length as shown below:
v = range(5)
print(*v)
# 0 1 2 3 4
print(len(v))
# 5
A non-empty range and empty range are:
-
TrueandFalse, checking them with bool() respectively. -
FalseandTrue, inverting their truth values withnotkeyword respectively.
# Non-empty range
print(bool(range(1)))
# True
# Empty range
print(bool(range(0)))
# False
# Non-empty range
print(not range(1))
# False
# Empty range
print(not range(0))
# True
A range can be checked if a specific number is and isn't in the range with in keyword and with not and in keyword respectively as shown below:
v = range(3)
print(*v)
# 0 1 2
print(0 in v)
print(1 in v)
print(2 in v)
# True
print(3 in v)
print(range(3) in v)
# False
v = range(3)
print(*v)
# 0 1 2
print(0 not in v)
print(1 not in v)
print(2 not in v)
# False
print(3 not in v)
print(range(3) not in v)
# True
A range can be checked if the range is and isn't referred to by two variables with is keyword and with is and not keyword respectively as shown below:
v1 = range(3)
v2 = range(3)
v3 = v1
print(v1 is v2) # False
print(v1 is v3) # True
print(v1 is not v2) # True
print(v1 is not v3) # False
A range and other range can be checked if all the integers in them are and aren't the same with == and != respectively as shown below:
v = range(3)
print(v == range(3)) # 0 1 2 # True
print(v == range(2, -1, -1)) # 2 1 0 # False
print(v == range(0, 5, 2)) # 0 2 4 # False
print(v == range(0, -5, -2)) # 0 -2 -4 # False
print(v == range(2)) # 0 1 # False
print(v == range(0, 5, 4)) # 0 4 # False
print(v == range(0, -5, -4)) # 0 -4 # False
print(v == range(4)) # 0 1 2 3 # False
print(v == range(0, 7, 2)) # 0 2 4 6 # False
print(v == range(0, -7, -2)) # 0 -2 -4 -6 # False
print(v == range(0)) # Nothing # False
v = range(3)
print(v != range(3)) # 0 1 2 # False
print(v != range(2, -1, -1)) # 2 1 0 # True
print(v != range(0, 5, 2)) # 0 2 4 # True
print(v != range(0, -5, -2)) # 0 -2 -4 # True
print(v != range(2)) # 0 1 # True
print(v == range(0, 5, 4)) # 0 4 # True
print(v == range(0, -5, -4)) # 0 -4 # True
print(v != range(4)) # 0 1 2 3 # True
print(v != range(0, 7, 2)) # 0 2 4 6 # True
print(v != range(0, -7, -2)) # 0 -2 -4 -6 # True
print(v != range(0)) # Nothing # True
A range and other range cannot be checked if:
- the range is greater than other range with
>. - the range is greater than or equal to other range with
>=. - the range is less than other range with
<. - the range is less than or equal to other range with
<=.
v = range(3)
print(v > range(3))
# TypeError: '>' not supported between instances of 'range' and 'range'
print(v >= range(3))
# TypeError: '>=' not supported between instances of 'range' and 'range'
print(v < range(3))
# TypeError: '<' not supported between instances of 'range' and 'range'
print(v <= range(3))
# TypeError: '<=' not supported between instances of 'range' and 'range'
A range and other range cannot be checked if they have and don't have their common integers with bool() and & and with not keyword and & respectively as shown below:
v = range(3)
print(bool(v & range(1, 4, 2)))
print(not (v & range(1, 4, 2)))
# TypeError: unsupported operand type(s) for &: 'range' and 'range'
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'
Top comments (0)