SlideShare a Scribd company logo
1 of 19
 Dictionary overview Creating
 Dictionaries
 Dictionary operations
 Dictionaries Data structure
Python
Dictionaries
Vibrant Technology Course in Mumbai1
objectives
1. Describe the characteristics of the
dictionary data structure in Python
2. Perform basic operations with
dictionaries including creation,
copying, updating, and traversing
3. Use dictionaries in functions
Vibrant Technology Course in Mumbai2
The dictionary data structure
In Python, a dictionary is
mapping between a set of indices
(keys) and a set of values
The items in a dictionary are key-
value pairs
Vibrant Technology Course in Mumbai3
The dictionary data structure
Keys can be any Python data
type
Because keys are used for indexing,
they should be immutable
Values can be any Python data
type
Values can be mutable or
immutable
Vibrant Technology Course in Mumbai4
Creating a dictionary
eng2sp = dict()
print eng2sp
eng2sp['one'] = 'uno'
print eng2sp
eng2sp['two'] = 'dos'
print eng2sp
Vibrant Technology Course in Mumbai5
Creating a dictionary
eng2sp = {'one': 'uno', 'two': 'dos',
'three': 'tres'}
print eng2sp
• In general, the order of items in a
dictionary is unpredictable
• Dictionaries are indexed by keys, not
integers
Vibrant Technology Course in Mumbai6
Dictionary indexing
print eng2sp['three']
print eng2sp['five']
* If the index is not a key in the
dictionary, Python raises an exception
Vibrant Technology Course in Mumbai7
Dictionary indexing
if 'five' in eng2sp:
print eng2sp['five']
print eng2sp.get('five')
Vibrant Technology Course in Mumbai8
The in operator
• Note that the in operator works differently
for dictionaries than for other sequences
• For offset indexed sequences (strings, lists,
tuples), x in y checks to see whether x is an
item in the sequence
• For dictionaries, x in y checks to see whether
x is a key in the dictionary
Vibrant Technology Course in Mumbai9
Keys and values
The keys method returns a list of the
keys in a dictionary
print eng2sp.keys()
The values method returns a list of
the values
print eng2sp.values()
Vibrant Technology Course in Mumbai10
Keys and values
The items method returns a list of
tuple pairs of the key-value pairs in a
dictionary
print eng2sp.items()
Vibrant Technology Course in Mumbai11
Example: histogram.py
def histogram(seq):
d = dict()
for element in seq:
if element not in d:
d[element] = 1
else:
d[element] += 1
return d
h = histogram('brontosaurus')
print h
Vibrant Technology Course in Mumbai12
Example: histogram2.py
Add the following code to histogram.py:
def print_hist(hist):
for key in hist:
print key, hist[key]
h = histogram('brontosaurus')
print_hist(h)
Vibrant Technology Course in Mumbai13
Example: histogram2.py
Change the print_hist function:
def print_hist(hist):
for key, value in hist:
print key, value
h = histogram('brontosaurus')
print_hist(h)
Vibrant Technology Course in Mumbai14
Sorting the keys
Change the print_hist function:
def print_hist(hist):
keys = hist.keys()
keys.sort()
for key in keys:
print key, hist[key]
h = histogram('brontosaurus')
print_hist(h)
Vibrant Technology Course in Mumbai15
Using lists as values: invert.py
Add the following code to histogram.py:
def invert_dict(d):
inv = dict()
for key in d:
val = d[key]
if val not in inv:
inv[val] = [key]
else:
inv[val].append(key)
return inv
Vibrant Technology Course in Mumbai16
Using lists as values: invert.py
Add the following code to histogram.py:
hist = histogram('parrot')
print hist
inverted = invert_dict(hist)
print inverted
Vibrant Technology Course in Mumbai17
Using tuples as keys: troupe.py
troupe = {('Cleese', 'John'): [1,2,3],
('Chapman', 'Graham'): [4,5,6],
('Idle', 'Eric'): [7,8,9],
('Jones', 'Terry'): [10,11,12],
('Gilliam', 'Terry'): [13,14,15,16,17,18],
('Palin', 'Michael'): [19,20]}
for last, first in troupe:
print first, last, troupe[last, first]
Vibrant Technology Course in Mumbai18
Thank You…
Vibrant Technology Course in Mumbai19
Vibrant Technologies & Computers
www.vibranttechnologies.co.in
“The Best Training Center in Mumbai”

More Related Content

What's hot

Python functions
Python functionsPython functions
Python functionsAliyamanasa
 
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYAChapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYAMaulik Borsaniya
 
python Function
python Function python Function
python Function Ronak Rathi
 
