SlideShare a Scribd company logo
msbacademy.org
Python
Dictionaries and Sets
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.
msbacademy.org
Creating a Dictionary
Colon
msb_dict = {‘students':‘60', ‘systems':‘30'}
dictionary_name Key value Comma
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]
msbacademy.org
Test for a Value
Using the in and not in Operators to Test for a Value in Dictionary
Ex: msb_dict = {'students':'60', 'systems':'30', 'chairs':'90'}
if 'students‘ in msb_dict:
print(msb_dict['students'])
msbacademy.org
Adding Elements
Adding Elements to an Existing Dictionary
Ex:
msb_dict = {'students':'60', 'systems':'30', 'chairs':'90'}
msb_dict['courses'] = '20'
msb_dict['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.
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]
msbacademy.org
Getting the Number of Elements
You can use the built-in “len” function to get the number of
elements in a dictionary
msbacademy.org
Mixing Data Types
The keys in a dictionary must be immutable objects, but their
associated values can be any type of object
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
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.
msbacademy.org
Some Dictionary Methods
1. Clear
2. Get
3. Items
4. Keys
5. Pop
6. Popitem
7. values
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()
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)
msbacademy.org
Items method
The items method returns all of a dictionaries keys and
their associated values. They are returned as a special type of
sequence known as a dictionary view.
Syntax:
dictionary.items()
msbacademy.org
Keys method
The keys method returns all of a dictionaries 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()
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)
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()
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
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'])
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
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
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.
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)
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)
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)
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)
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)
msbacademy.org
Follow Us:
/msbacademy
/msb academy
/msb_academy
/msb_academy
Thank You

More Related Content

What's hot

Python list
Python listPython list
Python list
Mohammed Sikander
 
Python : Functions
Python : FunctionsPython : Functions
Functions in python
Functions in pythonFunctions in python
Functions in python
colorsof
 
Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| Fundamentals
Mohd Sajjad
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
TMARAGATHAM
 
Python Modules
Python ModulesPython Modules
Python Modules
Nitin Reddy Katkam
 
Date and Time Module in Python | Edureka
Date and Time Module in Python | EdurekaDate and Time Module in Python | Edureka
Date and Time Module in Python | Edureka
Edureka!
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
Celine George
 
Python tuple
Python   tuplePython   tuple
Python tuple
Mohammed Sikander
 
What is Tuple in python? | Python Tuple Tutorial | Edureka
What is Tuple in python? | Python Tuple Tutorial | EdurekaWhat is Tuple in python? | Python Tuple Tutorial | Edureka
What is Tuple in python? | Python Tuple Tutorial | Edureka
Edureka!
 
Chapter 17 Tuples
Chapter 17 TuplesChapter 17 Tuples
Chapter 17 Tuples
Praveen M Jigajinni
 
Python programming : List and tuples
Python programming : List and tuplesPython programming : List and tuples
Python programming : List and tuples
Emertxe Information Technologies Pvt Ltd
 
Java Lambda Expressions.pptx
Java Lambda Expressions.pptxJava Lambda Expressions.pptx
Java Lambda Expressions.pptx
SameerAhmed593310
 
List in Python
List in PythonList in Python
List in Python
Siddique Ibrahim
 
This pointer
This pointerThis pointer
This pointer
Kamal Acharya
 
Class, object and inheritance in python
Class, object and inheritance in pythonClass, object and inheritance in python
Class, object and inheritance in python
Santosh Verma
 
Tuples in Python
Tuples in PythonTuples in Python
Tuples in Python
DPS Ranipur Haridwar UK
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
Mohammed Sikander
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
Praveen M Jigajinni
 
Chapter 16 Dictionaries
Chapter 16 DictionariesChapter 16 Dictionaries
Chapter 16 Dictionaries
Praveen M Jigajinni
 

What's hot (20)

Python list
Python listPython list
Python list
 
Python : Functions
Python : FunctionsPython : Functions
Python : Functions
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
Python-01| Fundamentals
Python-01| FundamentalsPython-01| Fundamentals
Python-01| Fundamentals
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
 
Python Modules
Python ModulesPython Modules
Python Modules
 
