0% found this document useful (0 votes)
3 views

Python__OOP Lab 13 (1)

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python__OOP Lab 13 (1)

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Department of Electrical

Engineering
Riphah College of Science & Technology
Faculty of Engineering & Applied Sciences
Riphah International University, Lahore

Program: B.Sc. Electrical engineering Semester: II


Subject: CSL-212 Object Oriented Programming Date: ……………….

Experiment 13: Tuple, Set and Dictionary in Python

OBJECTIVES:
(i) To study and implement Tuple, Set and Dictionary

Name: Syed Aoun Muhammad Shah

SAP: 58147

No. Lab Evaluation Scheme Obtained


Marks
1 Understanding and Ability to Conduct Experiment. (0-5)
2 Implementation and Results (0-5)
Total
No. Lab Report Scheme Obtained
Marks
1 Report Content (0-5)
2 Code/Output Presentation and Conclusion (0-5)
Total

Remarks (if any): ………………………………….

Name & Signature of faculty: …………………………………


Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering

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.

Create a Python Tuple

We create a tuple by placing items inside parentheses (). For example,

Example

numbers = (1, 2, -5)


print(numbers)

Example

tuple1 = (1,2,2,3,5,4,6)
tuple2 = ("Red", "Green",
"Blue")
print(tuple1)
print(tuple2)

Object Oriented Programming 2nd Semester-EE RCST Lahore


Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering

EEDEPARTMENT

Check for item in Tuple:

country = ("Spain", "Italy", "India",


"England", "Germany")
if "Germany" in country:
print("Germany is present.")

Tuple Indexes

country = ("Spain", "Italy", "India",


"England", "Germany")
# [0] [1] [2] [3]

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

countries = ("Spain", "Italy", "India", "England", "Germany")


temp = list(countries)
temp.append("Russia") #add item
temp.pop(3) #remove item
temp[2] = "Finland" #change item
countries = tuple(temp)
print(countries)

Object Oriented Programming 2nd Semester-EE RCST Lahore


Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering

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

• Cannot access items in a set by referring to an index or a key.


• We can loop through the set items using a for loop, or ask if a specified value is
present in a set, by using the in keyword.

Example

thisset = {"apple", "banana", "cherry"}


for x in thisset:
print(x)

Python Dictionaries
Object Oriented Programming 2nd Semester-EE RCST Lahore
Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering

EEDEPARTMENT

• Dictionaries are ordered collection of data items.


• They store multiple items in a single variable.
• Dictionaries items are key-value pairs that are separated by commas and enclosed
within curly brackets {}.

Example

info = {'name':'Karan', 'age':19, 'eligible':True}


print(info)

Accessing Dictionary items:

info = {'name':'Karan', 'age':19, 'eligible':True}


print(info['name'])

Accessing multiple values:

info = {'name':'Karan', 'age':19, 'eligible':True}


print(info.values())

info = {'name':'Karan', 'age':19, 'eligible':True}


print(info.keys())

Object Oriented Programming 2nd Semester-EE RCST Lahore


Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering

EEDEPARTMENT

info = {'name':'Karan', 'age':19, 'eligible':True}


print(info.items())

Lab Task

Write three separate programs to show the use of set, tuple and dictionary.

Object Oriented Programming 2nd Semester-EE RCST Lahore


Riphah College of Science and Technology,Lahore
FACULTY OF Electrical Engineering

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

Object Oriented Programming 2nd Semester-EE RCST Lahore

You might also like