0% found this document useful (0 votes)
24 views18 pages

L6 Tuple Container

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)
24 views18 pages

L6 Tuple Container

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

ED5340 - Data Science: Theory at h y

and Practise a p
gan
th u
M u
a n
th
L5 - Tuple container m a n a
R a

Ramanathan Muthuganapathy (https://ed.iitm.ac.in/~raman)


Course web page: https://ed.iitm.ac.in/~raman/datascience.html
Moodle page: Available at https://courses.iitm.ac.in/
Tuple representation

• Tuples is a collection of heterogeneous data types enclosed in C brackets


h y
at
• empty_tup = tup( ) #empty tuple an
a p
u g
uth
• tup1 = (10, ) # Note that you need
th a n a comma
M
n a
m a
• tup2 = (‘Ram’, 20, 33.333) R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Tuple basics and accessing

• Elements in the tuple can be repeated.


h y
at
• It is also a sequential collection - can be indexed
a na p and sliced
u g
uth
• Entire tuple or each element can be
th a nprinted.
M
n a
a
m
• Tuple is an `iterable’ i.e. you a
can
R iterate over its elements.

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


y

Demo using
at h
a p
gan
th u
u

L6_tuple_ex_access.py
n M
th a
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


CW: Define a tuple of different datatypesna p and print
at h y

them using iterator. M u th u g a

a n
ath
a n
HW:In a tuple, find the number of objects of each type.
R a m

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Tuple operations

• Tuples are immutable (like strings).


h y
at
• Tuples can be concatenated an
a p
u g
uth
• searching (containment) and sorting
th a n M
n a
am
• deletion - using index or a
range
R of indices

• conversion / other functions - str to tuple, len, max, min, sum


• index and count (member functions)
• tuple comparison
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
y

Demo using
at h
a p
gan
th u
u

L6_tuple_operations.py
n M
th a
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


h y
at
HW: Find out the functions that are not a p
gan
th u
u

in tuples but available in lists and why?


n M
th a
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Some points on Tuples

• Tuple is an iterable.
h y
at
• Tuple is like a structure in C. an
a p
u g
uth
• No tuple comprehension (why?) th a n M
n a
ma
• You can use list a
comprehension
R and then use ‘tuple’ function

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Tuple Varieties
Similar to List varieties

• Tuple of Tuples
h y
at
• Tuple embedding an
a p
u g
uth
• Tuple unpacking (using * operator)than M
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Grouping Tuples
Taking one each from each tuple

names = ('Ram', 'Raja', 'Geetha', 'Ramya')


h y
gender = ('male', 'male', 'female', ‘female') pat
a
gan
th u
u
You want to group the tuples to the following:
h a n M
a t
an
m a
('Ram', 'male'), ('Raja', 'male'), R('Geetha', 'female'), ('Ramya', 'female')

(names[0], gender[0]), (names[1], gender[1]), and so on

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Grouping Tuples
Taking one each from each tuple

fruits = ('apples', 'oranges', 'grapes', 'guava')


h y
num_kg = (2, 5, 3, 6) pat
a
gan
th u
u
cost_kg = (150, 80, 200, 170) a n M
ath
a n
a m
Group the tuples R

('apples', 2, 150), …….

(fruits[0], num_kg[0], cost_kg[0]), ………..

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


zip( ) function
grouping can be achieved by a zip( ) function

• Takes one or more iterables and groups them together


h y
at
• returns an iterator of tuples an
a p
u g
uth
• zip( ) function - a very important one
th a n M
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


zip( ) function
grouping tuples

names = ('Ram', 'Raja', 'Geetha', 'Ramya')


h y
gender = ('male', 'male', 'female', ‘female’) pat
a
gan
th u
#Sevaral ways in which zip( ) can be used M u
a n
th
a
ite = zip(names, gender) #zip returns mann
a iterator of tuples
Ra
print(*ite) #Remember that ite is like a pointer and hence * is needed to unpack

#You can also do the following

for ite in zip(names, gender):

print(*ite)

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Some nuances on iterator of zip
we will see more on iterators later!

('Ram', 'male'), ('Raja', 'male'), ('Geetha', 'female'), ('Ramya', 'female')


h y
pat
a
gan
th u
u
ite ite = ite->next ite h=anite->next
M ite = ite->next
a t
an
m
For printing Ra

*ite *ite *ite *ite

ite[0], ite[1] ite[0], ite[1] ite[0], ite[1] ite[0], ite[1]

Note: In Python, ite -> next is actually notated as ite.__next__( )

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


CW: Take three lists, one each for na p few names, their
at h y

ages and salaries and makeMutahu tuple out of the lists.


g a

a n
ath
a n
HW: WAP to print the transpose of a 3X5 matrix.
R a m

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Matrix problems using zip( )

mat = [[1,2,3], [4,5,6]] at h y


a p
gan
th u
zip(mat[0], mat[1]) will a
M
th
u
a n give three tuples
n
(1,4) (2,5) (3,6) (needs
R a m a
iterator to fetch them)
for ite in zip(mat[0], mat[1]):
print(ite) #prints each tuple
for ite in zip(*mat): zip(*mat) is same as
print(ite) #prints each tuple zip(mat[0], mat[1]
Matrix problems using zip( )
Inverse

mat = [[1,2,3], [4,5,6]] a t h y


a p
ite = zip(*mat) #gives M(1, u th u g
4),
a n
(2,5), (3, 6)
n
lst = list(ite) a m a n a th a

R
print(lst)

You might also like