SlideShare a Scribd company logo
6
Most read
7
Most read
15
Most read
Sets in python
Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
document of baabtra –Mentoring Partner
Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .
Ltd
Typing Speed
Week

Target

Achieved

1

25

23

2

30

26

3

30

28

4

35

32

5

40

38
Jobs Applied
#

Company

Designation

Applied Date

1
2
3

Aug 24

Current
Status
Sets in python
shafeeque
●

shafeequemonp@gmail.com

●

www.facebook.com/shafeequemonppambodan

●

twitter.com/shafeequemonp

●

in.linkedin.com/in/shafeequemonp

●

9809611325
Sets:
●

●

●

A set is an unordered collection of objects, unlike sequence objects such as lists and
tuples
Sets cannot have duplicate members - a given object appears in a set 0 or 1 times
All members of a set have to be hashable, just like dictionary keys

●

Integers, floating point numbers, tuples, and strings are hashable
process

●

dictionaries, lists, and other sets (except frozensets) are not.

●

Eg:

set(['b', 'e', 'o', 's', 'u', 't'])
set([32, 26, 12, 54])
Constructing Sets:
One way to construct sets is by passing any sequential object to the "set"
constructor

●

>>> set([0, 1, 2, 3])
set([0, 1, 2, 3])
>>> set("obtuse")
set(['b', 'e', 'o', 's', 'u', 't'])

●

Add elements to sets
>>> s = set([12, 26, 54])
>>> s.add(32)
>>> s
set([32, 26, 12, 54])
●

Update set:
>>> s.update([26, 12, 9, 14])
>>> s
set([32, 9, 12, 14, 54, 26])

●

Copy set:
●

The set function also provides a copy constructor. However, remember that the copy
constructor will copy the set, but not the individual elements
>>> s2 = s.copy()
>>> s2
set([32, 9, 12, 14, 54, 26])
Membership Testing:
●

We can check if an object is in the set using the same "in" operator as with
sequential data types
>>> 32 in s
True
>>> 6 in s
False
>>> 6 not in s
True

●

We can also test the membership of entire sets. Given two sets s1 and s2 , we
check if s1 is a subset or a superset of s2
>>> s.issubset(set([32, 8, 9, 12, 14, -4, 54, 26, 19]))
True
>>> s.issuperset(set([9, 12]))
True
●

Note that the <= and >= operators also express the issubset and issuperset functions
respectively.
>>> set([4, 5, 7]) <= set([4, 5, 7, 9])
True
>>> set([9, 12, 15]) >= set([9, 12])
True

Removing Items:
●

●

There are three functions which remove individual items from a set, called pop,
remove, and discard
The first, pop, simply removes an item from the set
>>> s = set([1,2,3,4,5,6])
>>> s.pop()
1
>>> s
set([2,3,4,5,6])
●

We also have the "remove" function to remove a specified element.
>>> s.remove(3)
>>> s
set([2,4,5,6])
●

However, removing a item which isn't in the set causes an error.
>>> s.remove(9)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
KeyError: 9

●

We also have another operation for removing elements from a set,
clear, which simply removes all elements from the set
>>> s.clear()
>>> s
set([])
Iteration Over Sets:
●

We can also have a loop move over each of the items in a set.

●

However, since sets are unordered, it is undefined which order the iteration will follow.
>>> s = set("blerg")
>>> for n in s:
...

print n,

...
rbelg
Set Operations:
●

Python allows us to perform all the standard mathematical set operations,
using members of set
●

Union
The union is the merger of two sets. Any element in s1 or s2 will appear
in their union
>>> s1 = set([4, 6, 9])
>>> s2 = set([1, 6, 8])
>>> s1.union(s2)
set([1, 4, 6, 8, 9])
>>> s1 | s2
set([1, 4, 6, 8, 9])
●

Intersection
●

Any element which is in both s1 and s2 will appear in their intersection
>>> s1 = set([4, 6, 9])
>>> s2 = set([1, 6, 8])
>>> s1.intersection(s2)
set([6])
>>> s1 & s2
set([6])
>>> s1.intersection_update(s2)
>>> s1
set([6])

●

Symmetric Difference
●

