0% found this document useful (0 votes)
6 views3 pages

Set Python Implementation - Ipynb - Colab

The document provides a Python implementation of sets, highlighting their properties such as being unordered, unindexed, and immutable. It demonstrates various set operations including intersection, union, difference, subset, superset, and symmetric difference with examples. Additionally, it shows how to handle errors related to set indexing and immutability.

Uploaded by

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

Set Python Implementation - Ipynb - Colab

The document provides a Python implementation of sets, highlighting their properties such as being unordered, unindexed, and immutable. It demonstrates various set operations including intersection, union, difference, subset, superset, and symmetric difference with examples. Additionally, it shows how to handle errors related to set indexing and immutability.

Uploaded by

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

5/3/25, 5:45 PM 2. Set python implementation.

ipynb - Colab

s = {1, 2, 2, 3, 4, 5}
s

{1, 2, 3, 4, 5}

type(s)

set

#set is a collection of unique elements


#unordered, unindexed and immutable
s

{1, 2, 3, 4, 5}

s[0] #throw an error

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[4], line 1
----> 1 s[0] #throw an error

TypeError: 'set' object is not subscriptable

 

s[0] = 2 #immutable

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[5], line 1
----> 1 s[0] = 2

TypeError: 'set' object does not support item assignment

 

max(s)

sum(s)

15

min(s)

sorted(s)

[1, 2, 3, 4, 5]

#set operations>> intersection, union, subset, superset, difference and symmetric difference

set1 = {34, 67, 78, 100}


set2 = {67, 78}

#intersection
set1 & set2

{67, 78}

#intersection
set1.intersection(set2)

{67, 78}

#union
set1 | set2

https://colab.research.google.com/drive/1xLby1ouJ7_l15-sLoQrXdg1Swx-ZRdYc#printMode=true 1/3
5/3/25, 5:45 PM 2. Set python implementation.ipynb - Colab

{34, 67, 78, 100}

#union
set1.union(set2)

{34, 67, 78, 100}

#difference
set1-set2

{34, 100}

#difference
set1.difference(set2)

{34, 100}

#superset
set1.issuperset(set2)

True

#subset
set1.issubset(set2)

False

#subset
set2.issubset(set2)

True

set2.issuperset(set1)

False

#symmetric difference

set1 = {34, 67, 78, 100}


set2 = {67, 78, 102}

set1 ^ set2

{34, 100, 102}

set1.symmetric_difference(set2)

{34, 100, 102}

Start coding or generate with AI.

https://colab.research.google.com/drive/1xLby1ouJ7_l15-sLoQrXdg1Swx-ZRdYc#printMode=true 2/3
5/3/25, 5:45 PM 2. Set python implementation.ipynb - Colab

https://colab.research.google.com/drive/1xLby1ouJ7_l15-sLoQrXdg1Swx-ZRdYc#printMode=true 3/3

You might also like