module 5 py1
module 5 py1
# initialize my_set
my_set = {1, 3, 4, 5, 6}
print(my_set)
# discard an element
my_set.discard(4)
print(my_set)
# Output: {1, 3, 5, 6}
# remove an element
my_set.remove(6)
print(my_set)
# Output: {1, 3, 5}
# discard an element
# not present in my_set
my_set.discard(2)
print(my_set)
# Output: {1, 3, 5}
# remove an element
# not present in my_set
# you will get an error.
my_set.remove(2)
# Output: KeyError
Characteristics of set
• The set as a whole is mutable
• There are no duplicate elements in a set and you
can add or even delete items in it.
• Sets can be helps to perform mathematical
operations like intersection ,union, symmetric
difference and so on
• A set can consist of elements or items with different
immutable data types like integer, string, float,
tuple, etc. However, a set cannot have mutable
elements like lists, dictionaries or a set itself.
Set operations
• Finding the set length
• Access the set
• Removing the set
• Union of set
• Set Intersections
• Difference of set
Union of sets
• The union operation (Set1 | Set2) can be used
to compare two sets and produce a new set
that contains distinct elements from both the
sets. For instance, if there are more than two
occurrences of the same element in both the
sets, the new set would produce the element
only once so there would be no repetition of
elements.
Example
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
C= A|B
Print (A|B)
Output:-
{1, 2, 3, 4, 5, 6, 7, 8}
Intersection of sets
• The intersection operation (Set1 & Set2) can
be used to compare two sets in which only the
common elements between the sets will be
produced in a new set.
• You can either use the & operator or
intersection() method in this case.
a={1, 0, 5, 2.0}
b={1,5,'abc'}
c={1,3,4,}
print(a&b)
print(a&b&c)
output:-
{1,5}
{1}
Difference of sets
• The difference operation (Set1 – Set2) can be
used to compare two sets where the elements
found only in one set will be produced in a
new set. This means that common elements
found between two sets are not produced in
the new set. You can either use the – operator
or difference() method in this case.
Example
a={10, 20,50, 4.6, 'x', 'y'}
b={20,50,'d'}
print(a-b)
o/p:-
{10, 4.6, ‘x’, ‘y’}
Symmetric difference of sets
• You can find the unique elements between two
sets by using the symmetric_difference()
method. In the example below, the output
elements are all unique and cannot be found in
another set.
• Symmetric Difference of A and B is a set of
elements in A and B but not in both (excluding
the intersection).
• Symmetric difference is performed
using ^ operator. Same can be accomplished
Example:-
set_A = {1, 2, 3, 4, 5}
set_B = {6, 7, 8, 1, 2}
print(set_A.symmetric_difference(set_B))
print(set_B.symmetric_difference(set_A))
Output:
{3, 4, 5, 6, 7, 8}
{3, 4, 5, 6, 7, 8}
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
print(A ^ B)
Output: {1, 2, 3, 6, 7, 8}
Built-in Functions with Set
• Built-infunctions like all(), any(), enumerate(),
len(), max(), min(), sorted(), sum() etc. are commonly
used with sets to perform different tasks.
• FunctionDescription
• all()Returns True if all elements of the set are true (or if
the set is empty).
• any()Returns True if any element of the set is true. If the
set is empty, returns False.
• enumerate()Returns an enumerate object. It contains
the index and value for all the items of the set as a pair.
• len()Returns the length (the number of
items) in the set.
• max()Returns the largest item in the set.
• min()Returns the smallest item in the set.
• sorted()Returns a new sorted list from
elements in the set(does not sort the set
itself).
• sum()Returns the sum of all elements in the
set.
Other Python Set Methods
• There are many set methods, some of which we have
already used above. Here is a list of all the methods that
are available with the set objects:
• add()Adds an element to the set
• clear()Removes all elements from the set
• copy()Returns a copy of the set
• difference()Returns the difference of two or more sets as a
new set
• difference_update()Removes all elements of another set
from this set
• discard()Removes an element from the set if it is a
member. (Do nothing if the element is not in set)
• intersection()Returns the intersection of two sets as a new
set
• intersection_update()Updates the set with the
intersection of itself and another
• isdisjoint()Returns True if two sets have a null intersection
• issubset()Returns True if another set contains this set
• issuperset()Returns True if this set contains another set
• pop()Removes and returns an arbitrary set element.
Raises KeyError if the set is empty
• remove()Removes an element from the set. If the
element is not a member, raises a KeyError
• symmetric_difference()Returns the symmetric difference
of two sets as a new set
• symmetric_difference_update()Updates a set with the
symmetric difference of itself and another
• union()Returns the union of sets in a new set
• update()Updates the set with the union of itself and