The symmetric difference of two sets is the set of elements which
are in one of either set, but not in both
>>> s1 = set([4, 6, 9])
>>> s2 = set([1, 6, 8])
>>> s1.symmetric_difference(s2)
set([8, 1, 4, 9])
>>> s1 ^ s2
set([8, 1, 4, 9])
>>> s1.symmetric_difference_update(s2)
>>> s1
set([8, 1, 4, 9])
Set Difference

●

●

Python can also find the set difference of s1 and s2 , which is the elements that
are in s1 but not in s2.
>>> s1 = set([4, 6, 9])
>>> s2 = set([1, 6, 8])
>>> s1.difference(s2)
set([9, 4])
>>> s1 - s2
set([9, 4])
>>> s1.difference_update(s2)
>>> s1
set([9, 4])

Multiple sets:
●

Starting with Python 2.6, "union", "intersection", and "difference" can work with
multiple input by using the set constructor. For example,
>>> s1 = set([3, 6, 7, 9])
>>> s2 = set([6, 7, 9, 10])
>>> s3 = set([7, 9, 10, 11])
>>> set.intersection(s1, s2, s3)
set([9, 7])
Frozenset:
●

●

●

A frozenset is basically the same as a set, except that it is immutable once it is created, its members cannot be changed
Since they are immutable, they are also hashable, which means that
frozensets can be used as members in other sets and as dictionary keys
frozensets have the same functions as normal sets, except none of the
functions that change the contents (update, remove, pop, etc.) are
available
>>> fs = frozenset([2, 3, 4])
>>> s1 = set([fs, 4, 5, 6])
>>> s1
set([4, frozenset([2, 3, 4]), 6, 5])
>>> fs.intersection(s1)
frozenset([4])
>>> fs.add(6)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'frozenset' object has no attribute
'add'
If this presentation helped you, please visit our page facebook.com/baabtra and
like it.

Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550

Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com

NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550

More Related Content

What's hot (20)

PDF
Python list
Mohammed Sikander
 
PDF
Set methods in python
deepalishinkar1
 
PDF
Strings in Python
nitamhaske
 
PDF
List,tuple,dictionary
nitamhaske
 
PPTX
Tuple in python
Sharath Ankrajegowda
 
PPTX
Python Functions
Mohammed Sikander
 
PDF
Python programming : Strings
Emertxe Information Technologies Pvt Ltd
 
PDF
Arrays in python
moazamali28
 
PPTX
Dictionary in python
vikram mahendra
 
PPTX
Python Collections
sachingarg0
 
PDF
Python programming : List and tuples
Emertxe Information Technologies Pvt Ltd
 
PPSX
python Function
Ronak Rathi
 
PDF
Python strings
Mohammed Sikander
 
PDF
Strings in python
Prabhakaran V M
 
PPSX
Modules and packages in python
TMARAGATHAM
 
PDF
Introduction to Python
Mohammed Sikander
 
Python list
Mohammed Sikander
 
Set methods in python
deepalishinkar1
 
Strings in Python
nitamhaske
 
List,tuple,dictionary
nitamhaske
 
Tuple in python
Sharath Ankrajegowda
 
Python Functions
Mohammed Sikander
 
Python programming : Strings
Emertxe Information Technologies Pvt Ltd
 
Arrays in python
moazamali28
 
Dictionary in python
vikram mahendra
 
Python Collections
sachingarg0
 
Python programming : List and tuples
Emertxe Information Technologies Pvt Ltd
 
python Function
Ronak Rathi
 
Python strings
Mohammed Sikander
 
Strings in python
Prabhakaran V M
 
Modules and packages in python
TMARAGATHAM
 
Introduction to Python
Mohammed Sikander
 

Similar to Sets in python (20)

PPTX
C++ STL (quickest way to learn, even for absolute beginners).pptx
Abhishek Tirkey
 
PPTX
C++ STL (quickest way to learn, even for absolute beginners).pptx
GauravPandey43518
 
PDF
Slicing in Python - What is It?
💾 Radek Fabisiak
 
PDF
Python lecture 05
Tanwir Zaman
 
PPT
Queue
Nabeel Ahsen
 
ODP
Very basic functional design patterns
Tomasz Kowal
 
PPT
Arrays in c programing. practicals and .ppt
Carlos701746
 
PPTX
Pythonlearn-08-Lists.pptx
MihirDatir
 
PPT
Insert Sort & Merge Sort Using C Programming
chandankumar364348
 
PDF
Introduction To Programming with Python
Sushant Mane
 
PDF
JavaScript Array Interview Questions PDF By ScholarHat
Scholarhat
 
PDF
Effective Numerical Computation in NumPy and SciPy
Kimikazu Kato
 
PPTX
BA lab1.pptx
sherifsalem24
 
PPT
UNIT III_Python Programming_aditya COllege
Ramanamurthy Banda
 
PPT
UNIT III_Python Programming_aditya COllege
Ramanamurthy Banda
 
PPTX
Developers' New features of Sql server express 2012
Ziaur Rahman
 
ODP
Talk on Standard Template Library
Anirudh Raja
 
PPT
Arrays 06.ppt
ahtishamtariq511
 
PPT
Python lab basics
Abi_Kasi
 
PPTX
07+08slide.pptx
MURADSANJOUM
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
Abhishek Tirkey
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
GauravPandey43518
 
Slicing in Python - What is It?
💾 Radek Fabisiak
 
Python lecture 05
Tanwir Zaman
 
Very basic functional design patterns
Tomasz Kowal
 
Arrays in c programing. practicals and .ppt
Carlos701746
 
Pythonlearn-08-Lists.pptx
MihirDatir
 
Insert Sort & Merge Sort Using C Programming
chandankumar364348
 
Introduction To Programming with Python
Sushant Mane
 
JavaScript Array Interview Questions PDF By ScholarHat
Scholarhat
 
Effective Numerical Computation in NumPy and SciPy
Kimikazu Kato
 
BA lab1.pptx
sherifsalem24
 
UNIT III_Python Programming_aditya COllege
Ramanamurthy Banda
 
UNIT III_Python Programming_aditya COllege
Ramanamurthy Banda
 
Developers' New features of Sql server express 2012
Ziaur Rahman
 
Talk on Standard Template Library
Anirudh Raja
 
Arrays 06.ppt
ahtishamtariq511
 
Python lab basics
Abi_Kasi
 
07+08slide.pptx
MURADSANJOUM
 
Ad

More from baabtra.com - No. 1 supplier of quality freshers (20)

PPTX
Agile methodology and scrum development
baabtra.com - No. 1 supplier of quality freshers
 
PDF
Acquiring new skills what you should know
baabtra.com - No. 1 supplier of quality freshers
 
PDF
Baabtra.com programming at school
baabtra.com - No. 1 supplier of quality freshers
 
PDF
99LMS for Enterprises - LMS that you will love
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 6 database normalisation
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 5 transactions and dcl statements
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 4 functions, views, indexing
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 3 stored procedures
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
Agile methodology and scrum development
baabtra.com - No. 1 supplier of quality freshers
 
Acquiring new skills what you should know
baabtra.com - No. 1 supplier of quality freshers
 
Baabtra.com programming at school
baabtra.com - No. 1 supplier of quality freshers
 
99LMS for Enterprises - LMS that you will love
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 6 database normalisation
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 5 transactions and dcl statements
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 4 functions, views, indexing
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
Ad

Recently uploaded (20)

PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 

Sets in python

  • 2. Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
  • 6. Sets: ● ● ● A set is an unordered collection of objects, unlike sequence objects such as lists and tuples Sets cannot have duplicate members - a given object appears in a set 0 or 1 times All members of a set have to be hashable, just like dictionary keys ● Integers, floating point numbers, tuples, and strings are hashable process ● dictionaries, lists, and other sets (except frozensets) are not. ● Eg: set(['b', 'e', 'o', 's', 'u', 't']) set([32, 26, 12, 54])
  • 7. Constructing Sets: One way to construct sets is by passing any sequential object to the "set" constructor ● >>> set([0, 1, 2, 3]) set([0, 1, 2, 3]) >>> set("obtuse") set(['b', 'e', 'o', 's', 'u', 't']) ● Add elements to sets >>> s = set([12, 26, 54]) >>> s.add(32) >>> s set([32, 26, 12, 54])
  • 8. ● Update set: >>> s.update([26, 12, 9, 14]) >>> s set([32, 9, 12, 14, 54, 26]) ● Copy set: ● The set function also provides a copy constructor. However, remember that the copy constructor will copy the set, but not the individual elements >>> s2 = s.copy() >>> s2 set([32, 9, 12, 14, 54, 26])
  • 9. Membership Testing: ● We can check if an object is in the set using the same "in" operator as with sequential data types >>> 32 in s True >>> 6 in s False >>> 6 not in s True ● We can also test the membership of entire sets. Given two sets s1 and s2 , we check if s1 is a subset or a superset of s2 >>> s.issubset(set([32, 8, 9, 12, 14, -4, 54, 26, 19])) True >>> s.issuperset(set([9, 12])) True
  • 10. ● Note that the <= and >= operators also express the issubset and issuperset functions respectively. >>> set([4, 5, 7]) <= set([4, 5, 7, 9]) True >>> set([9, 12, 15]) >= set([9, 12]) True Removing Items: ● ● There are three functions which remove individual items from a set, called pop, remove, and discard The first, pop, simply removes an item from the set >>> s = set([1,2,3,4,5,6]) >>> s.pop() 1 >>> s set([2,3,4,5,6])
  • 11. ● We also have the "remove" function to remove a specified element. >>> s.remove(3) >>> s set([2,4,5,6]) ● However, removing a item which isn't in the set causes an error. >>> s.remove(9) Traceback (most recent call last): File "<stdin>", line 1, in ? KeyError: 9 ● We also have another operation for removing elements from a set, clear, which simply removes all elements from the set >>> s.clear() >>> s set([])
  • 12. Iteration Over Sets: ● We can also have a loop move over each of the items in a set. ● However, since sets are unordered, it is undefined which order the iteration will follow. >>> s = set("blerg") >>> for n in s: ... print n, ... rbelg
  • 13. Set Operations: ● Python allows us to perform all the standard mathematical set operations, using members of set ● Union The union is the merger of two sets. Any element in s1 or s2 will appear in their union >>> s1 = set([4, 6, 9]) >>> s2 = set([1, 6, 8]) >>> s1.union(s2) set([1, 4, 6, 8, 9]) >>> s1 | s2 set([1, 4, 6, 8, 9])
  • 14. ● Intersection ● Any element which is in both s1 and s2 will appear in their intersection >>> s1 = set([4, 6, 9]) >>> s2 = set([1, 6, 8]) >>> s1.intersection(s2) set([6]) >>> s1 & s2 set([6]) >>> s1.intersection_update(s2) >>> s1 set([6]) ● Symmetric Difference ● The symmetric difference of two sets is the set of elements which are in one of either set, but not in both >>> s1 = set([4, 6, 9]) >>> s2 = set([1, 6, 8]) >>> s1.symmetric_difference(s2) set([8, 1, 4, 9]) >>> s1 ^ s2 set([8, 1, 4, 9]) >>> s1.symmetric_difference_update(s2) >>> s1 set([8, 1, 4, 9])
  • 15. Set Difference ● ● Python can also find the set difference of s1 and s2 , which is the elements that are in s1 but not in s2. >>> s1 = set([4, 6, 9]) >>> s2 = set([1, 6, 8]) >>> s1.difference(s2) set([9, 4]) >>> s1 - s2 set([9, 4]) >>> s1.difference_update(s2) >>> s1 set([9, 4]) Multiple sets: ● Starting with Python 2.6, "union", "intersection", and "difference" can work with multiple input by using the set constructor. For example, >>> s1 = set([3, 6, 7, 9]) >>> s2 = set([6, 7, 9, 10]) >>> s3 = set([7, 9, 10, 11]) >>> set.intersection(s1, s2, s3) set([9, 7])
  • 16. Frozenset: ● ● ● A frozenset is basically the same as a set, except that it is immutable once it is created, its members cannot be changed Since they are immutable, they are also hashable, which means that frozensets can be used as members in other sets and as dictionary keys frozensets have the same functions as normal sets, except none of the functions that change the contents (update, remove, pop, etc.) are available >>> fs = frozenset([2, 3, 4]) >>> s1 = set([fs, 4, 5, 6]) >>> s1 set([4, frozenset([2, 3, 4]), 6, 5]) >>> fs.intersection(s1) frozenset([4]) >>> fs.add(6) Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'frozenset' object has no attribute 'add'
  • 17. If this presentation helped you, please visit our page facebook.com/baabtra and like it. Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
  • 18. Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: [email protected] NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550

Editor's Notes

  • #2: &lt;number&gt;
  • #3: &lt;number&gt;
  • #6: &lt;number&gt;
  • #18: &lt;number&gt;
  • #19: &lt;number&gt;