Python__OOP Lab 13 (1)
Python__OOP Lab 13 (1)
Engineering
Riphah College of Science & Technology
Faculty of Engineering & Applied Sciences
Riphah International University, Lahore
OBJECTIVES:
(i) To study and implement Tuple, Set and Dictionary
SAP: 58147
EEDEPARTMENT
Theory
Python Tuple
A tuple is a collection similar to a Python list. The primary difference is that we cannot
modify a tuple once it is created.
Tuples are ordered collection of data items.They store multiple items in a single variable.
Tuple items are separated by commas and enclosed within round brackets ().
Tuples are unchangeable meaning we can not alter them after creation.
A tuple is a collection which is ordered and unchangeable.
Tuple allows duplicate values.
Example
Example
tuple1 = (1,2,2,3,5,4,6)
tuple2 = ("Red", "Green",
"Blue")
print(tuple1)
print(tuple2)
EEDEPARTMENT
Tuple Indexes
Manipulating Tuples
• Tuples are immutable, hence if you want to add, remove or change tuple items, then
first you must convert the tuple to a list.
• Then perform operation on that list and convert it back to tuple.
Example
EEDEPARTMENT
Python Sets
• Sets are unordered collection of data items.
• They store multiple items in a single variable.
• Sets items are separated by commas and enclosed within curly brackets {}.
• Sets are unchangeable, meaning you cannot change items of the set once created.
• Sets do not contain duplicate items.
Example
SET={2,3,"carol",5}
if 2 in SET:
print("value found")
Access Items
Example
Python Dictionaries
Object Oriented Programming 2nd Semester-EE RCST Lahore
Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering
EEDEPARTMENT
Example
EEDEPARTMENT
Lab Task
Write three separate programs to show the use of set, tuple and dictionary.
EEDEPARTMENT
Conclusion: Tuple, Set, and Dictionary are three fundamental data structures that serve distinct
purposes. A Tuple is an ordered and immutable collection of elements, defined using parentheses and
useful when storing unchangeable data. A Set is an unordered and mutable collection of unique
elements, defined using curly braces and useful when storing unique data without caring about order.
A Dictionary is an unordered and mutable collection of key-value pairs, also defined using curly braces
and useful when storing data with keys and values, allowing for efficient access and modification