PYTHON WITH
SETS
MADE BY :
IKSHU MISHRA
ROLL NO.
23/BAP/094
PYTHON SETS
* A set is an unordered collection of items. Every element
is unique and must be immutable.
* However, the set itself is mutable. We can add or
remove items from it.
* Sets can be used to perform mathematical set
operations like union, intersection, symmetric difference
etc.
CREATING A SET
• A set is created by placing all the elements inside curly braces {},
separated by comma or by using the built-in function set(). The elements can be of
different types (integer, float, tuple, string etc.).
• But a set cannot have a mutable element, like list, set or dictionary. as its
element.
A set can contain elements of different
type
we can convert a list
to set using set
function
A set cannot contain
lists.
LIS TUPLE
T
• appen • add
d • update
• extend • ........
• insert • remove
• pop • discard
• remov
e
ADDING ELEMENTS TO SET
• Sets are mutable. But since they are unordered, indexing have no meaning.
• We cannot access or change an element of set using indexing or slicing.
• We can add single element using the add() method and multiple elements using
the update() method.
• The update() method can take tuples, lists, strings or other sets as its argument. In
all cases, duplicates are avoided.
REMOVE ELEMENTS FROM A
SET
• A particular item can be removed from set using methods, discard() and
removed().
• using discard() if the item does not exist in the set , it remains unchanged
• but remove () will raise an error in such conditions.
SET MEMBERSHIP
TEST
• we can test , if an item exists in a set or not , using the in
operator
• given an list of elements, remove the
dupliicate element.
Read a string and find the
numbers of unique characters
in it
PYTHON SET
OPERATIONS
• sets can be used to carry out mathematical set operations like union,
intersection, difference and symmetrical difference
• We can do this with operators and methods.
SET MUTATION
OPERATIONS
• we have seen the applications of union , intersection, difference and
symmetric difference operations but these operations do not make
any changes or mutations to the set.
• we can use the following operations to create mutations to
a set.
THANK YOU