0% found this document useful (0 votes)
25 views43 pages

9 Sets, Dictionary 28 11 2022

Uploaded by

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

9 Sets, Dictionary 28 11 2022

Uploaded by

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

Python Sets

SETS
• A set is a collection which
is unordered, unchangeable*,
and unindexed.
• Sets are written with curly brackets.
Create a Set:
thisset ={"apple", "banana", "cherry"}
print(thisset)
Set Items

• Set items are unordered,


unchangeable, and do not allow
duplicate
Unordered
• Unordered means that the items in
a set do not have a defined order.
• Set items can appear in a different
order every time you use them, and
cannot be referred to by index or
key values.
Unchangeable
• Set items are unchangeable, meaning
that we cannot change the items after
the set has been created.
• Once a set is created, you cannot
change its items, but you can remove
items and add new items.
Duplicates Not Allowed

• Sets cannot have two items with the


same value.
• Duplicate values will be ignored:
• thisset =
{"apple", "banana", "cherry", "apple"}
print(thisset)
Set Items - Data Types
• Set items can be of any data type:
• Example
• String, int and boolean data types:
• set1 =
{"apple", "banana", "cherry"}
set2 = {1, 5, 7, 9, 3}
set3 = {True, False, False}
• A set can contain different data types:
• Example
• A set with strings, integers and
boolean values:
• set1 =
{"abc", 34, True, 40, "male"}
SET Methods
Python has a set of built-in methods that you can use on sets.

Method Description
add() Adds an element to the set
clear() Removes all the elements from the set
copy() Returns a copy of 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)

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
Get the Length of a Set
• To determine how many items a set has,
use the len() function.
• Example
• Get the number of items in a set:
• thisset = {"apple", "banana", "cherry"}
• print(len(thisset))
type()
• What is the data type of a set?
myset
={"apple", "banana", "cherry"}
print(type(myset))
Add Items

• Once a set is created, you cannot change


its items, but you can add new items.

• To add one item to a set use the add()


method.
• Example
• Add an item to a set, using the add()
method:
thisset = {"apple", "banana", "cherry"}
thisset.add("orange")
print(thisset)
update() - Add Sets
Syntax
set.update(set)

The update() method inserts the items in set2


into set1:

thisset = {"apple", "banana", "cherry"}


mylist = ["kiwi", "orange"]
thisset.update(mylist)
print(thisset)
thisset = {"apple", "banana", "cherry"}
tropical = {"pineapple", "mango", "papaya"}
thisset.update(tropical)
print(thisset)
Remove Item
To remove an item in a set, use the
remove(), or the discard() method.
Example
Remove "banana" by using the remove()
method:
thisset = {"apple", "banana", "cherry"}
thisset.remove("banana")
print(thisset)
Note: If the item to remove does not exist, remove() will raise an error.
Remove "banana" by using the discard()
method:

thisset = {"apple", "banana", "cherry"}

thisset.discard("banana")

print(thisset)

Note : If the item to remove does not exist, discard() will NOT raise an error.
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.

Remove the last item by using the pop()


method:

thisset = {"apple", "banana", "cherry"}

x = thisset.pop()
print(x)
print(thisset)
copy()
The copy() method copies the set.

Syntax Copy the fruits set:

set.copy() fruits = {"apple", "banana", "cherry"}

x = fruits.copy()
Parameter Values
No parameters print(x)

Example:
isdisjoint() Method
Definition and Usage
The isdisjoint() method returns True if none of
the items are present in both sets, otherwise it
returns False.
Syntax
set.isdisjoint(set)
Parameter Values
set Required. The set to search for equal
items in
• Example
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.isdisjoint(y)
print(z)
issubset()
The issubset() method returns True if all
items in the set exists in the specified set,
otherwise it retuns False.
Syntax
set.issubset(set)
Parameter Values
set Required. The set to search for equal
items in
Example:
x = {"a", "b", "c"}
y = {"a", "b", "d", "c", "b"}

z = x.issubset(y)

print(z)
issuperset()
The issuperset() method returns True if all
items in the specified set exists in the
original set, otherwise it retuns False.
Syntax
set.issuperset(set)
Parameter Values
set Required. The set to search for equal
items in
Example
x = {"f", "e", "d", "c", "b"}
y = {"a", "b", "c"}

z = x.issuperset(y)

print(z)
Set clear() Method
The clear() method empties the set:

Remove all elements from the fruits set:


thisset = {"apple", "banana", "cherry"}
thisset.clear()
print(thisset)
The del keyword will delete the set
completely:

thisset = {"apple", "banana", "cherry"}


del thisset
print(thisset)
Python - Loop Sets
• Loop through the set, and print the
values:
• thisset =
{"apple", "banana", "cherry"}

for x in thisset:
print(x)
Union()
The union() method returns a new set
with all items from both sets:
Syntax
set.union(set1, set2...)

set1 = {"a", "b" , "c"}


set2 = {1, 2, 3}
set3 = set1.union(set2)
print(set3)
set1 = {2, 4, 5, 6}
set2 = {4, 6, 7, 8}
set3 = {7, 8, 9, 10}
# union of two sets
print("set1 U set2 : ", set1.union(set2))
# union of three sets
print("set1 U set2 U set3 :",
set1.union(set2, set3))
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}

set1.update(set2)
print(set1)

Note: Both union() and update() will exclude


any duplicate items.
Intersection()
Syntax
set.intersection(set1, set2 ...
etc)
s1 = {1, 2, 3}
s2 = {2, 3}
print(s1.intersection(s2))
Return a set that contains the items that
exist in both set x, and set y:

x = {"apple", "banana", "cherry"}


y = {"google", "microsoft", "apple"}
z = x.intersection(y)
print(z)
The intersection_update() method will keep
only the items that are present in both sets.

Syntax
set.intersection_update(set1, set2 ... etc)
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.intersection_update(y)
print(x)

Note –Set X is modified


Python Set difference()

A = {10, 20, 30, 40, 80}


B = {100, 30, 80, 40, 60}
print (A.difference(B))
print (B.difference(A))
Python Set | difference_update()

• If A and B are two sets. The set difference()


method will get the (A – B) and will return a
new set. The set difference_update() method
modifies the existing set. If (A – B) is
performed, then A gets modified into (A – B),
and if (B – A) is performed, then B gets
modified into (B – A).
A = {10, 20, 30, 40, 80}
B = {100, 30, 80, 40, 60}

# Modifies A and returns None


A.difference_update(B)
# Prints the modified set
print (A)
The symmetric_difference() method will return a new set, that contains
only the elements that are NOT present in both sets.

Example

Return a set that contains all items from both sets, except items that are
present in both:

Syntax
set.symmetric_difference(set)

x = {"apple", "banana", "cherry"}


y = {"google", "microsoft", "apple"}

z = x.symmetric_difference(y)

print(z)
The symmetric_difference_update() method will keep
only the elements that are NOT present in both sets.

Example

Keep the items that are not present in both sets:


x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

x.symmetric_difference_update(y)

print(x)
set copy()
set1 = {1, 2, 3, 4}
# function to copy the set
set2 = set1.copy()
# prints the copied set
print(set2)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy