[go: up one dir, main page]

0% found this document useful (0 votes)
10 views3 pages

CpEProg2_Lab7 (1)

This document outlines a laboratory experiment focused on Python sets, detailing learning outcomes, materials needed, and an overview of set characteristics. It includes procedures for creating, accessing, and modifying sets, as well as practice problems for students to apply their knowledge. Key concepts such as the inability to index sets and the differences between the remove() and discard() methods are also discussed.

Uploaded by

Jordi Ariola
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

CpEProg2_Lab7 (1)

This document outlines a laboratory experiment focused on Python sets, detailing learning outcomes, materials needed, and an overview of set characteristics. It includes procedures for creating, accessing, and modifying sets, as well as practice problems for students to apply their knowledge. Key concepts such as the inability to index sets and the differences between the remove() and discard() methods are also discussed.

Uploaded by

Jordi Ariola
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

CpEProg 2

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:

 Duplicates are not allowed.


 Insertion order is not preserved but we can sort the elements.
 Indexing and slicing are not allowed for the set.
 Heterogeneous elements are allowed.
 Set objects are mutable i.e once we create set object we can perform any changes in
that object based on our requirement.
 We can represent set elements within curly braces and with comma separation.
 We can apply mathematical operations like union, intersection ,difference etc on set
objects.

IV. Procedures/Problems:
A. Creating a set
a. Create a set using curly braces {} or the set() constructor:

# Using curly braces


my_set = {1, 2, 3, 4, 5}

# Using the set() constructor


my_set = set([1, 2, 3, 4, 5])

b. Answer the question below.

Q1. Is it possible to access set elements using index? Why?

The inability to access set elements using indexes exists because


Python sets operate without any defined order for their member elements.
Since sets lack a set order and fixed position for their elements you cannot
depend on indexing to access or refer to set items.
B. Accessing Set Elements

c. Use a loop to iterate through the set and access each element one by one:

my_set = {1, 2, 3, 4, 5}

for item in my_set:


print(item)

C. Modifying Sets

d. Add elements to a set using the add() method:

my_set = {1, 2, 3}
my_set.add(4)
print(my_set)

e. Remove elements from a set using the remove() or discard() method:

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)

Q2. What is the difference between remove() and discard() method?

Removal of set elements through remove() triggers an error when an


absent element exists yet discard() deletes the element without raising an error
regardless of its presence.

D. Practice Problems

a. Perform the following set operations in the given two sets:


set1 = {1, 2, 3}
set2 = {3, 4, 5}
 Union of the two sets.
 Intersection of the two sets.
 Difference between the first set and the second set.
 Difference between the second set and the first set.
 Symmetric difference of the two sets.
Code:

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:

You might also like