SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 
Python programming : Files
Python programming : FilesPython programming : Files
Python programming : Files
 
Strings in python
Strings in pythonStrings in python
Strings in python
 
Python list
Python listPython list
Python list
 
Class 3 - PHP Functions
Class 3 - PHP FunctionsClass 3 - PHP Functions
Class 3 - PHP Functions
 
Python : Data Types
Python : Data TypesPython : Data Types
Python : Data Types
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
Dictionaries and Sets in Python
Dictionaries and Sets in PythonDictionaries and Sets in Python
Dictionaries and Sets in Python
 
Php array
Php arrayPhp array
Php array
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
 
Python set
Python setPython set
Python set
 
Tuples in Python
Tuples in PythonTuples in Python
Tuples in Python
 
What are variables and keywords in c++
What are variables and keywords in c++What are variables and keywords in c++
What are variables and keywords in c++
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Dictionaries in Python
Dictionaries in PythonDictionaries in Python
Dictionaries in Python
 
Basic i/o & file handling in java
Basic i/o & file handling in javaBasic i/o & file handling in java
Basic i/o & file handling in java
 
Namespaces
NamespacesNamespaces
Namespaces
 
Python-03| Data types
Python-03| Data typesPython-03| Data types
Python-03| Data types
 

Ähnlich wie Dictionaries and Sets in Python

Generics Collections
Generics CollectionsGenerics Collections
Generics Collections
phanleson
 

Ähnlich wie Dictionaries and Sets in Python (20)

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
 
Generics collections
Generics collectionsGenerics collections
Generics collections
 

Kürzlich hochgeladen

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
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
QucHHunhnh
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Kürzlich hochgeladen (20)

Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
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
 
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"
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
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
 
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
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
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
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
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
 

Dictionaries and Sets in Python