DEV Community

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

Posted 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 keyword respectively.
print(bool((0,)))  # tuple
print(bool(((),))) # tuple(Empty tuple)
# True

print(bool(())) # Empty tuple
# False
Enter fullscreen mode Exit fullscreen mode
print(not (0,))  # tuple
print(not ((),)) # tuple(Empty tuple)
# False

print(not ()) # Empty tuple
# 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 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
Enter fullscreen mode Exit fullscreen mode
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
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 keyword and with is and not keyword respectively as shown below:

*Memo:

  • Be careful, tuple literals with is keyword and with is and not keyword 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
# <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 "!="?
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 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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode

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

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)
Enter fullscreen mode Exit fullscreen mode
v = ('A', 'B', 'C', 'E', 'D') * 3

print(v)
# ('A', 'B', 'C', 'E', 'D', 'A', 'B', 'C', 'E', 'D', 'A', 'B', 'C', 'E', 'D')
Enter fullscreen mode Exit fullscreen mode
v = () * 3

print(v)
# ()
Enter fullscreen mode Exit fullscreen mode

<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))
Enter fullscreen mode Exit fullscreen mode
v = (('A', 'B', 'C', 'D', 'E'),) * 3

print(v)
# (('A', 'B', 'C', 'D', 'E'),
#  ('A', 'B', 'C', 'D', 'E'),
#  ('A', 'B', 'C', 'D', 'E'))
Enter fullscreen mode Exit fullscreen mode
v = ((),) * 3

print(v)
# ((), (), ())
Enter fullscreen mode Exit fullscreen mode

<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),))
Enter fullscreen mode Exit fullscreen mode
v = ((('A', 'B', 'C', 'D', 'E'),),) * 3

print(v)
# ((('A', 'B', 'C', 'D', 'E'),),
#  (('A', 'B', 'C', 'D', 'E'),),
#  (('A', 'B', 'C', 'D', 'E'),))
Enter fullscreen mode Exit fullscreen mode
v = (((),),) * 3

print(v)
# (((),), ((),), ((),))
Enter fullscreen mode Exit fullscreen mode

Top comments (0)