SlideShare a Scribd company logo
1 of 29
.orginfo@msbacademy.org
Python
Dictionaries and Sets
.orginfo@msbacademy.org
Dictionary
1. A dictionary is an object that stores a collection of data.
2. Each element in a dictionary has two parts: a key and a value.
3. You use a key to locate a specific value.
.orginfo@msbacademy.org
Creating a Dictionary
Colon
msb_academy = {‘students':‘60', ‘systems':‘30'}
dictionary_name Key value Comma
.orginfo@msbacademy.org
Retrieving a Value
To retrieve a value from a dictionary, you simply write an
expression in the following general format
Syntax:
dictionary_name[key]
.orginfo@msbacademy.org
Test for a Value
Using the in and not in Operators to Test for a Value in Dictionary
Ex: msb_academy = {'students':'60', 'systems':'30', 'chairs':'90'}
if 'students‘ in msb_academy:
print(msb_academy['students'])
.orginfo@msbacademy.org
Adding Elements
Adding Elements to an Existing Dictionary
Ex:
msb_academy = {'students':'60', 'systems':'30', 'chairs':'90'}
msb_academy['courses'] = '20'
msb_academy['students'] = '50'
NOTE: You cannot have duplicate keys in a dictionary. When you
assign a value to an existing key, the new value replaces the
existing value.
.orginfo@msbacademy.org
Deleting Elements
You can delete an existing key-value pair from a dictionary
with the del statement. Here is the general format:
Syntax:
del dictionary_name[key]
.orginfo@msbacademy.org
Getting the Number of Elements
You can use the built-in “len” function to get the number of
elements in a dictionary
.orginfo@msbacademy.org
Mixing Data Types
The keys in a dictionary must be immutable objects, but their
associated values can be any type of object
.orginfo@msbacademy.org
Creating an Empty Dictionary
Sometimes you need to create an empty dictionary and then add
elements to it as the program executes. You can use an empty
set of curly braces to create an empty dictionary
.orginfo@msbacademy.org
Using the for loop to Iterate over
Using the for loop to Iterate over a Dictionary
You can use the for loop in the following general format to iterate
over all the keys in a dictionary
for var in dictionary:
statement
statement
etc.
.orginfo@msbacademy.org
Some Dictionary Methods
1. Clear
2. Get
3. Items
4. Keys
5. Pop
6. Popitem
7. values
.orginfo@msbacademy.org
Clear method
The clear method deletes all the elements in a dictionary,
leaving the dictionary empty. The method’s general format is
Syntax:
dictionary.clear()
.orginfo@msbacademy.org
Get method
You can use the get method as an alternative to the [ ]
operator for getting a value from a dictionary.
Syntax:
dictionary.get(key, default)
.orginfo@msbacademy.org
Items method
The items method returns all of a dictionary’s keys and their
associated values. They are returned as a special type of
sequence known as a dictionary view.
Syntax:
dictionary.items()
.orginfo@msbacademy.org
Keys method
The keys method returns all of a dictionary’s keys as a
dictionary view, which is a type of sequence. Each element in the
dictionary view is a key from the dictionary.
Syntax:
dictionary.key()
.orginfo@msbacademy.org
Pop method
The pop method returns the value associated with a
specified key and removes that key value pair from the dictionary.
If the key is not found, the method returns a default value
Syntax:
dictionary.pop(key, default)
.orginfo@msbacademy.org
Popitem method
The popitem method returns a randomly selected key-value
pair, and it removes that key value pair from the dictionary. The
key-value pair is returned as a tuple
Syntax: dictionary.popitem()
You can use an assignment statement in the following
general format to assign the returned key and value to individual
variables
Syntax: k, v = dictionary.popitem()
.orginfo@msbacademy.org
Set
A set is an object that stores a collection of data in the same way
as mathematical sets.
1. All the elements in a set must be unique. No two elements
can have the same value.
2. Sets are unordered, which means that the elements in a set
are not stored in any particular order.
3. The elements that are stored in a set can be of different data
types
.orginfo@msbacademy.org
Creating a Set
To create a set, you have to call the built-in set function
Ex:
msb_set = set(['a', 'b', 'c'])
.orginfo@msbacademy.org
Getting the Number of Elements
As with lists, tuples, and dictionaries, you can use the “len”
function to get the number of elements in a set
.orginfo@msbacademy.org
Adding and Removing Elements
Sets are mutable objects, so you can add items to them
and remove items from them. You use the add method to add an
element to a set
.orginfo@msbacademy.org
Iterate over a Set
You can use the for loop in the following general format to iterate
over all the elements in a set:
for var in set:
statement
statement
etc.
.orginfo@msbacademy.org
Finding the Union of Sets
The union of two sets is a set that contains all the elements
of both sets. In Python, you can call the union method to get the
union of two sets
Syntax:
set1.union(set2)
.orginfo@msbacademy.org
Finding the Intersection of Sets
The intersection of two sets is a set that contains only the
elements that are found in both sets. In Python, you can call the
intersection method to get the intersection of two sets.
Syntax:
set1.intersection(set2)
.orginfo@msbacademy.org
Finding the Difference of Sets
The difference of set1 and set2 are the elements that
appear in set1 but do not appear in set2. In Python, you can call
the difference method to get the difference of two sets
Syntax:
set1.difference(set2)
.orginfo@msbacademy.org
Finding the Symmetric Difference of Sets
The symmetric difference of two sets is a set that contains
the elements that are not shared by the sets. In other words, it is
the elements that are in one set but not in both. In Python, you
can call the symmetric_difference method to get the symmetric
difference of two sets
Syntax:
set1.symmetric_difference(set2)
.orginfo@msbacademy.org
Finding Subsets and Supersets
Suppose you have two sets and one of those sets contains all of
the elements of the other set
Ex: set1 = set([1, 2, 3, 4])
set2 = set([2, 3])
set2.issubset(set1)
set1.issuperset(set2)
.orginfo@msbacademy.org
Follow Us:
/msbacademy/
/channel/UCqqRY7gXj0k52Fg9Q3BBgpg
/msb_academy/
Thank You

