# Sets

Acknowledgement : the contents of this notebook are partially based on the book "Starting Out with Python (3rd Edition)".

A set contains a collection of unique values and work like a mathematical set.

### Creating a set

In :
myset = set()
myset

Out:
set()
In :
myset = set(['a','b','c'])
myset

Out:
{'a', 'b', 'c'}
In :
myset = set('abc')
myset

Out:
{'a', 'b', 'c'}
In :
# no duplicate element
myset = set('aaaabc')
myset

Out:
{'a', 'b', 'c'}
In :
myset = set('one two three')
myset

Out:
{' ', 'e', 'h', 'n', 'o', 'r', 't', 'w'}
In :
# set of words
myset = set(['one','two','three'])
myset

Out:
{'one', 'three', 'two'}
In :
# getting the number of elements
myset = set([1,2,3,4,5])
len(myset)

Out:
5

### Adding and removing elements

Sets are mutable objects, so we can add items to them and remove items from them.

In :
# add an element using add()
myset = set()
myset.add(1)
myset.add(2)
myset.add(3)
myset

Out:
{1, 2, 3}
In :
myset.add(2)
myset

Out:
{1, 2, 3}
In :
# add multiple elements using update()
myset = set([1,2,3])
myset.update([4,5,6])
myset

Out:
{1, 2, 3, 4, 5, 6}
In :
set1 = set([1,2,3])
set2 = set([8,9,10])
set1.update(set2)

set1

Out:
{1, 2, 3, 8, 9, 10}
In :
set2

Out:
{8, 9, 10}
In :
# remove an element using remove() or discard()
# note : the remove method raises an exception when the specified item is not found in the set,
# whereas the discard method does not.

myset = set([1,2,3,4,5])
myset

Out:
{1, 2, 3, 4, 5}
In :
myset.remove(1)
myset

Out:
{2, 3, 4, 5}
In :
myset.discard(5)
myset

Out:
{2, 3, 4}
In :
myset.discard(99)
myset

Out:
{2, 3, 4}
In :
try:
myset.remove(99)
except KeyError:
print('value not found.')

value not found.

In :
# clear all elements using clear()
myset = set([1,2,3,4,5])
myset

Out:
{1, 2, 3, 4, 5}
In :
myset.clear()
myset

Out:
set()

### Using the for loop to iterate over a set

In :
myset = set(['a','b','c'])
for val in myset:
print(val)

c
b
a


### Finding the union of sets

set1.union(set2)

In :
set1 = set([1,2,3,4])
set2 = set([3,4,5,6])
set3 = set1.union(set2)
set3

Out:
{1, 2, 3, 4, 5, 6}
In :
# using | operator
set1 = set([1,2,3,4])
set2 = set([3,4,5,6])
set3 = set1 | set2
set3

Out:
{1, 2, 3, 4, 5, 6}

### Find the intersection of sets

set1.intersection(set2)

In :
set1 = set([1,2,3,4])
set2 = set([3,4,5,6])
set3 = set1.intersection(set2)
set3

Out:
{3, 4}
In :
# using & operator
set1 = set([1,2,3,4])
set2 = set([3,4,5,6])
set3 = set1 & set2
set3

Out:
{3, 4}

### Finding the difference of sets

set1.difference(set2)

In :
set1 = set([1,2,3,4])
set2 = set([3,4,5,6])
set3 = set1.difference(set2)
set3

Out:
{1, 2}
In :
# using - operator
set1 = set([1,2,3,4])
set2 = set([3,4,5,6])
set3 = set1 - set2
set3

Out:
{1, 2}

### Finding the symmetric difference of sets

set1.symmetric_difference(set2)

In :
set1 = set([1,2,3,4])
set2 = set([3,4,5,6])
set3 = set1.symmetric_difference(set2)
set3

Out:
{1, 2, 5, 6}
In :
# using ^ operator
set1 = set([1,2,3,4])
set2 = set([3,4,5,6])
set3 = set1 ^ set2
set3

Out:
{1, 2, 5, 6}

### Finding subsets and supersets

In :
set1 = set([1,2,3,4])
set2 = set([2,3])

print(set1)
print(set2)

{1, 2, 3, 4}
{2, 3}

In :
set2.issubset(set1)

Out:
True
In :
set1.issuperset(set2)

Out:
True
In :
# using <= and >=
set2 <= set1

Out:
True
In :
set1 >= set2

Out:
True
In [ ]:


In [ ]: