*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 withnotrespectively.
# Non-empty tuple print(bool((0,))) print(bool(((),))) # True # Empty tuple print(bool(())) # False # Non-empty tuple print(not (0,)) print(not ((),)) # False # Empty tuple print(not ()) # True A tuple can be checked if a specific element is and isn't in the tuple with in and not in respectively as shown below:
v = ('A', ('B', 'C')) print('A' in v) print(('B', 'C') in v) # True print('B' in v) print('C' in v) print(('A',) in v) print(('B',) in v) print(('C',) in v) print(('A', ('B', 'C')) in v) print(() in v) # False v = ('A', ('B', 'C')) print('A' not in v) print(('B', 'C') not in v) # False print('B' not in v) print('C' not in v) print(('A',) not in v) print(('B',) not in v) print(('C',) not in v) print(('A', ('B', 'C')) not in v) print(() not in v) # True A tuple can be checked if the tuple is and isn't referred to by two variables with is and is not respectively as shown below:
*Memo:
- Be careful, tuple literals with
isandis notget 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 # 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 # 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:
- all the elements in them are and aren't equal with
==and!=respectively:-
==and!=can also check if types of values are and aren't the same respectively.
-
- the tuple is greater than other tuple with
>. - the tuple is greater than or equal to other tuple with
>=. - the tuple is less than other tuple with
<. - the tuple is less than or equal to other tuple with
<=.
*Memo:
- Lexicographical comparison is used with their tuples:
- Lexicographical comparison:
- compares each element in their tuples as numbers from their 1st elements one by one:
- To compare the characters in two strings or the bytes in two bytes objects or two bytearrays, their Unicode points are used.
- finishes the comparison:
- just after the current comparison which has a difference between two elements in their tuples is done.
- or just after the current comparison of the element and absent element and vice versa in their tuples is done:
- An absent element is evaluated to a lower value than the other.
- or if both of their tuples are exhausted.
- compares each element in their tuples as numbers from their 1st elements one by one:
- E.g.
(0, 1, 2) > (0, 1, 3)isFalse, comparing2 > 3. - E.g.
(0, 1, 2) > (0, 1)isTrue, comparing2 > an absent element(lower value than the other). - E.g.
(0, 1, 2) > (0, 3)isFalse, comparing1 > 3.
- Lexicographical comparison:
v = (0, 1, 2) print(v == (0, 1, 2)) # True print(v == (2, 1, 0)) # False print(v == (0, 1, 3)) # False print(v == (0, 1, -3)) # False print(v == (0, 1)) # False print(v == (0, 3)) # False print(v == (0, -3)) # False print(v == (0, 1, 2, 3)) # False print(v == (0, 1, 3, 4)) # False print(v == (0, 1, -3, -4)) # False print(v == ()) # False # list print(v == [0, 1, 2]) # False v = (0, 1, 2) print(v != (0, 1, 2)) # False print(v != (2, 1, 0)) # True print(v != (0, 1, 3)) # True print(v != (0, 1, -3)) # True print(v != (0, 1)) # True print(v != (0, 3)) # True print(v != (0, -3)) # True print(v != (0, 1, 2, 3)) # True print(v != (0, 1, 3, 4)) # True print(v != (0, 1, -3, -4)) # True print(v != ()) # True # list print(v != [0, 1, 2]) # True v = (0, 1, 2) print(v > (0, 1, 2)) # False print(v > (2, 1, 0)) # False print(v > (0, 1, 3)) # False print(v > (0, 1, -3)) # True print(v > (0, 1)) # True print(v > (0, 3)) # False print(v > (0, -3)) # True print(v > (0, 1, 2, 3)) # False print(v > (0, 1, 3, 4)) # False print(v > (0, 1, -3, -4)) # True print(v > ()) # True v = (0, 1, 2) print(v >= (0, 1, 2)) # True print(v >= (2, 1, 0)) # False print(v >= (0, 1, 3)) # False print(v >= (0, 1, -3)) # True print(v >= (0, 1)) # True print(v >= (0, 3)) # False print(v >= (0, -3)) # True print(v >= (0, 1, 2, 3)) # False print(v >= (0, 1, 3, 4)) # False print(v >= (0, 1, -3, -4)) # True print(v >= ()) # True v = (0, 1, 2) print(v < (0, 1, 2)) # False print(v < (2, 1, 0)) # True print(v < (0, 1, 3)) # True print(v < (0, 1, -3)) # False print(v < (0, 1)) # False print(v < (0, 3)) # True print(v < (0, -3)) # False print(v < (0, 1, 2, 3)) # True print(v < (0, 1, 3, 4)) # True print(v < (0, 1, -3, -4)) # False print(v < ()) # False v = (0, 1, 2) print(v <= (0, 1, 2)) # True print(v <= (2, 1, 0)) # True print(v <= (0, 1, 3)) # True print(v <= (0, 1, -3)) # False print(v <= (0, 1)) # False print(v <= (0, 3)) # True print(v <= (0, -3)) # False print(v <= (0, 1, 2, 3)) # True print(v <= (0, 1, 3, 4)) # True print(v <= (0, 1, -3, -4)) # False print(v <= ()) # False # Equivalent print(('0', '1', '2') == ('0', '1', '2')) # True print(('012') == ('012')) # True print(('01', '2') == ('01', '2')) # True print(('0', '12') == ('0', '12')) # True print((48, 49, 50) == (48, 49, 50)) # True print(('0', '1', '2') == (48, 49, 50)) # False # Equivalent print(('01', '2') > ('0', '2')) # True print((('0', '1'), '2') > (('0',), '2')) # True print(((48, 49), 50) > ((48,), 50)) # True print((('0', '1'), '2') > ((48,), 50)) # TypeError: '>' not supported between instances of 'str' and 'int' A tuple and other tuple cannot be checked if they have and don't have their common elements with bool() and & and with not and & respectively as shown below:
v = (0, 1, 2) print(bool(v & (1, 3))) print(not (v & (1, 3))) # TypeError: unsupported operand type(s) for &: 'tuple' and 'tuple'
Top comments (0)