
Domains
Agile Management
Master Agile methodologies for efficient and timely project delivery.
View All Agile Management Coursesicon-refresh-cwCertifications
Scrum Alliance
16 Hours
Best Seller
Certified ScrumMaster (CSM) CertificationScrum Alliance
16 Hours
Best Seller
Certified Scrum Product Owner (CSPO) CertificationScaled Agile
16 Hours
Trending
Leading SAFe 6.0 CertificationScrum.org
16 Hours
Professional Scrum Master (PSM) CertificationScaled Agile
16 Hours
SAFe 6.0 Scrum Master (SSM) CertificationAdvanced Certifications
Scaled Agile, Inc.
32 Hours
Recommended
Implementing SAFe 6.0 (SPC) CertificationScaled Agile, Inc.
24 Hours
SAFe 6.0 Release Train Engineer (RTE) CertificationScaled Agile, Inc.
16 Hours
Trending
SAFe® 6.0 Product Owner/Product Manager (POPM)IC Agile
24 Hours
ICP Agile Certified Coaching (ICP-ACC)Scrum.org
16 Hours
Professional Scrum Product Owner I (PSPO I) TrainingMasters
32 Hours
Trending
Agile Management Master's Program32 Hours
Agile Excellence Master's ProgramOn-Demand Courses
Agile and ScrumRoles
Scrum MasterTech Courses and Bootcamps
Full Stack Developer BootcampAccreditation Bodies
Scrum AllianceTop Resources
Scrum TutorialProject Management
Gain expert skills to lead projects to success and timely completion.
View All Project Management Coursesicon-standCertifications
PMI
36 Hours
Best Seller
Project Management Professional (PMP) CertificationAxelos
32 Hours
PRINCE2 Foundation & Practitioner CertificationAxelos
16 Hours
PRINCE2 Foundation CertificationAxelos
16 Hours
PRINCE2 Practitioner CertificationSkills
Change ManagementMasters
Job Oriented
45 Hours
Trending
Project Management Master's ProgramUniversity Programs
45 Hours
Trending
Project Management Master's ProgramOn-Demand Courses
PRINCE2 Practitioner CourseRoles
Project ManagerAccreditation Bodies
PMITop Resources
Theories of MotivationCloud Computing
Learn to harness the cloud to deliver computing resources efficiently.
View All Cloud Computing Coursesicon-cloud-snowingCertifications
AWS
32 Hours
Best Seller
AWS Certified Solutions Architect - AssociateAWS
32 Hours
AWS Cloud Practitioner CertificationAWS
24 Hours
AWS DevOps CertificationMicrosoft
16 Hours
Azure Fundamentals CertificationMicrosoft
24 Hours
Best Seller
Azure Administrator CertificationMicrosoft
45 Hours
Recommended
Azure Data Engineer CertificationMicrosoft
32 Hours
Azure Solution Architect CertificationMicrosoft
40 Hours
Azure DevOps CertificationAWS
24 Hours
Systems Operations on AWS Certification TrainingAWS
24 Hours
Developing on AWSMasters
Job Oriented
48 Hours
New
AWS Cloud Architect Masters ProgramBootcamps
Career Kickstarter
100 Hours
Trending
Cloud Engineer BootcampRoles
Cloud EngineerOn-Demand Courses
AWS Certified Developer Associate - Complete GuideAuthorized Partners of
AWSTop Resources
Scrum TutorialIT Service Management
Understand how to plan, design, and optimize IT services efficiently.
View All DevOps Coursesicon-git-commitCertifications
Axelos
16 Hours
Best Seller
ITIL 4 Foundation CertificationAxelos
16 Hours
ITIL Practitioner CertificationPeopleCert
16 Hours
ISO 14001 Foundation CertificationPeopleCert
16 Hours
ISO 20000 CertificationPeopleCert
24 Hours
ISO 27000 Foundation CertificationAxelos
24 Hours
ITIL 4 Specialist: Create, Deliver and Support TrainingAxelos
24 Hours
ITIL 4 Specialist: Drive Stakeholder Value TrainingAxelos
16 Hours
ITIL 4 Strategist Direct, Plan and Improve TrainingOn-Demand Courses
ITIL 4 Specialist: Create, Deliver and Support ExamTop Resources
ITIL Practice TestData Science
Unlock valuable insights from data with advanced analytics.
View All Data Science Coursesicon-dataBootcamps
Job Oriented
6 Months
Trending
Data Science BootcampJob Oriented
289 Hours
Data Engineer BootcampJob Oriented
6 Months
Data Analyst BootcampJob Oriented
288 Hours
New
AI Engineer BootcampSkills
Data Science with PythonRoles
Data ScientistOn-Demand Courses
Data Analysis Using ExcelTop Resources
Machine Learning TutorialDevOps
Automate and streamline the delivery of products and services.
View All DevOps Coursesicon-terminal-squareCertifications
DevOps Institute
16 Hours
Best Seller
DevOps Foundation CertificationCNCF
32 Hours
New
Certified Kubernetes AdministratorDevops Institute
16 Hours
Devops LeaderSkills
KubernetesRoles
DevOps EngineerOn-Demand Courses
CI/CD with Jenkins XGlobal Accreditations
DevOps InstituteTop Resources
Top DevOps ProjectsBI And Visualization
Understand how to transform data into actionable, measurable insights.
View All BI And Visualization Coursesicon-microscopeBI and Visualization Tools
Certification
24 Hours
Recommended
Tableau CertificationCertification
24 Hours
Data Visualization with Tableau CertificationMicrosoft
24 Hours
Best Seller
Microsoft Power BI CertificationTIBCO
36 Hours
TIBCO Spotfire TrainingCertification
30 Hours
Data Visualization with QlikView CertificationCertification
16 Hours
Sisense BI CertificationOn-Demand Courses
Data Visualization Using Tableau TrainingTop Resources
Python Data Viz LibsCyber Security
Understand how to protect data and systems from threats or disasters.
View All Cyber Security Coursesicon-refresh-cwCertifications
CompTIA
40 Hours
Best Seller
CompTIA Security+EC-Council
40 Hours
Certified Ethical Hacker (CEH v12) CertificationISACA
22 Hours
Certified Information Systems Auditor (CISA) CertificationISACA
40 Hours
Certified Information Security Manager (CISM) Certification(ISC)²
40 Hours
Certified Information Systems Security Professional (CISSP)(ISC)²
40 Hours
Certified Cloud Security Professional (CCSP) Certification16 Hours
Certified Information Privacy Professional - Europe (CIPP-E) CertificationISACA
16 Hours
COBIT5 Foundation16 Hours
Payment Card Industry Security Standards (PCI-DSS) CertificationOn-Demand Courses
CISSPTop Resources
Laptops for IT SecurityWeb Development
Learn to create user-friendly, fast, and dynamic web applications.
View All Web Development Coursesicon-codeBootcamps
Career Kickstarter
6 Months
Best Seller
Full-Stack Developer BootcampJob Oriented
3 Months
Best Seller
UI/UX Design BootcampEnterprise Recommended
6 Months
Java Full Stack Developer BootcampCareer Kickstarter
490+ Hours
Front-End Development BootcampCareer Accelerator
4 Months
Backend Development Bootcamp (Node JS)Skills
ReactOn-Demand Courses
Angular TrainingTop Resources
Top HTML ProjectsBlockchain
Understand how transactions and databases work in blockchain technology.
View All Blockchain Coursesicon-stop-squareBlockchain Certifications
40 Hours
Blockchain Professional Certification32 Hours
Blockchain Solutions Architect Certification32 Hours
Blockchain Security Engineer Certification24 Hours
Blockchain Quality Engineer Certification5+ Hours
Blockchain 101 CertificationOn-Demand Courses
NFT Essentials 101: A Beginner's GuideTop Resources
Blockchain Interview QsProgramming
Learn to code efficiently and design software that solves problems.
View All Programming Coursesicon-codeSkills
Python CertificationInterview Prep
Career Accelerator
3 Months
Software Engineer Interview PrepOn-Demand Courses
Data Structures and Algorithms with JavaScriptTop Resources
Python TutorialSet is another collection data type in Python. However, it is not sequence, such as List or Tuple. It is an unordered collection of items. Hence, its constituent items don’t follow zero based positional index. Set also doesn’t allow duplicate objects to be stored, where as in List and Tuple, same object can appear more than once.
Set theory is an Important branch of Mathematics. Set data type is a Python implementation of set as defined in Mathematics. The built-in Set class has defined in it lot of suitable methods to perform mathematical set operations like union, intersection, difference etc.
A Set object contains one or more items, which may be of same or different type, separated by comma and enclosed in curly brackets {}. As mentioned earlier, Set doesn’t store duplicate objects. So, even if same object is put more than once inside curly brackets, only one copy appears in the object.
In [1]:
s1={10,34,32,11,25}
s1
Out[1]:
{10, 11, 25, 32, 34}
In [2]:
s1={100, "Hundred", 10.25, 10.100+5j}
s1
Out[2]:
{(10.1+5j), 10.25, 100, 'Hundred'}
The set object can store a collection of only immutable objects such as number, string and tuple. If we try to include a list or dict object in curly brackets, Python raises TypeError saying that these are unhashable.
Hashing is a mechanism in computer science which enables quicker searching of objects in computer’s memory. Python interpreter uses its own hashing algorithm to store items in set. As a result, items may not appear in the same sequence as used in its literal representation. Python optimizes the structure for improved performance of set operations.
In [3]:
s2={{1:11,2:22},(10,20),{1,2,3}}
s2
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-1ae95440415b> in <module>()
----> 1 s2={{1:11,2:22},(10,20),{1,2,3}}
2 s2
TypeError: unhashable type: 'dict'
Python has an in-built function set() using which set object can be constructed out of any sequence like string, list or tuple object. In fact this function acts as constructor of built-in set class.When used without arguments, set() produces empty set.
In [4]:
s2=tuple(range(5))
s2
Out[4]:
(0, 1, 2, 3, 4)
In [5]:
s2=set()
s2
Out[5]:
set()
In [6]:
s3=set("Hello World")
s3
Out[6]:
{' ', 'H', 'W', 'd', 'e', 'l', 'o', 'r'}
In [7]:
s4=set([54,23,45,11,23,76,11])
s4
Out[7]:
{11, 23, 45, 54, 76}
Even though mutable objects are not stored in a set, set itself is a mutable object. A set object can be modified by add(), update(), remove() and discard() methods.
| add() | Add an element to a set.This has no effect if the element is already present. |
| pop() | Remove and return an arbitrary set element.RaisesKeyError if the set is empty. |
| remove() | Remove an element from a set.If the element is not a member, raise a KeyError. |
| discard() | Remove an element from a set if it is a member.If the element is not a member, do nothing. |
| update() | Update a set with the union of itself and others. |
In [8]:
s1={"Django", "Flask", "Pyramid"}
s1
Out[8]:
{'Django', 'Flask', 'Pyramid'}
In [9]:
s1.add("Turbogears")
s1
Out[9]:
{'Django', 'Flask', 'Pyramid', 'Turbogears'}
In [10]:
s2={"NumPy", "SciPy","IPython"}
s1.update(s2)
s1
Out[10]:
{'Django', 'Flask', 'IPython', 'NumPy', 'Pyramid', 'SciPy', 'Turbogears'}
The remove(), pop() and discard() methods drop an item from the set. pop() method drops a random item whereas remove() and discard() need an item to be dropped as argument. If not present in the set, remove() method raises KeyError exception.
In [11]:
s1.remove('SciPy')
s1
Out[11]:
{'Django', 'Flask', 'IPython', 'NumPy', 'Pyramid', 'Turbogears'}
In [12]:
s1.remove("Kivy")
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-12-29217ddff50a> in <module>()
----> 1 s1.remove("Kivy")
KeyError: 'Kivy'
In [13]:
s1.pop()
Out[13]:
'Flask'
In [14]:
s1
Out[14]:
{'Django', 'IPython', 'NumPy', 'Pyramid', 'Turbogears'}
In [15]:
s1.discard("Flask")
In [16]:
s1
Out[16]:
{'Django', 'IPython', 'NumPy', 'Pyramid', 'Turbogears'}
As mentioned earlier, set data type in Python implements set as defined in mathematics. Various Set operations like union, intersection, difference and symmetric difference can be performed using Python’s set object.
Set operations can be summarized by following table:
Union | Union of two sets is a set of all elements in both. |
Intersection | Intersection of two sets is a set containing elements common to both |
Difference | Difference of two sets results in a set containing elements only in first but not in second set. |
Symmetric difference | Result of Symmetric difference is a set consisting of elements in both sets excluding common elements |
Following methods in set class perform above operations:
difference() | Return the difference of two or more sets as a new set. |
difference_update() | Remove all elements of another set from this set. |
intersection() | Return the intersection of two sets as a new set. |
intersection_update() | Update a set with the intersection of itself and another. |
symmetric_difference() | Return the symmetric difference of two sets as a new set. |
symmetric_difference_update() | Update a set with the symmetric difference of itself and another. |
union() | Return the union of sets as a new set |
In [30]:
s1={10,20,30,40,50}
s2=set('abcde')
s3=s1.union(s2)
s3
Out[30]:
{10, 20, 30, 40, 50, 'a', 'b', 'c', 'd', 'e'}
In [31]:
s1={10,20,30,40,50}
s2={0,20,40,60}
s3=s1.intersection(s2)
s3
Out[31]:
{20, 40}
In [32]:
s1={10,20,30,40,50}
s2={0,20,40,60}
s3=s1.difference(s2)
s3
Out[32]:
{10, 30, 50}
In [33]:
s1={10,20,30,40,50}
s2={0,20,40,60}
s3=s1.symmetric_difference(s2)
s3
Out[33]:
{0, 10, 30, 50, 60}
Above methods (union, intersection, difference and symmetric_difference) return a new set object. As against these methods intersection_update, difference_update and symmetric_difference_update perform their respective operations in place. It means s1.intersection_update(s2) will change s1 to the result of intersection with s2.
In [34]:
s1={10,20,30,40,50}
s2={20,40}
s1.difference_update(s2)
s1
Out[34]:
{10, 30, 50}
In [35]:
s1={10,20,30,40,50}
s2={0,20,40,60}
s1.symmetric_difference_update(s2)
s1
Out[35]:
{0, 10, 30, 50, 60}
In [36]:
s1={10,20,30,40,50}
s2={0,20,40,60}
s1.intersection_update(s2)
s1
Out[36]:
{20, 40}
Following methods decide whether a set is a super set, sub set or two are disjoint.
isdisjoint() | Return True if two sets have a null intersection. |
issubset() | Report whether another set contains this set. |
issuperset() | Report whether this set contains another set. |
In [37]:
s1={10,20,30,40,50}
s2={20,40}
s2.issubset(s1)
Out[37]:
True
In [38]:
s1.issuperset(s2)
Out[38]:
True
In [39]:
s1.isdisjoint(s2)
Out[39]:
False
In [40]:
s2.isdisjoint(s1)
Out[40]:
False
clear() method drops all items in the set keeping object intact. The del keyword on the other hand removes object from memory.
In [41]:
s1.clear()
s1
Out[41]:
set()
In [42]:
del s2
s2
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-42-9b4ca538b1cf> in <module>()
1 del s2
----> 2 s2
NameError: name 's2' is not defined
A frozenset is different from set in the sense that it is immutable. It means that any operations that change the composition of frozenset object will raise exception.
In [46]:
s1=frozenset(["Django", "Flask", "Pyramid"])
s1.add("TurboGears")
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-46-bfd9ac8ee662> in <module>()
1 s1=frozenset(["Django", "Flask", "Pyramid"])
----> 2 s1.add("TurboGears")
AttributeError: 'frozenset' object has no attribute 'add'
In [47]:
s1.pop()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-47-095118de218b> in <module>()
----> 1 s1.pop()
AttributeError: 'frozenset' object has no attribute 'pop'
However, set operations such as union, intersection etc. can be performed on frozenset object.