19/08/2024
Python Data Structures
TUPLES
&
SETS
[Link] devi,MCA.,[Link].,SET.,NET.,
Head & Asst. Professor,
Dept. of Data Science,
SFRC.
19.08.2024
Tuple
It differs from List in two things:
Tuple is a sequence of immutable objects i.e. you cannot
change the values in a tuple whereas you can change the value
of list.
Tuples use parenthesis to define its elements, whereas list uses
square brackets.
1
19/08/2024
Creating Tuples
To create a Tuple, put the different comma-seperated
values within a parentheses.
Syntax is
Tup = (val1, val2,….)
where,
val can be an integer, floating number, a character or a string.
Examples
2
19/08/2024
By default, any set of comma seperated values without parentheses are
treated as Tuples.
To create a tuple with a single element, then must add a comma after
the element. In the absence of comma, then it is treated as an ordinary
data type.
Utility of Tuples
Tuples are used for representing records or
structures.
It is used to store related information about a subject
together. The information belongs to different data
types.
Example:
stud_info = ("Alice", 2408229, 84)
print(stud_info)
3
19/08/2024
Some built-in functions return a tuple. For eg, the
divmod() function returns two values – Quotient and
Remainder.
Accessing Tuple values
to access values in tuple, slice operation is used along with the index or
indices to obtain value stored at that index.
Example1:
stud_info = ("Alice", 2408229, 84)
name = stud_info[0]
rollno = stud_info[1]
percent= stud_info[2]
Example 2:
4
19/08/2024
Updating Tuple
Since tuple is immutable, the value in the tuple
cannot be changed. We can extract the tuple values
to form another value with necessary updation.
Deleting Tuple Elements
Since tuple is immutable, the tuple elements cannot
be deleted.
So create a new tuple with the elements except the
one’s need to delete.
To delete the entire tuple, del statement can be used.
Example:
5
19/08/2024
Tuple Operations
Tuple Assignment
It is a very powerful feature in Python.
It allows a tuple of variables on the left side of the
assignment to be assigned values from a tuple on the
right side.
In case of providing an expression at the right side,
the expression is getting evaluated first and then
assignment is done.
On assigning values, the number of values on both
sides should be same, otherwise an error may occur.
6
19/08/2024
Example
Tuples for returning multiple values
A function can return only a single value. To return
more than one value from a function, group multiple
values together and return them together.
7
19/08/2024
Nested Tuples
Defining a tuple inside another tuple is called the nested tuple.
A list can also be specified inside a tuple.
SET
Sets are lists with no duplicate entries.
A set is a mutable and an unordered collection of
items.
8
19/08/2024
Creating a Set
A set is created by placing comma-separated
elements inside curly braces { }.
Syntax
set = {val1, val2, … }
set() function can be used to convert a list of different types of values
into a set.
Set Operations
Pyhton sets have the ability to calculate differences
and intersections between other sets.
9
19/08/2024
Example
Points to remember,
Refer book for various SET functions.
10
19/08/2024
THANK YOU
11