0% found this document useful (0 votes)
17 views4 pages

Session1 3 (Tuples)

The document discusses tuples in Python. Tuples are similar to lists but are immutable. The document demonstrates how to create, access, slice, iterate and perform other operations on tuples. It also discusses advantages of tuples such as performance and use as dictionary keys.

Uploaded by

srinuleone
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

Session1 3 (Tuples)

The document discusses tuples in Python. Tuples are similar to lists but are immutable. The document demonstrates how to create, access, slice, iterate and perform other operations on tuples. It also discusses advantages of tuples such as performance and use as dictionary keys.

Uploaded by

srinuleone
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

5/6/22, 9:54 AM 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)

# Tuple having integers


my_tuple = (1, 2, 3,3,3)
print(my_tuple)

# tuple with mixed datatypes


my_tuple = (1, "Hello", 3.4,1+3j)
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)

# tuple unpacking is also possible


a, b, c = my_tuple

print(a)
print(b)
print(c)

(3, 4.6, 'dog')


3
4.6
dog

In [23]:
my_tuple = ("hello")
print(type(my_tuple)) # <class 'str'>

# Creating a tuple having one element


my_tuple = ("hello",)
print(type(my_tuple)) # <class 'tuple'>

# Parentheses is optional

file:///C:/Users/GVPCOE/Desktop/Python sessions/session1_3(Tuples).html 1/4


5/6/22, 9:54 AM session1_3(Tuples)
my_tuple = "hello",
print(type(my_tuple)) # <class 'tuple'>

<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])

IndexError: tuple index out of range

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])

TypeError: tuple indices must be integers or slices, not float

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')

file:///C:/Users/GVPCOE/Desktop/Python sessions/session1_3(Tuples).html 2/4


5/6/22, 9:54 AM session1_3(Tuples)

# elements 2nd to 4th


print(my_tuple[1:4])

# elements beginning to 2nd


print(my_tuple[:-7])

# elements 8th to end


print(my_tuple[7:])

# elements beginning to end


print(my_tuple[:])

('r', 'o', 'g')


('p', 'r')
('i', 'z')
('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')

Tuples are immutable


In [37]:
# Changing tuple values
my_tuple = (4, 2, 3, [6, 5])
my_tuple[1] = 9

---------------------------------------------------------------------------
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

TypeError: 'tuple' object does not support item assignment

In [38]:
# However, item of mutable element can be changed
my_tuple[3][0] = 9
print(my_tuple)

(4, 2, 3, [9, 5])

In [40]:
# Tuples can be reassigned
my_tuple = ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')
print(my_tuple)

('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')

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

Advantages of Tuple over List


Since tuples are quite similar to lists, both of them are used in similar situations. However, there are
certain advantages of implementing a tuple over a list. Below listed are some of the main
advantages:

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 [ ]:

file:///C:/Users/GVPCOE/Desktop/Python sessions/session1_3(Tuples).html 4/4

You might also like