More Related Content

What's hot (20)

Dictionaries in Python
Dictionaries in PythonDictionaries in Python
Dictionaries in Python
 
Python - object oriented
Python - object orientedPython - object oriented
Python - object oriented
 
Java script arrays
Java script arraysJava script arrays
Java script arrays
 
This pointer
This pointerThis pointer
This pointer
 
Python dictionary
Python dictionaryPython dictionary
Python dictionary
 
Python set
Python setPython set
Python set
 
Java String
Java String Java String
Java String
 
Aggregate functions
Aggregate functionsAggregate functions
Aggregate functions
 
Javascript functions
Javascript functionsJavascript functions
Javascript functions
 
C++ string
C++ stringC++ string
C++ string
 
Python : Regular expressions
Python : Regular expressionsPython : Regular expressions
Python : Regular expressions
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
 
JavaScript Objects
JavaScript ObjectsJavaScript Objects
JavaScript Objects
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
javascript objects
javascript objectsjavascript objects
javascript objects
 
For Beginers - ADO.Net
For Beginers - ADO.NetFor Beginers - ADO.Net
For Beginers - ADO.Net
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
Function overloading
Function overloadingFunction overloading
Function overloading
 

Similar to Dictionaries and Sets in Python

Dictionaries and Sets in Python
Dictionaries and Sets in PythonDictionaries and Sets in Python
Dictionaries and Sets in PythonMSB Academy
 
Collections in Python - Where Data Finds Its Perfect Home.pdf
Collections in Python - Where Data Finds Its Perfect Home.pdfCollections in Python - Where Data Finds Its Perfect Home.pdf
Collections in Python - Where Data Finds Its Perfect Home.pdfSudhanshiBakre1
 
Collections generic
Collections genericCollections generic
Collections genericsandhish
 
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...vekariyakashyap
 
Week 10.pptx
Week 10.pptxWeek 10.pptx
Week 10.pptxCruiseCH
 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Threeamiable_indian
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfacesmadhavi patil
 
Lecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and CollectionsLecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and CollectionsSyed Afaq Shah MACS CP
 
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲Mohammad Reza Kamalifard
 
Kripanshu MOOC PPT - Kripanshu Shekhar Jha (1).pptx
Kripanshu MOOC PPT - Kripanshu Shekhar Jha (1).pptxKripanshu MOOC PPT - Kripanshu Shekhar Jha (1).pptx
Kripanshu MOOC PPT - Kripanshu Shekhar Jha (1).pptxsg4795
 
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونیاسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونیMohammad Reza Kamalifard
 
Chapter 14 Dictionary.pptx
Chapter 14 Dictionary.pptxChapter 14 Dictionary.pptx
Chapter 14 Dictionary.pptxjchandrasekhar3
 
Generics Collections
Generics CollectionsGenerics Collections
Generics Collectionsphanleson
 
Python Interview Questions And Answers
Python Interview Questions And AnswersPython Interview Questions And Answers
Python Interview Questions And AnswersH2Kinfosys
 

Similar to Dictionaries and Sets in Python (20)

Dictionaries and Sets in Python
Dictionaries and Sets in PythonDictionaries and Sets in Python
Dictionaries and Sets in Python
 
Collections in Python - Where Data Finds Its Perfect Home.pdf
Collections in Python - Where Data Finds Its Perfect Home.pdfCollections in Python - Where Data Finds Its Perfect Home.pdf
Collections in Python - Where Data Finds Its Perfect Home.pdf
 
.net F# mutable dictionay
.net F# mutable dictionay.net F# mutable dictionay
.net F# mutable dictionay
 
Collections generic
Collections genericCollections generic
Collections generic
 
Spsl vi unit final
Spsl vi unit finalSpsl vi unit final
Spsl vi unit final
 
Spsl v unit - final
Spsl v unit - finalSpsl v unit - final
Spsl v unit - final
 
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
 
Dictionaries and Sets
Dictionaries and SetsDictionaries and Sets
Dictionaries and Sets
 
Week 10.pptx
Week 10.pptxWeek 10.pptx
Week 10.pptx
 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Three
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 
Python Sets_Dictionary.pptx
Python Sets_Dictionary.pptxPython Sets_Dictionary.pptx
Python Sets_Dictionary.pptx
 
Lecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and CollectionsLecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and Collections
 
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
Kripanshu MOOC PPT - Kripanshu Shekhar Jha (1).pptx
Kripanshu MOOC PPT - Kripanshu Shekhar Jha (1).pptxKripanshu MOOC PPT - Kripanshu Shekhar Jha (1).pptx
Kripanshu MOOC PPT - Kripanshu Shekhar Jha (1).pptx
 
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونیاسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
 
Chapter 14 Dictionary.pptx
Chapter 14 Dictionary.pptxChapter 14 Dictionary.pptx
Chapter 14 Dictionary.pptx
 
Generics Collections
Generics CollectionsGenerics Collections
Generics Collections
 
Python Interview Questions And Answers
Python Interview Questions And AnswersPython Interview Questions And Answers
Python Interview Questions And Answers
 

Recently uploaded

Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxsqpmdrvczh
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxLigayaBacuel1
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 

Recently uploaded (20)

Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptx
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 

Dictionaries and Sets in Python