CpEProg2_Lab7 (1)
CpEProg2_Lab7 (1)
Programming Logic
and Design 2 0
Name : Jordi L. Ariola 7
Date: __________________
Rating: _________________
Experiment #7:
Python Sets
I. Learning Outcomes:
At the end of this laboratory experiment, the students should be able to:
a) Demonstrate the different ways of creating, accessing, and manipulating sets in
python
II. Materials:
Laptop/Desktop
Internet Connection
Code editor: PyCharm
III. Overview:
Sets
Python sets are used to store collections of unique items. Sets are unordered and mutable,
meaning that their values can be changed once they are created. In this lab manual,
students will explore the properties and usage of sets in Python.
Characteristics:
IV. Procedures/Problems:
A. Creating a set
a. Create a set using curly braces {} or the set() constructor:
c. Use a loop to iterate through the set and access each element one by one:
my_set = {1, 2, 3, 4, 5}
C. Modifying Sets
my_set = {1, 2, 3}
my_set.add(4)
print(my_set)
my_set = {1, 2, 3}
my_set.remove(2)
my_set.discard(1)
print(my_set)
f. Update a set by adding the elements of another set to it using the update() method:
my_set = {1, 2, 3}
new_set = {3, 4, 5}
my_set.update(new_set)
print(my_set)
D. Practice Problems
Output:
b. Write a Python function that takes a list as an argument and returns a new list with
duplicates removed using a set.
Code:
Output: