*Memo:
- My post explains a tuple (1).
- My post explains a tuple (3).
- My post explains a tuple (4).
- My post explains a tuple (5).
- My post explains a tuple (6).
A non-empty tuple and empty tuple are:
-
TrueandFalse, checking them with bool() respectively. -
FalseandTrue, inverting their truth values withnotkeyword respectively.
print(bool((0,))) # tuple
print(bool(((),))) # tuple(Empty tuple)
# True
print(bool(())) # Empty tuple
# False
print(not (0,)) # tuple
print(not ((),)) # tuple(Empty tuple)
# False
print(not ()) # Empty tuple
# True
A tuple can be checked if a specific element is and isn't in the tuple with in keyword and with not and in keyword respectively as shown below:
v = ('A', ('B', 'C'))
print('A' in v)
print(('B', 'C') in v)
# True
print('a' in v)
print('B' in v)
print('C' in v)
print(('B',) in v)
print(('C',) in v)
print(('A', ('B', 'C')) in v)
# False
v = ('A', ('B', 'C'))
print('A' not in v)
print(('B', 'C') not in v)
# False
print('a' not in v)
print('B' not in v)
print('C' not in v)
print(('B') not in v)
print(('C') not in v)
print(('A', ('B', 'C')) not in v)
# True
A tuple can be checked if the tuple is and isn't referred to by two variables with is keyword and with is and not keyword respectively as shown below:
*Memo:
- Be careful, tuple literals with
iskeyword and withisandnotkeyword get warnings so use==and!=respectively.
v1 = (0, 1, 2)
v2 = (0, 1, 2)
v3 = v1
print(v1 is v2) # False
print(v1 is v3) # True
print(v1 is not v2) # True
print(v1 is not v3) # False
print((0, 1, 2) is (0, 1, 2)) # True
print((0, 1, 2) is (0, 1)) # False
# <exec>:1: SyntaxWarning: "is" with 'tuple' literal. Did you mean "=="?
print((0, 1, 2) is not (0, 1, 2)) # False
print((0, 1, 2) is not (0, 1)) # True
# <exec>:1: SyntaxWarning: "is not" with 'tuple' literal. Did you mean "!="?
print((0, 1, 2) == (0, 1, 2)) # True
print((0, 1, 2) == (0, 1)) # False
print((0, 1, 2) != (0, 1, 2)) # False
print((0, 1, 2) != (0, 1)) # True
A tuple and other tuple can be checked if every element in:
- them is and isn't the same with
==and!=respectively. - the tuple is in other tuple with
<=. - other tuple is in the tuple with
>=. - the tuple and other elements are in other tuple with
<. - other tuple and other elements are in the tuple with
>.
v = (0, 1, 2)
print(v == (0, 1, 2)) # True
print(v == (0, 1)) # False
print(v == (0, 3)) # False
print(v == (0, 1, 2, 3)) # False
print(v == (0, 1, 3, 4)) # False
print(v == ()) # False
v = (0, 1, 2)
print(v != (0, 1, 2)) # False
print(v != (0, 1)) # True
print(v != (0, 3)) # True
print(v != (0, 1, 2, 3)) # True
print(v != (0, 1, 3, 4)) # True
print(v != ()) # True
v = (0, 1, 2)
print(v <= (0, 1, 2)) # True
print(v <= (0, 1)) # False
print(v <= (0, 3)) # True
print(v <= (0, 1, 2, 3)) # True
print(v <= (0, 1, 3, 4)) # True
print(v <= ()) # False
v = (0, 1, 2)
print(v >= (0, 1, 2)) # True
print(v >= (0, 1)) # True
print(v >= (0, 3)) # False
print(v >= (0, 1, 2, 3)) # False
print(v >= (0, 1, 3, 4)) # False
print(v >= ()) # True
v = (0, 1, 2)
print(v < (0, 1, 2)) # False
print(v < (0, 1)) # False
print(v < (0, 3)) # True
print(v < (0, 1, 2, 3)) # True
print(v < (0, 1, 3, 4)) # True
print(v < ()) # False
v = (0, 1, 2)
print(v > (0, 1, 2)) # False
print(v > (0, 1)) # True
print(v > (0, 3)) # False
print(v > (0, 1, 2, 3)) # False
print(v > (0, 1, 3, 4)) # False
print(v > ()) # True
A tuple and other tuple cannot be checked if they have and don't have common elements with bool() and & and with not keyword and & respectively as shown below:
v = (0, 1, 2)
print(bool(v & (3, 4)))
print(not (v & (3, 4)))
# TypeError: unsupported operand type(s) for &: 'tuple' and 'tuple'
A tuple can be enlarged with * and a number as shown below:
<1D tuple>:
v = (0, 1, 2, 3, 4) * 3
print(v)
# (0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4)
v = ('A', 'B', 'C', 'E', 'D') * 3
print(v)
# ('A', 'B', 'C', 'E', 'D', 'A', 'B', 'C', 'E', 'D', 'A', 'B', 'C', 'E', 'D')
v = () * 3
print(v)
# ()
<2D tuple>:
v = ((0, 1, 2, 3, 4),) * 3
print(v)
# ((0, 1, 2, 3, 4), (0, 1, 2, 3, 4), (0, 1, 2, 3, 4))
v = (('A', 'B', 'C', 'D', 'E'),) * 3
print(v)
# (('A', 'B', 'C', 'D', 'E'),
# ('A', 'B', 'C', 'D', 'E'),
# ('A', 'B', 'C', 'D', 'E'))
v = ((),) * 3
print(v)
# ((), (), ())
<3D tuple>:
v = (((0, 1, 2, 3, 4),),) * 3
print(v)
# (((0, 1, 2, 3, 4),), ((0, 1, 2, 3, 4),), ((0, 1, 2, 3, 4),))
v = ((('A', 'B', 'C', 'D', 'E'),),) * 3
print(v)
# ((('A', 'B', 'C', 'D', 'E'),),
# (('A', 'B', 'C', 'D', 'E'),),
# (('A', 'B', 'C', 'D', 'E'),))
v = (((),),) * 3
print(v)
# (((),), ((),), ((),))
Top comments (0)