Intro

In Python, tuples are a built-in container type used to store fixed collections of heterogeneous data.

Creating Tuples

  • Tuple literals
tuple1 = ("Alice", 30, True)
print(tuple1)
 
# Single element tuple
tuple2 = (5,)   
print(tuple2)
 
not_tuple = (5) # Just an int
print(type(not_tuple))
('Alice', 30, True)
(5,)
<class 'int'>
  • Tuple constructor: tuple(iterable)
lst = [1, 2, 3]
t = tuple(lst)
print(t)  # Output: (1, 2, 3)
 
t2 = tuple("hello")
print(t2) # Output: ('h', 'e', 'l', 'l', 'o')
(1, 2, 3)
('h', 'e', 'l', 'l', 'o')