際際滷

際際滷Share a Scribd company logo
DICTIONARY
Class XI
CHAPTER-13
Dictionary
Dictionary
It is an unordered collection of items where each
item consist of a key and a value.
It is mutable (can modify its contents ) but Key must be
unique and immutable.
COMPUTERSCIENCE (CLASS XI)
Dictionary
Creating A Dictionary
 It is enclosed in curly braces {} and each item is separated
from other item by a comma(,)
 Within each item, key and value are separated by a colon (:)
 Passing value in dictionary at declaration is dictionary
initialization.
 get() method is used to access value of a key
E.g.
dict = {Subject': Informatic Practices', 'Class': 11'}
Accessing List Item
dict = {'Subject': 'Informatics Practices', 'Class': 11}
print(dict)
print ("Subject : ", dict['Subject'])
dict.get('Class')
OUTPUT
{'Class': '11', 'Subject': 'Informatics Practices'}
('Subject : ', 'Informatics Practices')
11
Dictionary
Iterating / Traversing through A Dictionary
Following example will show how dictionary items can be accessed
through loop.
e.g.
dict = {'Subject': 'Informatics Practices', 'Class': 11}
for i in dict:
print(dict[i])
OUTPUT
Informatics Practices
11
Dictionary
Iterating / Traversing through A Dictionary
Following example will show how dictionary items can be accessed
through loop.
e.g.
dict = {'Subject': 'Informatics Practices', 'Class': 11}
for i in dict:
print(dict[i])
OUTPUT
Informatics Practices
11
Updating/Manipulating Dictionary Elements
We can change the individual element of
dictionary.
e.g.
dict = {'Subject': 'Informatics Practices', 'Class': 11}
dict['Subject']='computer science'
print(dict)
OUTPU
T
Dictionary
Questions
1. Create dictionary to store 4 student details
with rollno, name, age.
2. Create dictionary for month and no of days
for a year. User is asked to enter month
name and system will show no of days of
that month.
Dictionary
Visit : python.mykvs.in for regular
Deleting Dictionary Elements
del, pop() and clear() statement are used to remove
elements from the dictionary.
e.g. DEL
dict = {'Subject': 'Informatics Practices', 'Class': 11}
print('before del', dict)
del dict['Class'] # delete single element
print('after item delete', dict)
del dict #delete whole dictionary
print('after dictionary delete', type(dict))
Output
(before del {'Class': 11, 'Subject': 'Informatics Practices'})
(after item delete {'Subject': 'Informatics Practices'})
('after dictionary delete' <type 'dict'>)
Dictionary
pop() method is used to remove a particular item in a
dictionary.
clear() method is used to remove all elements from the dictionary.
e.g.
dict = {'Subject': 'Informatics Practices', 'Class': 11}
print('before del', dict)
dict.pop('Class')
print('after item delete', dict)
dict.clear()
print('after clear', dict)
Output
('before del' {'Class': 11, 'Subject': 'Informatics Practices'})
(after item delete {'Subject': 'Informatics Practices'})
('after clear' {})
Dictionary
Dictionary
MCQ
Q.1 What will be the output of the following?
d = {abc: 5, def: 6, ghi:7}
print(d1[abc]) # Line 1
print(d[0]) # Line 2
Q.2. Dictionaries are___________ data types of Python.
a) Mutable b)immutable c) simple d) none of these
Q.3. Which of the following can be used to delete item(s) from a
dictionary?
a) Del b) pop() c) popitem() d) all of these
Q.4. Dictionaries are also called_________
a) Mapping b) hashes c) associative arrays
d) all of these
Built-in Dictionary Functions
S.No. Function & Description
1 len(dict)----Gives the total length of the dictionary. It is equal to the
number of items in the dictionary.
dict = {'Name': 'Aman', 'Age': 37}
print (len (dict))
OUTPUT ->2
2 str(dict)-----Return a printable stringrepresentationof a Dictionary.
dict = {'Name': 'Aman', 'Age': 37}
>>> "{'Name': 'Aman', 'Age': 37}"
3 type(variable)If variable is dictionary, then it would return a
dictionary type.
dict = {'Name': 'Aman', 'Age': 37}
>>>type(dict)
Dictionary
Built-in Dictionary Methods
S.No. Method & Description
1 dict() - creates dictionary # Argument method
x = dict(name = Aman", age = 37, country = India")
Here x is created as dictionary
2
keys() - returns all the available keys
x = dict(name = Aman", age = 37, country = India")
print(x.keys())
OUTPUT->dict_keys(['country', 'age', 'name'])
3
values() - returns all the available values
x = dict(name = Aman", age = 37, country = India")
print(x.values())
OUTPUT->dict_values(['India', 37, 'Aman'])
Dictionary
S.No. Method & Description
4
items() - return the list with all dictionary keys with values.
x = dict(name = "Aman", age = 37, country = "India")
print(x.items())
OUTPUT->dict_items([('country', 'India'), ('age', 37), ('name',
'Aman')])
5
update()-used to change the values of a key and add new keys
x = dict(name = "Aman", age = 37, country =
India") d1 = dict(age= 39)
x.update(d1,state="Rajasthan
")
print(x)
OUTPUT-{'country': 'India', 'age': 39,'name':'Aman','state':
'Rajasthan'}
Dictionary
Built-in Dictionary Methods
S.No. Method & Description
6
del -used to remove key
x = dict(name = "Aman", age = 37, country = "India")
del x['age']
print(x)
OUTPUT->{'country': 'India', 'name': 'Aman'}
del x -> will remove complete dictionary
7
fromkeys()  is used to create dictionary from keys
keys = {'a', 'e', 'i', 'o', 'u' }
value ="Vowel"
vowels = dict.fromkeys(keys, value)
print(vowels)
OUTPUT-> {'i': 'Vowel', 'u': 'Vowel', 'e': 'Vowel', 'a': 'Vowel', 'o': 'Vowel'}
Dictionary
Built-in Dictionary Methods
S.No. Method & Description
8 copy() - returns a shallow copy of the dictionary.
x = dict(name = "Aman", age = 37, country = "India")
y=x.copy()
print(y)
print(id(x))
print(id(y))
OUTPUT - >{'country': 'India', 'age': 37, 'name': 'Aman'}
33047872
33047440
9 popitem()  removes last item from dictionary
x = dict(name = "Aman", age = 37, country = "India")
x.popitem()
print(x)
OUTPUT-> {'age': 37, 'name': 'Aman'}
Dictionary
Built-in Dictionary Methods
S.No. Method & Description
10 setdefault() method returns the value of the item with the specified
key. If the key does not exist, insert the key, with the specified value.
x = dict(name = "Aman", country = "India")
y=x.setdefault('age',39)
print(y)
OUTPUT->
39
11 max()  returns key having maximum value
Tv = {'a':100, 'b':1292, 'c' : 88}
Keymax = max(Tv, key=Tv.get)
print(Keymax)
OUTPUT-> b
12 min()- returns key having minimum value
Dict = {'Name': 'Aman', 'Age': 37}
print (min(Dict)
>>> Output - ?
Dictionary
Built-in Dictionary Methods
Dictionary
S.No. Method & Description
13 max( )- returns key having maximum value
Dict = {'Name': 'Aman', 'Age': 37}
print (max(Dict)
>>> Output - ?
14. sorted- sort by key or value
dict1 = {'b':100, 'a':12, 'c' : 88}
y = sorted(dict1.items(),key=lambda x:
x[1],reverse=True) print(y)
OUTPUT-> [('b', 100), ('c', 88), ('a',12)]
Dictionary
Built-in Dictionary Methods
Competency Based Question
stateCapital = {AndhraPradesh:Hyderabad, Bihar:Patna,
Maharashtra:Mumbai, Rajasthan:Jaipur}
Find output of the following:
1. print(stateCapital.get(Bihar))
2. print(stateCapital.keys()
3. print(stateCapital.values())
4. print(len(stateCapital))
5. del stateCapital [Andhra Pradesh]
print (stateCapital)
6. print(Maharashtra in stateCapital)
Dictionary
Visit : python.mykvs.in for regular
1. create a dictionary with names of
employees, their salary and access them
2. Count the number of times a character
appears in agiven string using a dictionary
Dictionary
Program:

More Related Content

Similar to dictionary14 ppt FINAL.pptx (20)

The Ring programming language version 1.9 book - Part 42 of 210
The Ring programming language version 1.9 book - Part 42 of 210The Ring programming language version 1.9 book - Part 42 of 210
The Ring programming language version 1.9 book - Part 42 of 210
Mahmoud Samir Fayed
The Ring programming language version 1.10 book - Part 43 of 212
The Ring programming language version 1.10 book - Part 43 of 212The Ring programming language version 1.10 book - Part 43 of 212
The Ring programming language version 1.10 book - Part 43 of 212
Mahmoud Samir Fayed
Using-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptxUsing-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptx
UadAccount
The Ring programming language version 1.7 book - Part 37 of 196
The Ring programming language version 1.7 book - Part 37 of 196The Ring programming language version 1.7 book - Part 37 of 196
The Ring programming language version 1.7 book - Part 37 of 196
Mahmoud Samir Fayed
Ruby tricks2
Ruby tricks2Ruby tricks2
Ruby tricks2
Micha omnicki
Session10_Dictionaries.ppggyyyyyyyyyggggggggtx
Session10_Dictionaries.ppggyyyyyyyyyggggggggtxSession10_Dictionaries.ppggyyyyyyyyyggggggggtx
Session10_Dictionaries.ppggyyyyyyyyyggggggggtx
pawankamal3
Beginner's Python Cheat Sheet
Beginner's Python Cheat SheetBeginner's Python Cheat Sheet
Beginner's Python Cheat Sheet
Verxus
beginners_python_cheat_sheet_pcc_all (1).pdf
beginners_python_cheat_sheet_pcc_all (1).pdfbeginners_python_cheat_sheet_pcc_all (1).pdf
beginners_python_cheat_sheet_pcc_all (1).pdf
ElNew2
python cheat sheat, Data science, Machine learning
python cheat sheat, Data science, Machine learningpython cheat sheat, Data science, Machine learning
python cheat sheat, Data science, Machine learning
TURAGAVIJAYAAKASH
2. Python Cheat Sheet.pdf
2. Python Cheat Sheet.pdf2. Python Cheat Sheet.pdf
2. Python Cheat Sheet.pdf
MeghanaDBengalur
Farhana shaikh webinar_dictionaries
Farhana shaikh webinar_dictionariesFarhana shaikh webinar_dictionaries
Farhana shaikh webinar_dictionaries
Farhana Shaikh
Python : Functions
Python : FunctionsPython : Functions
Python : Functions
Emertxe Information Technologies Pvt Ltd
Unit4-Basic Concepts and methods of Dictionary.pptx
Unit4-Basic Concepts and methods of Dictionary.pptxUnit4-Basic Concepts and methods of Dictionary.pptx
Unit4-Basic Concepts and methods of Dictionary.pptx
SwapnaliGawali5
Python cheatsheet for beginners
Python cheatsheet for beginnersPython cheatsheet for beginners
Python cheatsheet for beginners
Lahore Garrison University
1. python
1. python1. python
1. python
PRASHANT OJHA
Beginners python cheat sheet - Basic knowledge
Beginners python cheat sheet - Basic knowledge Beginners python cheat sheet - Basic knowledge
Beginners python cheat sheet - Basic knowledge
O T
Python dictionaries
Python dictionariesPython dictionaries
Python dictionaries
Krishna Nanda
Python programming part 7
Python programming part 7Python programming part 7
Python programming part 7
Megha V
The Ring programming language version 1.7 book - Part 35 of 196
The Ring programming language version 1.7 book - Part 35 of 196The Ring programming language version 1.7 book - Part 35 of 196
The Ring programming language version 1.7 book - Part 35 of 196
Mahmoud Samir Fayed
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
Yashpatel821746
The Ring programming language version 1.9 book - Part 42 of 210
The Ring programming language version 1.9 book - Part 42 of 210The Ring programming language version 1.9 book - Part 42 of 210
The Ring programming language version 1.9 book - Part 42 of 210
Mahmoud Samir Fayed
The Ring programming language version 1.10 book - Part 43 of 212
The Ring programming language version 1.10 book - Part 43 of 212The Ring programming language version 1.10 book - Part 43 of 212
The Ring programming language version 1.10 book - Part 43 of 212
Mahmoud Samir Fayed
Using-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptxUsing-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptx
UadAccount
The Ring programming language version 1.7 book - Part 37 of 196
The Ring programming language version 1.7 book - Part 37 of 196The Ring programming language version 1.7 book - Part 37 of 196
The Ring programming language version 1.7 book - Part 37 of 196
Mahmoud Samir Fayed
Session10_Dictionaries.ppggyyyyyyyyyggggggggtx
Session10_Dictionaries.ppggyyyyyyyyyggggggggtxSession10_Dictionaries.ppggyyyyyyyyyggggggggtx
Session10_Dictionaries.ppggyyyyyyyyyggggggggtx
pawankamal3
Beginner's Python Cheat Sheet
Beginner's Python Cheat SheetBeginner's Python Cheat Sheet
Beginner's Python Cheat Sheet
Verxus
beginners_python_cheat_sheet_pcc_all (1).pdf
beginners_python_cheat_sheet_pcc_all (1).pdfbeginners_python_cheat_sheet_pcc_all (1).pdf
beginners_python_cheat_sheet_pcc_all (1).pdf
ElNew2
python cheat sheat, Data science, Machine learning
python cheat sheat, Data science, Machine learningpython cheat sheat, Data science, Machine learning
python cheat sheat, Data science, Machine learning
TURAGAVIJAYAAKASH
2. Python Cheat Sheet.pdf
2. Python Cheat Sheet.pdf2. Python Cheat Sheet.pdf
2. Python Cheat Sheet.pdf
MeghanaDBengalur
Farhana shaikh webinar_dictionaries
Farhana shaikh webinar_dictionariesFarhana shaikh webinar_dictionaries
Farhana shaikh webinar_dictionaries
Farhana Shaikh
Unit4-Basic Concepts and methods of Dictionary.pptx
Unit4-Basic Concepts and methods of Dictionary.pptxUnit4-Basic Concepts and methods of Dictionary.pptx
Unit4-Basic Concepts and methods of Dictionary.pptx
SwapnaliGawali5
Beginners python cheat sheet - Basic knowledge
Beginners python cheat sheet - Basic knowledge Beginners python cheat sheet - Basic knowledge
Beginners python cheat sheet - Basic knowledge
O T
Python dictionaries
Python dictionariesPython dictionaries
Python dictionaries
Krishna Nanda
Python programming part 7
Python programming part 7Python programming part 7
Python programming part 7
Megha V
The Ring programming language version 1.7 book - Part 35 of 196
The Ring programming language version 1.7 book - Part 35 of 196The Ring programming language version 1.7 book - Part 35 of 196
The Ring programming language version 1.7 book - Part 35 of 196
Mahmoud Samir Fayed
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
Yashpatel821746

Recently uploaded (20)

APM People Interest Network Conference - Tim Lyons - The neurological levels ...
APM People Interest Network Conference - Tim Lyons - The neurological levels ...APM People Interest Network Conference - Tim Lyons - The neurological levels ...
APM People Interest Network Conference - Tim Lyons - The neurological levels ...
Association for Project Management
Year 10 The Senior Phase Session 3 Term 1.pptx
Year 10 The Senior Phase Session 3 Term 1.pptxYear 10 The Senior Phase Session 3 Term 1.pptx
Year 10 The Senior Phase Session 3 Term 1.pptx
mansk2
Digital Tools with AI for e-Content Development.pptx
Digital Tools with AI for e-Content Development.pptxDigital Tools with AI for e-Content Development.pptx
Digital Tools with AI for e-Content Development.pptx
Dr. Sarita Anand
Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...
Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...
Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...
sandynavergas1
FESTIVAL: SINULOG & THINGYAN-LESSON 4.pptx
FESTIVAL: SINULOG & THINGYAN-LESSON 4.pptxFESTIVAL: SINULOG & THINGYAN-LESSON 4.pptx
FESTIVAL: SINULOG & THINGYAN-LESSON 4.pptx
DanmarieMuli1
Kaun TALHA quiz Prelims - El Dorado 2025
Kaun TALHA quiz Prelims - El Dorado 2025Kaun TALHA quiz Prelims - El Dorado 2025
Kaun TALHA quiz Prelims - El Dorado 2025
Conquiztadors- the Quiz Society of Sri Venkateswara College
Principle and Practices of Animal Breeding || Boby Basnet
Principle and Practices of Animal Breeding || Boby BasnetPrinciple and Practices of Animal Breeding || Boby Basnet
Principle and Practices of Animal Breeding || Boby Basnet
Boby Basnet
Fuel part 1.pptx........................
Fuel part 1.pptx........................Fuel part 1.pptx........................
Fuel part 1.pptx........................
ksbhattadcm
How to Setup WhatsApp in Odoo 17 - Odoo 際際滷s
How to Setup WhatsApp in Odoo 17 - Odoo 際際滷sHow to Setup WhatsApp in Odoo 17 - Odoo 際際滷s
How to Setup WhatsApp in Odoo 17 - Odoo 際際滷s
Celine George
cervical spine mobilization manual therapy .pdf
cervical spine mobilization manual therapy .pdfcervical spine mobilization manual therapy .pdf
cervical spine mobilization manual therapy .pdf
SamarHosni3
EDL 290F Week 3 - Mountaintop Views (2025).pdf
EDL 290F Week 3  - Mountaintop Views (2025).pdfEDL 290F Week 3  - Mountaintop Views (2025).pdf
EDL 290F Week 3 - Mountaintop Views (2025).pdf
Liz Walsh-Trevino
How to Configure Flexible Working Schedule in Odoo 18 Employee
How to Configure Flexible Working Schedule in Odoo 18 EmployeeHow to Configure Flexible Working Schedule in Odoo 18 Employee
How to Configure Flexible Working Schedule in Odoo 18 Employee
Celine George
Kaun TALHA quiz Finals -- El Dorado 2025
Kaun TALHA quiz Finals -- El Dorado 2025Kaun TALHA quiz Finals -- El Dorado 2025
Kaun TALHA quiz Finals -- El Dorado 2025
Conquiztadors- the Quiz Society of Sri Venkateswara College
How to use Init Hooks in Odoo 18 - Odoo 際際滷s
How to use Init Hooks in Odoo 18 - Odoo 際際滷sHow to use Init Hooks in Odoo 18 - Odoo 際際滷s
How to use Init Hooks in Odoo 18 - Odoo 際際滷s
Celine George
POWERPOINT-PRESENTATION_DM-NO.017-S.2025.pptx
POWERPOINT-PRESENTATION_DM-NO.017-S.2025.pptxPOWERPOINT-PRESENTATION_DM-NO.017-S.2025.pptx
POWERPOINT-PRESENTATION_DM-NO.017-S.2025.pptx
MarilenQuintoSimbula
Database population in Odoo 18 - Odoo slides
Database population in Odoo 18 - Odoo slidesDatabase population in Odoo 18 - Odoo slides
Database population in Odoo 18 - Odoo slides
Celine George
Research & Research Methods: Basic Concepts and Types.pptx
Research & Research Methods: Basic Concepts and Types.pptxResearch & Research Methods: Basic Concepts and Types.pptx
Research & Research Methods: Basic Concepts and Types.pptx
Dr. Sarita Anand
A PPT on the First Three chapters of Wings of Fire
A PPT on the First Three chapters of Wings of FireA PPT on the First Three chapters of Wings of Fire
A PPT on the First Three chapters of Wings of Fire
Beena E S
English 4 Quarter 4 Week 4 Classroom Obs
English 4 Quarter 4 Week 4 Classroom ObsEnglish 4 Quarter 4 Week 4 Classroom Obs
English 4 Quarter 4 Week 4 Classroom Obs
NerissaMendez1
The Broccoli Dog's inner voice (look A)
The Broccoli Dog's inner voice  (look A)The Broccoli Dog's inner voice  (look A)
The Broccoli Dog's inner voice (look A)
merasan
APM People Interest Network Conference - Tim Lyons - The neurological levels ...
APM People Interest Network Conference - Tim Lyons - The neurological levels ...APM People Interest Network Conference - Tim Lyons - The neurological levels ...
APM People Interest Network Conference - Tim Lyons - The neurological levels ...
Association for Project Management
Year 10 The Senior Phase Session 3 Term 1.pptx
Year 10 The Senior Phase Session 3 Term 1.pptxYear 10 The Senior Phase Session 3 Term 1.pptx
Year 10 The Senior Phase Session 3 Term 1.pptx
mansk2
Digital Tools with AI for e-Content Development.pptx
Digital Tools with AI for e-Content Development.pptxDigital Tools with AI for e-Content Development.pptx
Digital Tools with AI for e-Content Development.pptx
Dr. Sarita Anand
Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...
Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...
Eng7-Q4-Lesson 1 Part 1 Understanding Discipline-Specific Words, Voice, and T...
sandynavergas1
FESTIVAL: SINULOG & THINGYAN-LESSON 4.pptx
FESTIVAL: SINULOG & THINGYAN-LESSON 4.pptxFESTIVAL: SINULOG & THINGYAN-LESSON 4.pptx
FESTIVAL: SINULOG & THINGYAN-LESSON 4.pptx
DanmarieMuli1
Principle and Practices of Animal Breeding || Boby Basnet
Principle and Practices of Animal Breeding || Boby BasnetPrinciple and Practices of Animal Breeding || Boby Basnet
Principle and Practices of Animal Breeding || Boby Basnet
Boby Basnet
Fuel part 1.pptx........................
Fuel part 1.pptx........................Fuel part 1.pptx........................
Fuel part 1.pptx........................
ksbhattadcm
How to Setup WhatsApp in Odoo 17 - Odoo 際際滷s
How to Setup WhatsApp in Odoo 17 - Odoo 際際滷sHow to Setup WhatsApp in Odoo 17 - Odoo 際際滷s
How to Setup WhatsApp in Odoo 17 - Odoo 際際滷s
Celine George
cervical spine mobilization manual therapy .pdf
cervical spine mobilization manual therapy .pdfcervical spine mobilization manual therapy .pdf
cervical spine mobilization manual therapy .pdf
SamarHosni3
EDL 290F Week 3 - Mountaintop Views (2025).pdf
EDL 290F Week 3  - Mountaintop Views (2025).pdfEDL 290F Week 3  - Mountaintop Views (2025).pdf
EDL 290F Week 3 - Mountaintop Views (2025).pdf
Liz Walsh-Trevino
How to Configure Flexible Working Schedule in Odoo 18 Employee
How to Configure Flexible Working Schedule in Odoo 18 EmployeeHow to Configure Flexible Working Schedule in Odoo 18 Employee
How to Configure Flexible Working Schedule in Odoo 18 Employee
Celine George
How to use Init Hooks in Odoo 18 - Odoo 際際滷s
How to use Init Hooks in Odoo 18 - Odoo 際際滷sHow to use Init Hooks in Odoo 18 - Odoo 際際滷s
How to use Init Hooks in Odoo 18 - Odoo 際際滷s
Celine George
POWERPOINT-PRESENTATION_DM-NO.017-S.2025.pptx
POWERPOINT-PRESENTATION_DM-NO.017-S.2025.pptxPOWERPOINT-PRESENTATION_DM-NO.017-S.2025.pptx
POWERPOINT-PRESENTATION_DM-NO.017-S.2025.pptx
MarilenQuintoSimbula
Database population in Odoo 18 - Odoo slides
Database population in Odoo 18 - Odoo slidesDatabase population in Odoo 18 - Odoo slides
Database population in Odoo 18 - Odoo slides
Celine George
Research & Research Methods: Basic Concepts and Types.pptx
Research & Research Methods: Basic Concepts and Types.pptxResearch & Research Methods: Basic Concepts and Types.pptx
Research & Research Methods: Basic Concepts and Types.pptx
Dr. Sarita Anand
A PPT on the First Three chapters of Wings of Fire
A PPT on the First Three chapters of Wings of FireA PPT on the First Three chapters of Wings of Fire
A PPT on the First Three chapters of Wings of Fire
Beena E S
English 4 Quarter 4 Week 4 Classroom Obs
English 4 Quarter 4 Week 4 Classroom ObsEnglish 4 Quarter 4 Week 4 Classroom Obs
English 4 Quarter 4 Week 4 Classroom Obs
NerissaMendez1
The Broccoli Dog's inner voice (look A)
The Broccoli Dog's inner voice  (look A)The Broccoli Dog's inner voice  (look A)
The Broccoli Dog's inner voice (look A)
merasan

dictionary14 ppt FINAL.pptx

  • 3. Dictionary It is an unordered collection of items where each item consist of a key and a value. It is mutable (can modify its contents ) but Key must be unique and immutable. COMPUTERSCIENCE (CLASS XI)
  • 5. Creating A Dictionary It is enclosed in curly braces {} and each item is separated from other item by a comma(,) Within each item, key and value are separated by a colon (:) Passing value in dictionary at declaration is dictionary initialization. get() method is used to access value of a key E.g. dict = {Subject': Informatic Practices', 'Class': 11'} Accessing List Item dict = {'Subject': 'Informatics Practices', 'Class': 11} print(dict) print ("Subject : ", dict['Subject']) dict.get('Class') OUTPUT {'Class': '11', 'Subject': 'Informatics Practices'} ('Subject : ', 'Informatics Practices') 11 Dictionary
  • 6. Iterating / Traversing through A Dictionary Following example will show how dictionary items can be accessed through loop. e.g. dict = {'Subject': 'Informatics Practices', 'Class': 11} for i in dict: print(dict[i]) OUTPUT Informatics Practices 11 Dictionary
  • 7. Iterating / Traversing through A Dictionary Following example will show how dictionary items can be accessed through loop. e.g. dict = {'Subject': 'Informatics Practices', 'Class': 11} for i in dict: print(dict[i]) OUTPUT Informatics Practices 11 Updating/Manipulating Dictionary Elements We can change the individual element of dictionary. e.g. dict = {'Subject': 'Informatics Practices', 'Class': 11} dict['Subject']='computer science' print(dict) OUTPU T Dictionary
  • 8. Questions 1. Create dictionary to store 4 student details with rollno, name, age. 2. Create dictionary for month and no of days for a year. User is asked to enter month name and system will show no of days of that month. Dictionary Visit : python.mykvs.in for regular
  • 9. Deleting Dictionary Elements del, pop() and clear() statement are used to remove elements from the dictionary. e.g. DEL dict = {'Subject': 'Informatics Practices', 'Class': 11} print('before del', dict) del dict['Class'] # delete single element print('after item delete', dict) del dict #delete whole dictionary print('after dictionary delete', type(dict)) Output (before del {'Class': 11, 'Subject': 'Informatics Practices'}) (after item delete {'Subject': 'Informatics Practices'}) ('after dictionary delete' <type 'dict'>) Dictionary
  • 10. pop() method is used to remove a particular item in a dictionary. clear() method is used to remove all elements from the dictionary. e.g. dict = {'Subject': 'Informatics Practices', 'Class': 11} print('before del', dict) dict.pop('Class') print('after item delete', dict) dict.clear() print('after clear', dict) Output ('before del' {'Class': 11, 'Subject': 'Informatics Practices'}) (after item delete {'Subject': 'Informatics Practices'}) ('after clear' {}) Dictionary
  • 11. Dictionary MCQ Q.1 What will be the output of the following? d = {abc: 5, def: 6, ghi:7} print(d1[abc]) # Line 1 print(d[0]) # Line 2 Q.2. Dictionaries are___________ data types of Python. a) Mutable b)immutable c) simple d) none of these Q.3. Which of the following can be used to delete item(s) from a dictionary? a) Del b) pop() c) popitem() d) all of these Q.4. Dictionaries are also called_________ a) Mapping b) hashes c) associative arrays d) all of these
  • 12. Built-in Dictionary Functions S.No. Function & Description 1 len(dict)----Gives the total length of the dictionary. It is equal to the number of items in the dictionary. dict = {'Name': 'Aman', 'Age': 37} print (len (dict)) OUTPUT ->2 2 str(dict)-----Return a printable stringrepresentationof a Dictionary. dict = {'Name': 'Aman', 'Age': 37} >>> "{'Name': 'Aman', 'Age': 37}" 3 type(variable)If variable is dictionary, then it would return a dictionary type. dict = {'Name': 'Aman', 'Age': 37} >>>type(dict) Dictionary
  • 13. Built-in Dictionary Methods S.No. Method & Description 1 dict() - creates dictionary # Argument method x = dict(name = Aman", age = 37, country = India") Here x is created as dictionary 2 keys() - returns all the available keys x = dict(name = Aman", age = 37, country = India") print(x.keys()) OUTPUT->dict_keys(['country', 'age', 'name']) 3 values() - returns all the available values x = dict(name = Aman", age = 37, country = India") print(x.values()) OUTPUT->dict_values(['India', 37, 'Aman']) Dictionary
  • 14. S.No. Method & Description 4 items() - return the list with all dictionary keys with values. x = dict(name = "Aman", age = 37, country = "India") print(x.items()) OUTPUT->dict_items([('country', 'India'), ('age', 37), ('name', 'Aman')]) 5 update()-used to change the values of a key and add new keys x = dict(name = "Aman", age = 37, country = India") d1 = dict(age= 39) x.update(d1,state="Rajasthan ") print(x) OUTPUT-{'country': 'India', 'age': 39,'name':'Aman','state': 'Rajasthan'} Dictionary Built-in Dictionary Methods
  • 15. S.No. Method & Description 6 del -used to remove key x = dict(name = "Aman", age = 37, country = "India") del x['age'] print(x) OUTPUT->{'country': 'India', 'name': 'Aman'} del x -> will remove complete dictionary 7 fromkeys() is used to create dictionary from keys keys = {'a', 'e', 'i', 'o', 'u' } value ="Vowel" vowels = dict.fromkeys(keys, value) print(vowels) OUTPUT-> {'i': 'Vowel', 'u': 'Vowel', 'e': 'Vowel', 'a': 'Vowel', 'o': 'Vowel'} Dictionary Built-in Dictionary Methods
  • 16. S.No. Method & Description 8 copy() - returns a shallow copy of the dictionary. x = dict(name = "Aman", age = 37, country = "India") y=x.copy() print(y) print(id(x)) print(id(y)) OUTPUT - >{'country': 'India', 'age': 37, 'name': 'Aman'} 33047872 33047440 9 popitem() removes last item from dictionary x = dict(name = "Aman", age = 37, country = "India") x.popitem() print(x) OUTPUT-> {'age': 37, 'name': 'Aman'} Dictionary Built-in Dictionary Methods
  • 17. S.No. Method & Description 10 setdefault() method returns the value of the item with the specified key. If the key does not exist, insert the key, with the specified value. x = dict(name = "Aman", country = "India") y=x.setdefault('age',39) print(y) OUTPUT-> 39 11 max() returns key having maximum value Tv = {'a':100, 'b':1292, 'c' : 88} Keymax = max(Tv, key=Tv.get) print(Keymax) OUTPUT-> b 12 min()- returns key having minimum value Dict = {'Name': 'Aman', 'Age': 37} print (min(Dict) >>> Output - ? Dictionary Built-in Dictionary Methods Dictionary
  • 18. S.No. Method & Description 13 max( )- returns key having maximum value Dict = {'Name': 'Aman', 'Age': 37} print (max(Dict) >>> Output - ? 14. sorted- sort by key or value dict1 = {'b':100, 'a':12, 'c' : 88} y = sorted(dict1.items(),key=lambda x: x[1],reverse=True) print(y) OUTPUT-> [('b', 100), ('c', 88), ('a',12)] Dictionary Built-in Dictionary Methods
  • 19. Competency Based Question stateCapital = {AndhraPradesh:Hyderabad, Bihar:Patna, Maharashtra:Mumbai, Rajasthan:Jaipur} Find output of the following: 1. print(stateCapital.get(Bihar)) 2. print(stateCapital.keys() 3. print(stateCapital.values()) 4. print(len(stateCapital)) 5. del stateCapital [Andhra Pradesh] print (stateCapital) 6. print(Maharashtra in stateCapital) Dictionary Visit : python.mykvs.in for regular
  • 20. 1. create a dictionary with names of employees, their salary and access them 2. Count the number of times a character appears in agiven string using a dictionary Dictionary Program: