# 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 [1]:
myset = set()
myset

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

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

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

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

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

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

Out[7]:
5

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

In [8]:
# add an element using add()
myset = set()
myset

Out[8]:
{1, 2, 3}
In [9]:
myset.add(2)
myset

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

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

set1

Out[11]:
{1, 2, 3, 8, 9, 10}
In [12]:
set2

Out[12]:
{8, 9, 10}
In [13]:
# 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[13]:
{1, 2, 3, 4, 5}
In [14]:
myset.remove(1)
myset

Out[14]:
{2, 3, 4, 5}
In [15]:
myset.discard(5)
myset

Out[15]:
{2, 3, 4}
In [16]:
myset.discard(99)
myset

Out[16]:
{2, 3, 4}
In [17]:
try:
myset.remove(99)
except KeyError:

value not found.

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

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

Out[19]:
set()

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

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

c
b
a


### Finding the union of sets

set1.union(set2)

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

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

Out[22]:
{1, 2, 3, 4, 5, 6}

### Find the intersection of sets

set1.intersection(set2)

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

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

Out[24]:
{3, 4}

### Finding the difference of sets

set1.difference(set2)

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

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

Out[26]:
{1, 2}

### Finding the symmetric difference of sets

set1.symmetric_difference(set2)

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

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

Out[28]:
{1, 2, 5, 6}

### Finding subsets and supersets

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

print(set1)
print(set2)

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

In [30]:
set2.issubset(set1)

Out[30]:
True
In [31]:
set1.issuperset(set2)

Out[31]:
True
In [32]:
# using <= and >=
set2 <= set1

Out[32]:
True
In [33]:
set1 >= set2

Out[33]:
True
In [ ]:


In [ ]: