0% found this document useful (0 votes)
3 views6 pages

10.python Set

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

10.python Set

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

0Python Sets

Set
Sets are used to store multiple items in a single variable.

Set is one of 4 built-in data types in Python used to store collections of data, the other 3
are List, Tuple, and Dictionary, all with different qualities and usage.

A set is a collection which is unordered, unchangeable*, and unindexed.

Create a Set:

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


print(thisset)

Duplicates Not Allowed


Sets cannot have two items with the same value.

Example
Duplicate values will be ignored:

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

print(thisset)

Set Items - Data Types


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:

set1 = {"abc", 34, True, 40, "male"}

Python - Access Set Items


Access Items
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.

Loop through the set, and print the values:

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

for x in thisset:
print(x)

Check if "banana" is present in the set:

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

print("banana" in thisset)

Example
Check if "banana" is NOT present in the set:

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

print("banana" not in thisset)

Python - Add Set Items


Add an item to a set, using the add() method:

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

[Link]("orange")

print(thisset)

Python - Remove Set Items


Remove "banana" by using the remove() method:

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

[Link]("banana")

print(thisset)
Python - Join Sets
Join Sets
There are several ways to join two or more sets in Python.

The union() and update() methods joins all items from both sets.

The intersection() method keeps ONLY the duplicates.

The difference() method keeps the items from the first set that are not in the other
set(s).

The symmetric_difference() method keeps all items EXCEPT the duplicates.

Union
The union() method returns a new set with all items from both sets.

Join set1 and set2 into a new set:

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


set2 = {1, 2, 3}

set3 = [Link](set2)
print(set3)

You can use the | operator instead of the union() method, and you will get the same
result.

Use | to join two sets:

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


set2 = {1, 2, 3}

set3 = set1 | set2


print(set3)

Join Multiple Sets


All the joining methods and operators can be used to join multiple sets.

When using a method, just add more sets in the parentheses, separated by commas:

Join multiple sets with the union() method:


set1 = {"a", "b", "c"}
set2 = {1, 2, 3}
set3 = {"John", "Elena"}
set4 = {"apple", "bananas", "cherry"}

myset = [Link](set2, set3, set4)


print(myset)

When using the | operator, separate the sets with more | operators:

Example
Use | to join two sets:

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


set2 = {1, 2, 3}
set3 = {"John", "Elena"}
set4 = {"apple", "bananas", "cherry"}

myset = set1 | set2 | set3 |set4


print(myset)

Join a Set and a Tuple


Join a set with a tuple:

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


y = (1, 2, 3)

z = [Link](y)
print(z)

Intersection
Keep ONLY the duplicates

The intersection() method will return a new set, that only contains
the items that are present in both sets.

Join set1 and set2, but keep only the duplicates:

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


set2 = {"google", "microsoft", "apple"}
set3 = [Link](set2)
print(set3)
{'apple'}

You can use the & operator instead of the intersection() method, and
you will get the same result.

Example
Use & to join two sets:

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


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

set3 = set1 & set2


print(set3)

Difference
The difference() method will return a new set that will contain only
the items from the first set that are not present in the other set.

Example
Keep all items from set1 that are not in set2:

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


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

set3 = [Link](set2)

print(set3)

{'banana', 'cherry'}

Use - to join two sets:

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


set2 = {"google", "microsoft", "apple"}
set3 = set1 - set2
print(set3)

Symmetric Differences
The symmetric_difference() method will keep only the elements
that are NOT present in both sets.

Example
Keep the items that are not present in both sets:

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


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

set3 = set1.symmetric_difference(set2)

print(set3)

{'google', 'banana', 'microsoft', 'cherry'}

You might also like