Python Closures Explained | What are Closures in Python | Python Closures
Python Closures Explained | What are Closures in Python | Python ClosuresPython Closures Explained | What are Closures in Python | Python Closures
Python Closures Explained | What are Closures in Python | Python ClosuresIntellipaat
 
CSE240 Macros and Preprocessing
CSE240 Macros and PreprocessingCSE240 Macros and Preprocessing
CSE240 Macros and PreprocessingGarrett Gutierrez
 
Functional Python Webinar from October 22nd, 2014
Functional Python Webinar from October 22nd, 2014Functional Python Webinar from October 22nd, 2014
Functional Python Webinar from October 22nd, 2014Reuven Lerner
 
2 cs xii_python_functions _ scopes
2 cs xii_python_functions _ scopes2 cs xii_python_functions _ scopes
2 cs xii_python_functions _ scopesSanjayKumarMahto1
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocationGem WeBlog
 
Maharishi University of Management (MSc Computer Science test questions)
Maharishi University of Management (MSc Computer Science test questions)Maharishi University of Management (MSc Computer Science test questions)
Maharishi University of Management (MSc Computer Science test questions)Dharma Kshetri
 
Introduction To Programming with Python-3
Introduction To Programming with Python-3Introduction To Programming with Python-3
Introduction To Programming with Python-3Syed Farjad Zia Zaidi
 
Iterarators and generators in python
Iterarators and generators in pythonIterarators and generators in python
Iterarators and generators in pythonSarfaraz Ghanta
 

What's hot (20)

Advance python
Advance pythonAdvance python
Advance python
 
Interpreter Case Study - Design Patterns
Interpreter Case Study - Design PatternsInterpreter Case Study - Design Patterns
Interpreter Case Study - Design Patterns
 
Lk module5 pointers
Lk module5 pointersLk module5 pointers
Lk module5 pointers
 
Python functions
Python functionsPython functions
Python functions
 
C workshop day 7
C workshop day 7C workshop day 7
C workshop day 7
 
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYAChapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
 
python Function
python Function python Function
python Function
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Pointer to function 2
Pointer to function 2Pointer to function 2
Pointer to function 2
 
Python Closures Explained | What are Closures in Python | Python Closures
Python Closures Explained | What are Closures in Python | Python ClosuresPython Closures Explained | What are Closures in Python | Python Closures
Python Closures Explained | What are Closures in Python | Python Closures
 
CSE240 Macros and Preprocessing
CSE240 Macros and PreprocessingCSE240 Macros and Preprocessing
CSE240 Macros and Preprocessing
 
Functional Python Webinar from October 22nd, 2014
Functional Python Webinar from October 22nd, 2014Functional Python Webinar from October 22nd, 2014
Functional Python Webinar from October 22nd, 2014
 
2 cs xii_python_functions _ scopes
2 cs xii_python_functions _ scopes2 cs xii_python_functions _ scopes
2 cs xii_python_functions _ scopes
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Maharishi University of Management (MSc Computer Science test questions)
Maharishi University of Management (MSc Computer Science test questions)Maharishi University of Management (MSc Computer Science test questions)
Maharishi University of Management (MSc Computer Science test questions)
 
Python advance
Python advancePython advance
Python advance
 
Introduction To Programming with Python-3
Introduction To Programming with Python-3Introduction To Programming with Python-3
Introduction To Programming with Python-3
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Iterarators and generators in python
Iterarators and generators in pythonIterarators and generators in python
Iterarators and generators in python
 

Similar to Python03 course in_mumbai

Automation Testing theory notes.pptx
Automation Testing theory notes.pptxAutomation Testing theory notes.pptx
Automation Testing theory notes.pptxNileshBorkar12
 
FDP-faculty deveopmemt program on python
FDP-faculty deveopmemt program on pythonFDP-faculty deveopmemt program on python
FDP-faculty deveopmemt program on pythonkannikadg
 
Improve Your Edge on Machine Learning - Day 1.pptx
Improve Your Edge on Machine Learning - Day 1.pptxImprove Your Edge on Machine Learning - Day 1.pptx
Improve Your Edge on Machine Learning - Day 1.pptxCatherineVania1
 
Interview-level-QA-on-Python-Programming.pdf
Interview-level-QA-on-Python-Programming.pdfInterview-level-QA-on-Python-Programming.pdf
Interview-level-QA-on-Python-Programming.pdfExaminationSectionMR
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimizationg3_nittala
 