Date and Time Module in Python | Edureka
Date and Time Module in Python | EdurekaDate and Time Module in Python | Edureka
Date and Time Module in Python | Edureka
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
Python tuple
Python   tuplePython   tuple
Python tuple
 
What is Tuple in python? | Python Tuple Tutorial | Edureka
What is Tuple in python? | Python Tuple Tutorial | EdurekaWhat is Tuple in python? | Python Tuple Tutorial | Edureka
What is Tuple in python? | Python Tuple Tutorial | Edureka
 
Chapter 17 Tuples
Chapter 17 TuplesChapter 17 Tuples
Chapter 17 Tuples
 
Python programming : List and tuples
Python programming : List and tuplesPython programming : List and tuples
Python programming : List and tuples
 
Java Lambda Expressions.pptx
Java Lambda Expressions.pptxJava Lambda Expressions.pptx
Java Lambda Expressions.pptx
 
List in Python
List in PythonList in Python
List in Python
 
This pointer
This pointerThis pointer
This pointer
 
Class, object and inheritance in python
Class, object and inheritance in pythonClass, object and inheritance in python
Class, object and inheritance in python
 
Tuples in Python
Tuples in PythonTuples in Python
Tuples in Python
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
 
Chapter 16 Dictionaries
Chapter 16 DictionariesChapter 16 Dictionaries
Chapter 16 Dictionaries
 

Similar to Dictionaries and Sets in Python

Python Sets_Dictionary.pptx
Python Sets_Dictionary.pptxPython Sets_Dictionary.pptx
Python Sets_Dictionary.pptx
M Vishnuvardhan Reddy
 
Dictionaries and Sets
Dictionaries and SetsDictionaries and Sets
Dictionaries and Sets
Munazza-Mah-Jabeen
 
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
SudhanshiBakre1
 
Chapter 14 Dictionary.pptx
Chapter 14 Dictionary.pptxChapter 14 Dictionary.pptx
Chapter 14 Dictionary.pptx
jchandrasekhar3
 
Spsl v unit - final
Spsl v unit - finalSpsl v unit - final
Spsl v unit - final
Sasidhar Kothuru
 
Spsl vi unit final
Spsl vi unit finalSpsl vi unit final
Spsl vi unit final
Sasidhar Kothuru
 
Week 10.pptx
Week 10.pptxWeek 10.pptx
Week 10.pptx
CruiseCH
 
Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
Mohammed Sikander
 
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
 
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونیاسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
Mohammad Reza Kamalifard
 
Ch 7 Dictionaries 1.pptx
Ch 7 Dictionaries 1.pptxCh 7 Dictionaries 1.pptx
Ch 7 Dictionaries 1.pptx
KanchanaRSVVV
 
ch 13 Dictionaries2.pptx
ch 13 Dictionaries2.pptxch 13 Dictionaries2.pptx
ch 13 Dictionaries2.pptx
sogarongt
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
madhavi patil
 
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
Mohammad Reza Kamalifard
 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Three
amiable_indian
 
Java Unit 2(part 3)
Java Unit 2(part 3)Java Unit 2(part 3)
Java Unit 2(part 3)
SURBHI SAROHA
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
ssuser8347a1
 
Lecture4_py………"………………………………………………..…………..
Lecture4_py………"………………………………………………..…………..Lecture4_py………"………………………………………………..…………..
Lecture4_py………"………………………………………………..…………..
RoshanKumar419236
 
.net F# mutable dictionay
.net F# mutable dictionay.net F# mutable dictionay
.net F# mutable dictionay
DrRajeshreeKhande
 
Collections generic
Collections genericCollections generic
Collections generic
sandhish
 

Similar to Dictionaries and Sets in Python (20)

Python Sets_Dictionary.pptx
Python Sets_Dictionary.pptxPython Sets_Dictionary.pptx
Python Sets_Dictionary.pptx
 
Dictionaries and Sets
Dictionaries and SetsDictionaries and Sets
Dictionaries and Sets
 
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
 
Chapter 14 Dictionary.pptx
Chapter 14 Dictionary.pptxChapter 14 Dictionary.pptx
Chapter 14 Dictionary.pptx
 
Spsl v unit - final
Spsl v unit - finalSpsl v unit - final
Spsl v unit - final
 
