18-08-2024
Tuple
Python Data Structures 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
TUPLES of list.
& Tuples use parenthesis to define its elements, whereas list uses
SETS square brackets.
[Link] devi,MCA.,[Link].,SET.,NET.,
Head & Asst. Professor,
Dept. of Data Science,
SFRC.
19.08.2024
Creating Tuples Examples
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.
1
18-08-2024
Utility of Tuples
By default, any set of comma seperated values without parentheses are Tuples are used for representing records or
treated as Tuples.
structures.
It is used to store related information about a subject
together. The information belongs to different data
types.
Example:
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 stud_info = ("Alice", 2408229, 84)
data type. print(stud_info)
Accessing Tuple values
to access values in tuple, slice operation is used along with the index or
Some built-in functions return a tuple. For eg, the indices to obtain value stored at that index.
divmod() function returns two values – Quotient and Example1:
stud_info = ("Alice", 2408229, 84)
Remainder. name = stud_info[0]
rollno = stud_info[1]
percent= stud_info[2]
Example 2:
2
18-08-2024
Updating Tuple Deleting Tuple Elements
Since tuple is immutable, the value in the tuple Since tuple is immutable, the tuple elements cannot
cannot be changed. We can extract the tuple values be deleted.
to form another value with necessary updation. 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:
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.
3
18-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.
Nested Tuples SET
Defining a tuple inside another tuple is called the nested tuple. Sets are lists with no duplicate entries.
A set is a mutable and an unordered collection of
items.
A list can also be specified inside a tuple.
4
18-08-2024
Creating a Set Set Operations
A set is created by placing comma-separated Pyhton sets have the ability to calculate differences
elements inside curly braces { }. and intersections between other sets.
Syntax
set = {val1, val2, … }
set() function can be used to convert a list of different types of values
into a set.
Example
Points to remember,
Refer book for various SET functions.
5
18-08-2024
THANK YOU