10 Tips for Writing Pythonic Code by Michael Kennedy
10 Tips for Writing Pythonic Code by Michael Kennedy10 Tips for Writing Pythonic Code by Michael Kennedy
10 Tips for Writing Pythonic Code by Michael KennedyMichael Kennedy
 
Top Python Online Training Institutes in Bangalore
Top Python Online Training Institutes in BangaloreTop Python Online Training Institutes in Bangalore
Top Python Online Training Institutes in BangaloreSaagTechnologies
 
"Python Dictionary: The Key to Efficient Data Storage, Manipulation, and Vers...
"Python Dictionary: The Key to Efficient Data Storage, Manipulation, and Vers..."Python Dictionary: The Key to Efficient Data Storage, Manipulation, and Vers...
"Python Dictionary: The Key to Efficient Data Storage, Manipulation, and Vers...ZainabHaneen
 
James Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonJames Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonCP-Union
 
Advanced Web Technology ass.pdf
Advanced Web Technology ass.pdfAdvanced Web Technology ass.pdf
Advanced Web Technology ass.pdfsimenehanmut
 
Introduction-to-Iteration.pdf
Introduction-to-Iteration.pdfIntroduction-to-Iteration.pdf
Introduction-to-Iteration.pdfKeshavBandil2
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAdam Getchell
 
2 UNIT CH3 Dictionaries v1.ppt
2 UNIT CH3 Dictionaries v1.ppt2 UNIT CH3 Dictionaries v1.ppt
2 UNIT CH3 Dictionaries v1.ppttocidfh
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdfprasnt1
 
Python Programming by Dr. C. Sreedhar.pdf
Python Programming by Dr. C. Sreedhar.pdfPython Programming by Dr. C. Sreedhar.pdf
Python Programming by Dr. C. Sreedhar.pdfSreedhar Chowdam
 

Similar to Python03 course in_mumbai (20)

Python dictionaries
Python dictionariesPython dictionaries
Python dictionaries
 
Automation Testing theory notes.pptx
Automation Testing theory notes.pptxAutomation Testing theory notes.pptx
Automation Testing theory notes.pptx
 
FDP-faculty deveopmemt program on python
FDP-faculty deveopmemt program on pythonFDP-faculty deveopmemt program on python
FDP-faculty deveopmemt program on python
 
Python basic
Python basicPython basic
Python basic
 
Improve Your Edge on Machine Learning - Day 1.pptx
Improve Your Edge on Machine Learning - Day 1.pptxImprove Your Edge on Machine Learning - Day 1.pptx
Improve Your Edge on Machine Learning - Day 1.pptx
 
Interview-level-QA-on-Python-Programming.pdf
Interview-level-QA-on-Python-Programming.pdfInterview-level-QA-on-Python-Programming.pdf
Interview-level-QA-on-Python-Programming.pdf
 
Feature Engineering in NLP.pdf
Feature Engineering in NLP.pdfFeature Engineering in NLP.pdf
Feature Engineering in NLP.pdf
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimization
 
10 Tips for Writing Pythonic Code by Michael Kennedy
10 Tips for Writing Pythonic Code by Michael Kennedy10 Tips for Writing Pythonic Code by Michael Kennedy
10 Tips for Writing Pythonic Code by Michael Kennedy
 
CPPDS Slide.pdf
CPPDS Slide.pdfCPPDS Slide.pdf
CPPDS Slide.pdf
 
Top Python Online Training Institutes in Bangalore
Top Python Online Training Institutes in BangaloreTop Python Online Training Institutes in Bangalore
Top Python Online Training Institutes in Bangalore
 
"Python Dictionary: The Key to Efficient Data Storage, Manipulation, and Vers...
"Python Dictionary: The Key to Efficient Data Storage, Manipulation, and Vers..."Python Dictionary: The Key to Efficient Data Storage, Manipulation, and Vers...
"Python Dictionary: The Key to Efficient Data Storage, Manipulation, and Vers...
 
James Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonJames Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on Python
 
Advanced Web Technology ass.pdf
Advanced Web Technology ass.pdfAdvanced Web Technology ass.pdf
Advanced Web Technology ass.pdf
 
Introduction-to-Iteration.pdf
Introduction-to-Iteration.pdfIntroduction-to-Iteration.pdf
Introduction-to-Iteration.pdf
 
Chapter 16 Dictionaries
Chapter 16 DictionariesChapter 16 Dictionaries
Chapter 16 Dictionaries
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional Programming
 
2 UNIT CH3 Dictionaries v1.ppt
2 UNIT CH3 Dictionaries v1.ppt2 UNIT CH3 Dictionaries v1.ppt
2 UNIT CH3 Dictionaries v1.ppt
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdf
 
