Tuple#

Overview#

A tuple is an ordered collection of elements enclosed in parentheses (). Tuples are only different of lists because they are immutable (i.e., their elements cannot be modified after creation) so the elements in tuple remain in fixed order.

Note

General syntax

my_tuple = (element1, element2, element3, ..., elementN)
  • my_tuple: Tuple variable name

  • element1, element2, element3, …, elementN:

  • The elements of the tuple are enclosed in parentheses () and separated by commas

  • Tuples are commonly used in scenarios where you want to ensure that the data remains unchanged, such as historical records

Examples#

my_tuple = (1, 2, 3, "apple", "banana")
print(my_tuple)
(1, 2, 3, 'apple', 'banana')
  • Concatenate tuples or create new tuples by slicing existing ones.

print(my_tuple[0])  # 1
print(my_tuple[3])  # "apple"
1
apple