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

4 Python List, Tuple, Set 05-10-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)
8 views

4 Python List, Tuple, Set 05-10-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/ 26

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)
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))
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"}
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)
Add Sets

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


tropical = {"pineapple", "mango", "papaya"}
thisset.update(tropical)
print(thisset)
thisset = {"apple", "banana", "cherry"}
mylist = ["kiwi", "orange"]
thisset.update(mylist)
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)
The clear() method empties the 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)
set copy()
set1 = {1, 2, 3, 4}
# function to copy the set
set2 = set1.copy()
# prints the copied set
print(set2)
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)
Intersection()
s1 = {1, 2, 3}
s2 = {2, 3}
print(s1.intersection(s2))
Union()
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))

You might also like