Session1 3 (Tuples)
Session1 3 (Tuples)
A tuple in Python is similar to a list. The difference between the two is that we cannot change the
elements of a tuple once it is assigned whereas we can change the elements of a list.
A tuple is created by placing all the items (elements) inside parentheses (), separated by commas.
The parentheses are optional, however, it is a good practice to use them.
A tuple can have any number of items and they may be of different types (integer, float, list, string,
etc.).
In [46]:
# Different types of tuples
# Empty tuple
my_tuple = ()
print(my_tuple)
# nested tuple
my_tuple = ("mouse", [8, 4, 6], (1, 2, 3))
print(my_tuple)
()
(1, 2, 3, 3, 3)
(1, 'Hello', 3.4, (1+3j))
('mouse', [8, 4, 6], (1, 2, 3))
In [21]:
my_tuple = 3, 4.6, "dog"
print(my_tuple)
print(a)
print(b)
print(c)
In [23]:
my_tuple = ("hello")
print(type(my_tuple)) # <class 'str'>
# Parentheses is optional
<class 'str'>
<class 'tuple'>
<class 'tuple'>
In [26]:
# Accessing tuple elements using indexing
my_tuple = ('p','e','r','m','i','t')
print(my_tuple[0])
print(my_tuple[5])
p
t
In [27]:
print(my_tuple[6])
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_13348/2917517823.py in <module>
----> 1 print(my_tuple[6])
In [29]:
print(my_tuple[2.0])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_13348/691639436.py in <module>
----> 1 print(my_tuple[2.0])
In [31]:
# nested tuple
n_tuple = ("mouse", [8, 4, 6], (1, 2, 3))
# nested index
print(n_tuple[0][3])
print(n_tuple[1][1])
s
4
In [34]:
# Negative indexing for accessing tuple elements
my_tuple = ('p', 'e', 'r', 'm', 'i', 't')
print(my_tuple[-1])
print(my_tuple[-6])
t
p
In [35]:
# Accessing tuple elements using slicing
my_tuple = ('p','r','o','g','r','a','m','i','z')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_13348/913935715.py in <module>
1 # Changing tuple values
2 my_tuple = (4, 2, 3, [6, 5])
----> 3 my_tuple[1] = 9
In [38]:
# However, item of mutable element can be changed
my_tuple[3][0] = 9
print(my_tuple)
In [40]:
# Tuples can be reassigned
my_tuple = ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')
print(my_tuple)
In [41]:
# Concatenation
print((1, 2, 3) + (4, 5, 6))
# Repeat
print(("Repeat",) * 3)
(1, 2, 3, 4, 5, 6)
('Repeat', 'Repeat', 'Repeat')
In [42]:
my_tuple = ('a', 'p', 'p', 'l', 'e',)
file:///C:/Users/GVPCOE/Desktop/Python sessions/session1_3(Tuples).html 3/4
5/6/22, 9:54 AM session1_3(Tuples)
print(my_tuple.count('p'))
print(my_tuple.index('l'))
2
3
In [43]:
# Membership test in tuple
my_tuple = ('a', 'p', 'p', 'l', 'e',)
# In operation
print('a' in my_tuple)
print('b' in my_tuple)
# Not in operation
print('g' not in my_tuple)
True
False
True
In [44]:
# Using a for loop to iterate through a tuple
for name in ('John', 'Kate'):
print("Hello", name)
Hello John
Hello Kate
We generally use tuples for heterogeneous (different) data types and lists for
homogeneous (similar) data types.
Since tuples are immutable, iterating through a tuple is faster than with list. So there
is a slight performance boost.
Tuples that contain immutable elements can be used as a key for a dictionary. With
lists, this is not possible.
If you have data that doesn't change, implementing it as tuple will guarantee that it
remains write-protected.
In [ ]: