SlideShare uma empresa Scribd logo
1 de 18
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

Mais conteúdo relacionado

Mais procurados

Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and PackagesDamian T. Gordon
 
Python dictionary
Python dictionaryPython dictionary
Python dictionarySagar Kumar
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in pythonhydpy
 
Packages In Python Tutorial
Packages In Python TutorialPackages In Python Tutorial
Packages In Python TutorialSimplilearn
 
Python Collections Tutorial | Edureka
Python Collections Tutorial | EdurekaPython Collections Tutorial | Edureka
Python Collections Tutorial | EdurekaEdureka!
 
Generators In Python
Generators In PythonGenerators In Python
Generators In PythonSimplilearn
 
Arrays in python
Arrays in pythonArrays in python
Arrays in pythonmoazamali28
 
Functions in python
Functions in pythonFunctions in python
Functions in pythoncolorsof
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionarySoba Arjun
 
List,tuple,dictionary
List,tuple,dictionaryList,tuple,dictionary
List,tuple,dictionarynitamhaske
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide shareDevashish Kumar
 
File and directories in python
File and directories in pythonFile and directories in python
File and directories in pythonLifna C.S
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in javaVishnu Suresh
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in PythonDevashish Kumar
 

Mais procurados (20)

Namespaces
NamespacesNamespaces
Namespaces
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
 
Set methods in python
Set methods in pythonSet methods in python
Set methods in python
 
Python dictionary
Python dictionaryPython dictionary
Python dictionary
 
Python list
Python listPython list
Python list
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
 
Packages In Python Tutorial
Packages In Python TutorialPackages In Python Tutorial
Packages In Python Tutorial
 
Python Collections Tutorial | Edureka
Python Collections Tutorial | EdurekaPython Collections Tutorial | Edureka
Python Collections Tutorial | Edureka
 
Python Modules
Python ModulesPython Modules
Python Modules
 
Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
 
Generators In Python
Generators In PythonGenerators In Python
Generators In Python
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
 
List,tuple,dictionary
List,tuple,dictionaryList,tuple,dictionary
List,tuple,dictionary
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 
File and directories in python
File and directories in pythonFile and directories in python
File and directories in python
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
 

Semelhante a Sets in python

C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxAbhishek Tirkey
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxGauravPandey43518
 
Very basic functional design patterns
Very basic functional design patternsVery basic functional design patterns
Very basic functional design patternsTomasz Kowal
 
Pythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxMihirDatir
 
Insert Sort & Merge Sort Using C Programming
Insert Sort & Merge Sort Using C ProgrammingInsert Sort & Merge Sort Using C Programming
Insert Sort & Merge Sort Using C Programmingchandankumar364348
 
Introduction To Programming with Python
Introduction To Programming with PythonIntroduction To Programming with Python
Introduction To Programming with PythonSushant Mane
 
Effective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPyEffective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPyKimikazu Kato
 
UNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllegeUNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllegeRamanamurthy Banda
 
UNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllegeUNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllegeRamanamurthy Banda
 
Developers' New features of Sql server express 2012
Developers' New features of Sql server express 2012Developers' New features of Sql server express 2012
Developers' New features of Sql server express 2012Ziaur Rahman
 
Talk on Standard Template Library
Talk on Standard Template LibraryTalk on Standard Template Library
Talk on Standard Template LibraryAnirudh Raja
 
Python lab basics
Python lab basicsPython lab basics
Python lab basicsAbi_Kasi
 
Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303Namgee Lee
 

Semelhante a Sets in python (20)

C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
 
Slicing in Python - What is It?
Slicing in Python - What is It?Slicing in Python - What is It?
Slicing in Python - What is It?
 
Python lecture 05
Python lecture 05Python lecture 05
Python lecture 05
 
Queue
QueueQueue
Queue
 
Very basic functional design patterns
Very basic functional design patternsVery basic functional design patterns
Very basic functional design patterns
 
Pythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptx
 
Insert Sort & Merge Sort Using C Programming
Insert Sort & Merge Sort Using C ProgrammingInsert Sort & Merge Sort Using C Programming
Insert Sort & Merge Sort Using C Programming
 
Introduction To Programming with Python
Introduction To Programming with PythonIntroduction To Programming with Python
Introduction To Programming with Python
 
Effective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPyEffective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPy
 
BA lab1.pptx
BA lab1.pptxBA lab1.pptx
BA lab1.pptx
 
UNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllegeUNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllege
 
UNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllegeUNIT III_Python Programming_aditya COllege
UNIT III_Python Programming_aditya COllege
 
Developers' New features of Sql server express 2012
Developers' New features of Sql server express 2012Developers' New features of Sql server express 2012
Developers' New features of Sql server express 2012
 
Talk on Standard Template Library
Talk on Standard Template LibraryTalk on Standard Template Library
Talk on Standard Template Library
 
Arrays 06.ppt
Arrays 06.pptArrays 06.ppt
Arrays 06.ppt
 
Python lab basics
Python lab basicsPython lab basics
Python lab basics
 
07+08slide.pptx
07+08slide.pptx07+08slide.pptx
07+08slide.pptx
 
Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303
 
07012023.pptx
07012023.pptx07012023.pptx
07012023.pptx
 

Mais de baabtra.com - No. 1 supplier of quality freshers

Mais de baabtra.com - No. 1 supplier of quality freshers (20)

Agile methodology and scrum development
Agile methodology and scrum developmentAgile methodology and scrum development
Agile methodology and scrum development
 
Best coding practices
Best coding practicesBest coding practices
Best coding practices
 
Core java - baabtra
Core java - baabtraCore java - baabtra
Core java - baabtra
 
Acquiring new skills what you should know
Acquiring new skills   what you should knowAcquiring new skills   what you should know
Acquiring new skills what you should know
 
Baabtra.com programming at school
Baabtra.com programming at schoolBaabtra.com programming at school
Baabtra.com programming at school
 
99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love 99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 
Php database connectivity
Php database connectivityPhp database connectivity
Php database connectivity
 
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
 
Chapter 5 transactions and dcl statements
Chapter 5  transactions and dcl statementsChapter 5  transactions and dcl statements
Chapter 5 transactions and dcl statements
 
Chapter 4 functions, views, indexing
Chapter 4  functions, views, indexingChapter 4  functions, views, indexing
Chapter 4 functions, views, indexing
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 3 stored procedures
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Microsoft holo lens
Microsoft holo lensMicrosoft holo lens
Microsoft holo lens
 
Blue brain
Blue brainBlue brain
Blue brain
 
5g
5g5g
5g
 
Aptitude skills baabtra
Aptitude skills baabtraAptitude skills baabtra
Aptitude skills baabtra
 
Gd baabtra
Gd baabtraGd baabtra
Gd baabtra
 

Último

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 

Último (20)

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 

Sets in python

  • 1.
  • 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: info@baabtra.com NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550

Notas do Editor

  1. &lt;number&gt;
  2. &lt;number&gt;
  3. &lt;number&gt;
  4. &lt;number&gt;
  5. &lt;number&gt;