You are on page 1of 2

06-Tuples

September 22, 2021

1 Tuples
A tuple is created by placing all the items (elements) inside parentheses () separated by commas.
It can have any number of items and they may be of different types (integer, float, string etc.)
# empty tuple
my_tuple = ()
# tuple of integers
my_tuple = (5, 9, 3)
# tuple with mixed data types
my_tuple = (1, "Hi!", 3.4j)
Create an empty tuple language then assign to it (“Swahili”, “Arabic”, “English”,
“Swazi”,“French”, “Egede”)
In [2]: t = ("Swahili", "Arabic", "English", "Swazi","French", "Egede")

1.1 Access the first element


In [3]: print(t[0])
Swahili

What do you think will be the output of print(t[-5])


In [4]: print(t[-5])
Arabic

1.2 nested index


What do you think will be the output of print(t[4][3])
In [5]: print(t[4][3])
n

Tuples are similar to lists except you cannot change elements of a tuple once it is defined.
Whereas in a list, items can be modified. We will then say that a list is mutable whereas a tuple is
immutable

1
1.3 More operations
dir(tuple) or dir(t) where t is a tuple!

You might also like