DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

Tuple in Python (2)

Buy Me a Coffee

*Memo:

A non-empty tuple and empty tuple are:

  • True and False, checking them with bool() respectively.
  • False and True, inverting their truth values with not respectively.
# Non-empty tuple print(bool((0,))) print(bool(((),))) # True  # Empty tuple print(bool(())) # False 
Enter fullscreen mode Exit fullscreen mode
# Non-empty tuple print(not (0,)) print(not ((),)) # False  # Empty tuple print(not ()) # True 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode
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 
Enter fullscreen mode Exit fullscreen mode

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 is and is not 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 
Enter fullscreen mode Exit fullscreen mode
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 "!="? 
Enter fullscreen mode Exit fullscreen mode
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 
Enter fullscreen mode Exit fullscreen mode

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.
    • E.g. (0, 1, 2) > (0, 1, 3) is False, comparing 2 > 3.
    • E.g. (0, 1, 2) > (0, 1) is True, comparing 2 > an absent element(lower value than the other).
    • E.g. (0, 1, 2) > (0, 3) is False, comparing 1 > 3.
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 
Enter fullscreen mode Exit fullscreen mode
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 
Enter fullscreen mode Exit fullscreen mode
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 
Enter fullscreen mode Exit fullscreen mode
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 
Enter fullscreen mode Exit fullscreen mode
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 
Enter fullscreen mode Exit fullscreen mode
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 
Enter fullscreen mode Exit fullscreen mode
# 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 
Enter fullscreen mode Exit fullscreen mode
# 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' 
Enter fullscreen mode Exit fullscreen mode

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' 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)