0% found this document useful (0 votes)
19 views14 pages

Chapter - 7 Python Collective Tuples

The document explains tuples in Python, highlighting their immutability compared to lists, and how to create, access, and manipulate them. It covers operations such as indexing, slicing, packing, unpacking, and basic tuple operations, along with built-in methods. Additionally, it provides examples for creating tuples through constructors and performing linear searches.

Uploaded by

nakul18209
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)
19 views14 pages

Chapter - 7 Python Collective Tuples

The document explains tuples in Python, highlighting their immutability compared to lists, and how to create, access, and manipulate them. It covers operations such as indexing, slicing, packing, unpacking, and basic tuple operations, along with built-in methods. Additionally, it provides examples for creating tuples through constructors and performing linear searches.

Uploaded by

nakul18209
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/ 14

It is a sequence of immutable objects. It is just like list.

Difference between the tuples and the lists is that the tuples
cannot be changed unlike lists. Lists uses square bracket where
as tuples use parentheses.

Creating A Tuple
A tuple is enclosed in parentheses () for creation and each item is
separated by a comma.
e.g.
tup1 = (‘comp sc', ‘info practices', 2017, 2018)
tup2 = (5,11,22,44)

NOTE:- Indexing of tuple is just similar to indexing of list.


Accessing Values from Tuples/tuple slicing
Use the square brackets for slicing along with the
index or indices to obtain the value available at
that index.

e.g.
tup1 = ("comp sc", "info practices", 2017, 2018)
tup2 = (5,11,22,44,9,66)
print ("tup1[0]: ", tup1[0])
print ("tup2[1:5]: ", tup2[1:5])

Output
('tup1[0]: ', 'comp sc')
('tup2[1:5]: ', (11, 22, 44, 9))
Iterating Through A Tuple
Element of the tuple can be accessed sequentially
using loop.
e.g.
tup = (5,11,22)
for i in range(0,len(tup)):
print(tup[i])

Output
5
11
22
Updating Tuples
Tuples are immutable,that’s why we can’t change the content
of tuple.It’s alternate way is to take contents of existing tuple
and create another tuple with these contents as well as new
content.
E.g.
tup1 = (1, 2)
tup2 = ('a', 'b')
tup3 = tup1 + tup2
print (tup3)

Output
(1, 2, 'a', 'b')
Packing Unpacking Tuples And using list constructor
Tuples are immutable,that’s why we can’t change the content
of tuple. It’s other alternate way is packing unpacking
E.g.
a,b = (1, 2)
tup2 = ('a', 'b')
tup3 = tup1 + tup2
print (tup3)

Output
(1, 2, 'a', 'b')
Delete Tuple Elements
Direct deletion of tuple element is not possible but
shifting of required content after discard of
unwanted content to another tuple.
e.g.
tup1 = (1, 2,3)
tup3 = tup1[0:1] + tup1[2:]
print (tup3)

Output
(1, 3)

NOTE : Entire tuple can be deleted using del statement.


e.g. del tup1
Editing Tuple Elements
Direct deletion of tuple element is not possible.

Using Packing Unpacking : Using constructor :

NOTE : Slicing and creating a new tuple can be used to


edit a tuple.
Basic Tuples Operations
Python Expression Results Description
len((1, 2)) 2 Length
(1, 2) + (4, 5) (1, 2, 4, 5) Concatenation
(‘CS',) * 2 (‘CS', ‘CS‘) Repetition
5 in (1, 2, 3) False Membership
for x in (4,2,3) : 423 Iteration
print (x, end = ' ')
(‘a’,’b’,’c’) > (‘a’,’b’,’z’) False Comparison
Creating a tuple through constructor method :

tuple(seq) Converts a list, string, set, dictionary into tuple. Dictionary


will take only keys and string characters become elements

t = tuple() #empty tuple created as []


t = tuple(“abcd”) #tuple with four elements as (‘a’,’b’,’c’,’d’)
t = tuple((1,2)) #tuple from tuple created as (1,2)
t = tuple([1,2]) #tuple from another list created as (1,2)
t = tuple({1:’a’,2:’b’}] #tuple from dictionary with only keys

Build in methods :
Function Description
len(t) Return total length of the tuple.
max(t) Return item with maximum value in the tuple.
min(t) Return item with min value in the tuple.
sum(t) Return the sum of integers,float and Boolean ( True or False)
sorted(t) Returns after sorting the list. sorted(list,reverse=True) =desc sorted
del(t) Deletes the tuple. Syntax error if try to return
Build in methods :
Function Description
len(t) Return total length of the tuple.
max(t) Return item with maximum value in the tuple containing
integer and Boolean values
min(t) Return item with min value in the tuple containing integer
and Boolean values
sum(t) Return the sum of integers and Boolean values
sorted(t) Sorts the tuple and returns it in List form. Tuple should be
sorted(t,reverse=True) homogeneous. Descending if reverse=True
del(list) Deletes the list and does not even return None
t=[1,2,True,False]
len(t) #4
max(t) #2
min(t) #0
sum(t) #4
sorted(t) # [False,1,True,2]
del(t) # Deletes the list and does not return anything not even None
(syntax error if try to return)
NameError thrown if list not found for all the methods
Important methods and functions of tuple

Function Description Remarks


tup.index(ele) Returns the index of the element ValueError if element not in range
tup.count(ele) Return number of times element 0 returned if element not in tuple
present in the tuple

NameError if all the methods if list not present


max of a tuple elements.
x = (1,4,6)
r= max(x)
print('maximum value in tuple', r)
OUTPUT:6

mean/average of a tuple elements.


x = (1,4,6)
r= sum(x)/len(x)
print(mean of tuple is ', r)
OUTPUT:3.66
Linear Search in Tuple

You might also like