Spsl vi unit final
Spsl vi unit finalSpsl vi unit final
Spsl vi unit final
 
Week 10.pptx
Week 10.pptxWeek 10.pptx
Week 10.pptx
 
Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
 
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...
 
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونیاسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
 
Ch 7 Dictionaries 1.pptx
Ch 7 Dictionaries 1.pptxCh 7 Dictionaries 1.pptx
Ch 7 Dictionaries 1.pptx
 
ch 13 Dictionaries2.pptx
ch 13 Dictionaries2.pptxch 13 Dictionaries2.pptx
ch 13 Dictionaries2.pptx
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Three
 
Java Unit 2(part 3)
Java Unit 2(part 3)Java Unit 2(part 3)
Java Unit 2(part 3)
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
 
Lecture4_py………"………………………………………………..…………..
Lecture4_py………"………………………………………………..…………..Lecture4_py………"………………………………………………..…………..
Lecture4_py………"………………………………………………..…………..
 
.net F# mutable dictionay
.net F# mutable dictionay.net F# mutable dictionay
.net F# mutable dictionay
 
Collections generic
Collections genericCollections generic
Collections generic
 

Recently uploaded

HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
Operating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptxOperating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptx
Pravash Chandra Das
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
dbms calicut university B. sc Cs 4th sem.pdf
dbms  calicut university B. sc Cs 4th sem.pdfdbms  calicut university B. sc Cs 4th sem.pdf
dbms calicut university B. sc Cs 4th sem.pdf
Shinana2
 
Trusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process MiningTrusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process Mining
LucaBarbaro3
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
Wouter Lemaire
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
Finale of the Year: Apply for Next One!
Finale of the Year: Apply for Next One!Finale of the Year: Apply for Next One!
Finale of the Year: Apply for Next One!
GDSC PJATK
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
akankshawande
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
MichaelKnudsen27
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 
A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024
Intelisync
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
kumardaparthi1024
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 

Recently uploaded (20)

HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
Operating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptxOperating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptx
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 
dbms calicut university B. sc Cs 4th sem.pdf
dbms  calicut university B. sc Cs 4th sem.pdfdbms  calicut university B. sc Cs 4th sem.pdf
dbms calicut university B. sc Cs 4th sem.pdf
 
Trusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process MiningTrusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process Mining
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
Finale of the Year: Apply for Next One!
Finale of the Year: Apply for Next One!Finale of the Year: Apply for Next One!
Finale of the Year: Apply for Next One!
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 
A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 

Dictionaries and Sets in Python

  • 2. 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.
  • 3. msbacademy.org Creating a Dictionary Colon msb_dict = {‘students':‘60', ‘systems':‘30'} dictionary_name Key value Comma
  • 4. 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]
  • 5. msbacademy.org Test for a Value Using the in and not in Operators to Test for a Value in Dictionary Ex: msb_dict = {'students':'60', 'systems':'30', 'chairs':'90'} if 'students‘ in msb_dict: print(msb_dict['students'])
  • 6. msbacademy.org Adding Elements Adding Elements to an Existing Dictionary Ex: msb_dict = {'students':'60', 'systems':'30', 'chairs':'90'} msb_dict['courses'] = '20' msb_dict['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.
  • 7. 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]
  • 8. msbacademy.org Getting the Number of Elements You can use the built-in “len” function to get the number of elements in a dictionary
  • 9. msbacademy.org Mixing Data Types The keys in a dictionary must be immutable objects, but their associated values can be any type of object
  • 10. 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
  • 11. 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.
  • 12. msbacademy.org Some Dictionary Methods 1. Clear 2. Get 3. Items 4. Keys 5. Pop 6. Popitem 7. values
  • 13. 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()
  • 14. 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)
  • 15. msbacademy.org Items method The items method returns all of a dictionaries keys and their associated values. They are returned as a special type of sequence known as a dictionary view. Syntax: dictionary.items()
  • 16. msbacademy.org Keys method The keys method returns all of a dictionaries 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()
  • 17. 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)
  • 18. 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()
  • 19. 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
  • 20. 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'])
  • 21. 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
  • 22. 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
  • 23. 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.
  • 24. 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)
  • 25. 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)
  • 26. 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)
  • 27. 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)
  • 28. 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)