Python
Programming
Set in Python
Learning
outcomes:
• Python Set
• Can we access element of Set?
• Updating Set
• Delete Set Elements
• Properties of Dictionary Keys
• Built-in Dictionary Functions and
Methods
Set
A Python set is the collection of the unordered
items. Each element in the set must be unique,
immutable, and the sets remove the duplicate
elements. Sets are mutable which means we can
modify it after its creation.
Unlike other collections in Python, there is no index
attached to the elements of the set, i.e., we cannot
directly access any element of the set by the index.
However, we can print them all together, or we can
get the list of elements by looping through the set.
Set
Creating a set:
The set can be created by enclosing the comma-
separated immutable items with the curly braces {}.
Python also provides the set() method, which can
be used to create the set by the passed sequence.
Example 1: Using curly braces.
Days = {"Monday", "Tuesday", "Wednesday", "Thur
sday", "Friday", "Saturday", "Sunday"}
print(Days)
print(type(Days))
Can we access element of
Set?
In Python sets, you cannot access elements by index as
they are unordered collections. However, you can
check for the existence of an element in a set using the
in operator. Here's an example:
# Creating a set
my_set = {1, 2, 3, 4, 5}
# Checking if an element exists in the set
element_to_check = 3
if element_to_check in my_set:
print(f"{element_to_check} exists in the set.")
else:
print(f"{element_to_check} does not exist in the
set.")
Updating Set
In Python, sets are mutable, meaning you can add and
remove elements from them, but you cannot directly
update an individual element by index because sets are
unordered collections. If you want to update a set, you
typically add or remove elements. Here's an example:
# Creating a set
my_set = {1, 2, 3, 4, 5}
# Adding an element to the set
my_set.add(6)
print("After adding 6:", my_set)
# Removing an element from the set
my_set.remove(3)
print("After removing 3:", my_set)
Delete Set Elements
In Python, you can delete elements from a set using the remove()
method to remove a specific element, or the discard() method, which
also removes a specific element but does not raise an error if the
element is not present in the set. Additionally, you can use the pop()
method to remove and return an arbitrary element from the set. Here's
an example demonstrating these methods:
# Creating a set
my_set = {1, 2, 3, 4, 5}
my_set.remove(3) # Remove a specific element
print("After removing 3:", my_set)
my_set.discard(5) # Discard a specific element
print("After discarding 5:", my_set)
# Remove and return an arbitrary element
removed_element = my_set.pop()
print("Removed element:", removed_element)
print("Set after pop operation:", my_set)
Mathematical Set Operations in
python
In Python, you can perform various mathematical
set operations using the built-in set methods. Here
are examples of some common set operations:
Union:
Returns a set containing all unique elements from
both sets.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = [Link](set2)
print(union_set) # Output: {1, 2, 3, 4, 5}
Mathematical Set Operations in
python
Intersection:
Returns a set containing common elements from
both sets.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection_set = [Link](set2)
print(intersection_set) # Output: {3}
Mathematical Set Operations in
python
Difference:
Returns a set containing elements that are in the
first set but not in the second set.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
difference_set = [Link](set2)
print(difference_set) # Output: {1, 2}
These operations can also be performed using
corresponding operators (| for union, & for
intersection, - for difference)
Thank you