Python Programming by Dr. C. Sreedhar.pdf
Python Programming by Dr. C. Sreedhar.pdfPython Programming by Dr. C. Sreedhar.pdf
Python Programming by Dr. C. Sreedhar.pdf
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 

Recently uploaded (20)

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 

Python03 course in_mumbai

  • 1.  Dictionary overview Creating  Dictionaries  Dictionary operations  Dictionaries Data structure Python Dictionaries Vibrant Technology Course in Mumbai1
  • 2. objectives 1. Describe the characteristics of the dictionary data structure in Python 2. Perform basic operations with dictionaries including creation, copying, updating, and traversing 3. Use dictionaries in functions Vibrant Technology Course in Mumbai2
  • 3. The dictionary data structure In Python, a dictionary is mapping between a set of indices (keys) and a set of values The items in a dictionary are key- value pairs Vibrant Technology Course in Mumbai3
  • 4. The dictionary data structure Keys can be any Python data type Because keys are used for indexing, they should be immutable Values can be any Python data type Values can be mutable or immutable Vibrant Technology Course in Mumbai4
  • 5. Creating a dictionary eng2sp = dict() print eng2sp eng2sp['one'] = 'uno' print eng2sp eng2sp['two'] = 'dos' print eng2sp Vibrant Technology Course in Mumbai5
  • 6. Creating a dictionary eng2sp = {'one': 'uno', 'two': 'dos', 'three': 'tres'} print eng2sp • In general, the order of items in a dictionary is unpredictable • Dictionaries are indexed by keys, not integers Vibrant Technology Course in Mumbai6
  • 7. Dictionary indexing print eng2sp['three'] print eng2sp['five'] * If the index is not a key in the dictionary, Python raises an exception Vibrant Technology Course in Mumbai7
  • 8. Dictionary indexing if 'five' in eng2sp: print eng2sp['five'] print eng2sp.get('five') Vibrant Technology Course in Mumbai8
  • 9. The in operator • Note that the in operator works differently for dictionaries than for other sequences • For offset indexed sequences (strings, lists, tuples), x in y checks to see whether x is an item in the sequence • For dictionaries, x in y checks to see whether x is a key in the dictionary Vibrant Technology Course in Mumbai9
  • 10. Keys and values The keys method returns a list of the keys in a dictionary print eng2sp.keys() The values method returns a list of the values print eng2sp.values() Vibrant Technology Course in Mumbai10
  • 11. Keys and values The items method returns a list of tuple pairs of the key-value pairs in a dictionary print eng2sp.items() Vibrant Technology Course in Mumbai11
  • 12. Example: histogram.py def histogram(seq): d = dict() for element in seq: if element not in d: d[element] = 1 else: d[element] += 1 return d h = histogram('brontosaurus') print h Vibrant Technology Course in Mumbai12
  • 13. Example: histogram2.py Add the following code to histogram.py: def print_hist(hist): for key in hist: print key, hist[key] h = histogram('brontosaurus') print_hist(h) Vibrant Technology Course in Mumbai13
  • 14. Example: histogram2.py Change the print_hist function: def print_hist(hist): for key, value in hist: print key, value h = histogram('brontosaurus') print_hist(h) Vibrant Technology Course in Mumbai14
  • 15. Sorting the keys Change the print_hist function: def print_hist(hist): keys = hist.keys() keys.sort() for key in keys: print key, hist[key] h = histogram('brontosaurus') print_hist(h) Vibrant Technology Course in Mumbai15
  • 16. Using lists as values: invert.py Add the following code to histogram.py: def invert_dict(d): inv = dict() for key in d: val = d[key] if val not in inv: inv[val] = [key] else: inv[val].append(key) return inv Vibrant Technology Course in Mumbai16
  • 17. Using lists as values: invert.py Add the following code to histogram.py: hist = histogram('parrot') print hist inverted = invert_dict(hist) print inverted Vibrant Technology Course in Mumbai17
  • 18. Using tuples as keys: troupe.py troupe = {('Cleese', 'John'): [1,2,3], ('Chapman', 'Graham'): [4,5,6], ('Idle', 'Eric'): [7,8,9], ('Jones', 'Terry'): [10,11,12], ('Gilliam', 'Terry'): [13,14,15,16,17,18], ('Palin', 'Michael'): [19,20]} for last, first in troupe: print first, last, troupe[last, first] Vibrant Technology Course in Mumbai18
  • 19. Thank You… Vibrant Technology Course in Mumbai19 Vibrant Technologies & Computers www.vibranttechnologies.co.in “The Best Training Center in Mumbai”