0% found this document useful (0 votes)
5 views

Python Sets

The document provides an overview of Python sets, highlighting their characteristics such as being unordered, unchangeable, and not allowing duplicates. It explains how to create sets, add or remove elements, and join multiple sets using various methods. Additionally, it lists several set methods and their functionalities.

Uploaded by

akshitat699
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Python Sets

The document provides an overview of Python sets, highlighting their characteristics such as being unordered, unchangeable, and not allowing duplicates. It explains how to create sets, add or remove elements, and join multiple sets using various methods. Additionally, it lists several set methods and their functionalities.

Uploaded by

akshitat699
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Python Programming-

BCSG0001

Priya Agrawal (Technical Trainer)


Department of Training & Development (TND)
Python Sets
Python Sets Collection of elements.

 Basically used to store multiple items in a single variable.


 Represented by {}.
• Can be Homogeneous (similar data type) or Heterogeneous (data type
different types)

• Sets are Unchangeable


We can not change the elements but yes we can add or remove elements.
• Sets are Mutable.
(We can not modify the existing elements but we can update the set.)
• Unordered.

• Duplicates not allowed.


Python Sets Collection of unordered and unchangeable elements.

Which data type we will use to store a set/list of pin code of any state (UP).

[ ( { 281001, 281002, 281003, 281004} ) }


Python Sets Collection of unordered and unchangeable elements.

Which data type we will use to store a set/list of pin code of any state (UP).

[ ( { 281001, 281002, 281003, 281004} ) }

Answer: Set
reason: -pin code can not be duplicates.
-pin code can not be change.
-we can add or remove any pin code.
St = { }
Creating a Set This is not an empty set.
It’s a dictionary.

St = {1,2} //creating a set

print(St)

St = set() //creating a set using set() constructor

print(St)
Can we have duplicates in a set..?
st = {1,2,3,4,2,2,1,0,-1}
//creating a set

print(st)

{1, 2, 3, 4, 0, -1}
Size of a Set
s = set() s = {1,2,3,4,5,6}

print(len(s)) print(len(s))

0 6
Accessing elements
s = {1,2,3,4}

print(s[0])

We cant not access the elements because sets are unordered.( indexing can not be performed)
Accessing elements
• You cannot access items in a set by referring to an index or a key.
• But you can loop through the set items using a for loop, or ask if a specified value is present in a set,
by using the in keyword.

s = {1,2,3,4}

for i in s:
print(i)

3
2
1
4
Once a set is created, you cannot change its items, but you can add new items.
Adding elements in a set Using add() method
To add one item to a set use the add() method.

St = set()
St.add(1)
St.add(2)
St.add(3)
St.add(4)

Print(St)

{1,2,3,4}
Adding elements in a set Using update() method
To add items from another set into the current set, use the update() method.

You can add any iterable object


(tuples, lists, dictionaries et,).
removing elements from the set Using remove() method
Remove() method only removes one
element at a
time, to remove out range of elements
then an
Error arises if element doesn’t exist in
the set.
removing elements from the set Using discard() method

Note: If the item to remove does not


exist, discard() will NOT raise an error.
removing elements from the set Using clear() method
used to erase all the elements of set. After this operation, set becomes empty.
del keyword
used to delete a set.
PTR’s
 You can also use the pop(), method to remove an item, but this method will
remove the last item. Remember that sets are unordered, so you will not know
what item that gets removed.
 The return value of the pop() method is the removed item.
 We can’t perform slicing on set. (due to unordered set.)
Join sets
There are several ways to join two or more sets in Python.

union()
update()

intersection()
intersection_update()

symmetric_difference()
symmetric_difference_update()
Join sets – union()
Returns a new set containing all items from both sets.
Join sets – update()
To add items from another set into the current set, use the update() method.
Join sets – update()
To add items from another set into the current set, use the update() method.
Note: Both union() and update() will exclude any duplicate items.
Join sets – intersection()
Return a new set, that only contains the items that are present in both sets.
Join sets – intersection_update()
No return.
Join sets – symmetric_difference()
Return set.
Join sets – symmetric_difference_update()
No return.
Set methods
Method Description
add() Adds an element to the set
clear() Removes all the elements from the set

difference() Returns a set containing the difference


between two or more sets
difference_update() Removes the items in this set that are also
included in another, specified set
discard() Remove the specified item
intersection() Returns a set, that is the intersection of two
other sets
intersection_update() Removes the items in this set that are not
present in other, specified set(s)
Set methods
isdisjoint() Returns whether two sets have a intersection or
not
issubset() Returns whether another set contains this set
or not
issuperset() Returns whether this set contains another set
or not
pop() Removes an element from the set
remove() Removes the specified element
symmetric_difference() Returns a set with the symmetric differences of
two sets
symmetric_difference_update() inserts the symmetric differences from this set
and another
union() Return a set containing the union of sets
update() Update the set with the union of this set and
others

You might also like