SlideShare una empresa de Scribd logo
1 de 12
Descargar para leer sin conexión
Dictionaries (Hash Maps)
Python dictionaries are unordered collection of pairs in the form of key:value.
e.g. L=[ 2,4,5,6,10 ]
print(L[2])
D = { 'dog' : 'friendly' , 'lion' : 'wild' , 'parrot' : 'funny' }
print( D['dog'] )
Dictionaries
● It’s an unordered set of key:value pairs.
● Dictionaries are mutable.
● Unlike a string, list & tuple. A dictionary is not a sequence, it’s
unordered set of elements.
● All keys must be unique.
● We can change the value of any key :
D[<key>] = <new value>
Playing with Dictionaries
■ Walking in a Dictionary
To traverse in a dictionary we can use the for loop.
for <variable> in <dictionary> :
print(<variable>)
for i in d :
print( i, ':', d[i] )
D = { 'dog' : 'friendly' , 'lion' : 'wild' , 'parrot' : 'funny' }
Playing with Dictionaries
■ Adding an element
D['monkey'] = 'Human-Like'
■ Updating an element
D[ 'parrot'] ='Sometimes-funny' ✔
D['bat' ] ='Not-for-eating' ❌
■ Deleting an element
del <dictionary>[<key>] del d['lion']
<dictionary>.pop(<key>) d.pop('lion')
Pop method also returns the deleted element.
print(d.pop('lion'))
■ Checking existence of a key
‘lion’ in d --- true
‘tiger’ in d --- false
Playing with Dictionaries
■ The len( ) method
len( <dictionary> ) --- gives the count of pairs
■ The clear( ) method
<dictionary>.clear( ) --- clears the whole dictionary.
■ The get( ) method
print(d.get(‘lion’)) --- print(d[‘lion’])
■ The keys( ) method
print(d.keys( )) --- displays all the keys
Dictionaries Functions
■ The items( ) method
This method returns all the pairs as a sequence of (key,value) tuples.
(in no particular order).
L=d.items( )
print(L) --- [('dog' , 'friendly'),('lion' , 'wild'),('parrot' , 'funny')]
■ The values( ) method
<dictionary>.values( ) --- returns all the values.
Dictionaries Functions
Dictionaries Functions
■ The update( ) method
<dictionary>.update( <other-dictionary> )
<other-dictionary> will over-ride in <dictionary>.
● New Keys will be added.
● Existing key will be updated with new values.
e.g. d = { 'dog' : 'friendly' , 'lion' : 'wild' , 'parrot' : 'funny' }
d2= { 'dog' : pet' , 'lion' : 'wild' , tiger' : 'dangerous’ }
d.update(d2)
print(d)
{ 'dog' : pet' , 'lion' : 'wild' , 'parrot' : 'funny', ‘tiger’:’dangerous’ }
Practice Time
Q1. R={ 'Name' : 'Python', 'Age' : '20', 'Addr' : 'NJ' , 'Country' : 'USA' }
id1=id(R)
R2={ 'Name' : 'Python', 'Age' : '20', 'Addr' : 'NJ' , 'Country' : 'USA' }
id2=id(R2)
print(id1==id2) ( id( ) gives the address of the object )
Q2. D = { }
D[ (1,2,4) ] =8
D[ (4,2,1) ] =10
D[ (1,2) ] =12
sum = 0
for k in D:
sum+=D[k]
print(sum)
print(D)
Practice Time
1. W.a.p. to verify whether the phone number is in the given format
or not
( ex. 017-555-1212 is the correct format )
2. Write a program that prints the longest word in a list.
3. Write a python program that creates a list storing first 9 terms of
fibonacci series.
1, 1, 2, 3 ,5 ,8 ….
4. W.a.p. to make a list of all integers less than hundred which are
multiples of 3 or 5. --- H.W.
Practice Time
1. Write a function addDict(dict1, dict2) which computes the union of two
dictionaries. If same key appears in both, pick either one.
2. Create a dictionary whose keys are month names and whose values are
number of days in the corresponding months.
a. Ask user to enter month name and number of days.
b. Print out all of the keys in alphabetical order.
c. Print out all of the months with 31 days.
d. Print out the (key-value) pairs sorted by keys.
e. Print out the (key-value) pairs sorted by values. Typical
Lecture-6.pdf

Más contenido relacionado

Similar a Lecture-6.pdf

Beginner's Python Cheat Sheet
Beginner's Python Cheat SheetBeginner's Python Cheat Sheet
Beginner's Python Cheat SheetVerxus
 
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).pdfElNew2
 
DICTIONARIES (1).pptx
DICTIONARIES (1).pptxDICTIONARIES (1).pptx
DICTIONARIES (1).pptxKalashJain27
 
An Introduction to Tuple List Dictionary in Python
An Introduction to Tuple List Dictionary in PythonAn Introduction to Tuple List Dictionary in Python
An Introduction to Tuple List Dictionary in Pythonyashar Aliabasi
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayUtkarsh Sengar
 
Basics of Python programming (part 2)
Basics of Python programming (part 2)Basics of Python programming (part 2)
Basics of Python programming (part 2)Pedro Rodrigues
 
Class 31: Deanonymizing
Class 31: DeanonymizingClass 31: Deanonymizing
Class 31: DeanonymizingDavid Evans
 
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
 
2 UNIT CH3 Dictionaries v1.ppt
2 UNIT CH3 Dictionaries v1.ppt2 UNIT CH3 Dictionaries v1.ppt
2 UNIT CH3 Dictionaries v1.ppttocidfh
 
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdfGE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdfAsst.prof M.Gokilavani
 
STRING LIST TUPLE DICTIONARY FILE.pdf
STRING LIST TUPLE DICTIONARY FILE.pdfSTRING LIST TUPLE DICTIONARY FILE.pdf
STRING LIST TUPLE DICTIONARY FILE.pdfomprakashmeena48
 
beginners_python_cheat_sheet_pcc_all (3).pptx
beginners_python_cheat_sheet_pcc_all (3).pptxbeginners_python_cheat_sheet_pcc_all (3).pptx
beginners_python_cheat_sheet_pcc_all (3).pptxHongAnhNguyn285885
 
python chapter 1
python chapter 1python chapter 1
python chapter 1Raghu nath
 
Python chapter 2
Python chapter 2Python chapter 2
Python chapter 2Raghu nath
 

Similar a Lecture-6.pdf (20)

Beginner's Python Cheat Sheet
Beginner's Python Cheat SheetBeginner's Python Cheat Sheet
Beginner's Python Cheat Sheet
 
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
 
DICTIONARIES (1).pptx
DICTIONARIES (1).pptxDICTIONARIES (1).pptx
DICTIONARIES (1).pptx
 
An Introduction to Tuple List Dictionary in Python
An Introduction to Tuple List Dictionary in PythonAn Introduction to Tuple List Dictionary in Python
An Introduction to Tuple List Dictionary in Python
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
 
Python tutorial
Python tutorialPython tutorial
Python tutorial
 
Basics of Python programming (part 2)
Basics of Python programming (part 2)Basics of Python programming (part 2)
Basics of Python programming (part 2)
 
Dictionary in python
Dictionary in pythonDictionary in python
Dictionary in python
 
Python lecture 05
Python lecture 05Python lecture 05
Python lecture 05
 
Class 31: Deanonymizing
Class 31: DeanonymizingClass 31: Deanonymizing
Class 31: Deanonymizing
 
Beginners python cheat sheet - Basic knowledge
Beginners python cheat sheet - Basic knowledge Beginners python cheat sheet - Basic knowledge
Beginners python cheat sheet - Basic knowledge
 
1. python
1. python1. python
1. python
 
Python cheatsheet for beginners
Python cheatsheet for beginnersPython cheatsheet for beginners
Python cheatsheet for beginners
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
2 UNIT CH3 Dictionaries v1.ppt
2 UNIT CH3 Dictionaries v1.ppt2 UNIT CH3 Dictionaries v1.ppt
2 UNIT CH3 Dictionaries v1.ppt
 
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdfGE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
GE3151 PSPP UNIT IV QUESTION BANK.docx.pdf
 
STRING LIST TUPLE DICTIONARY FILE.pdf
STRING LIST TUPLE DICTIONARY FILE.pdfSTRING LIST TUPLE DICTIONARY FILE.pdf
STRING LIST TUPLE DICTIONARY FILE.pdf
 
beginners_python_cheat_sheet_pcc_all (3).pptx
beginners_python_cheat_sheet_pcc_all (3).pptxbeginners_python_cheat_sheet_pcc_all (3).pptx
beginners_python_cheat_sheet_pcc_all (3).pptx
 
python chapter 1
python chapter 1python chapter 1
python chapter 1
 
Python chapter 2
Python chapter 2Python chapter 2
Python chapter 2
 

Último

How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceanilsa9823
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 

Último (20)

CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 

Lecture-6.pdf

  • 1. Dictionaries (Hash Maps) Python dictionaries are unordered collection of pairs in the form of key:value. e.g. L=[ 2,4,5,6,10 ] print(L[2]) D = { 'dog' : 'friendly' , 'lion' : 'wild' , 'parrot' : 'funny' } print( D['dog'] )
  • 2. Dictionaries ● It’s an unordered set of key:value pairs. ● Dictionaries are mutable. ● Unlike a string, list & tuple. A dictionary is not a sequence, it’s unordered set of elements. ● All keys must be unique. ● We can change the value of any key : D[<key>] = <new value>
  • 3. Playing with Dictionaries ■ Walking in a Dictionary To traverse in a dictionary we can use the for loop. for <variable> in <dictionary> : print(<variable>) for i in d : print( i, ':', d[i] ) D = { 'dog' : 'friendly' , 'lion' : 'wild' , 'parrot' : 'funny' }
  • 4. Playing with Dictionaries ■ Adding an element D['monkey'] = 'Human-Like' ■ Updating an element D[ 'parrot'] ='Sometimes-funny' ✔ D['bat' ] ='Not-for-eating' ❌
  • 5. ■ Deleting an element del <dictionary>[<key>] del d['lion'] <dictionary>.pop(<key>) d.pop('lion') Pop method also returns the deleted element. print(d.pop('lion')) ■ Checking existence of a key ‘lion’ in d --- true ‘tiger’ in d --- false Playing with Dictionaries
  • 6. ■ The len( ) method len( <dictionary> ) --- gives the count of pairs ■ The clear( ) method <dictionary>.clear( ) --- clears the whole dictionary. ■ The get( ) method print(d.get(‘lion’)) --- print(d[‘lion’]) ■ The keys( ) method print(d.keys( )) --- displays all the keys Dictionaries Functions
  • 7. ■ The items( ) method This method returns all the pairs as a sequence of (key,value) tuples. (in no particular order). L=d.items( ) print(L) --- [('dog' , 'friendly'),('lion' , 'wild'),('parrot' , 'funny')] ■ The values( ) method <dictionary>.values( ) --- returns all the values. Dictionaries Functions
  • 8. Dictionaries Functions ■ The update( ) method <dictionary>.update( <other-dictionary> ) <other-dictionary> will over-ride in <dictionary>. ● New Keys will be added. ● Existing key will be updated with new values. e.g. d = { 'dog' : 'friendly' , 'lion' : 'wild' , 'parrot' : 'funny' } d2= { 'dog' : pet' , 'lion' : 'wild' , tiger' : 'dangerous’ } d.update(d2) print(d) { 'dog' : pet' , 'lion' : 'wild' , 'parrot' : 'funny', ‘tiger’:’dangerous’ }
  • 9. Practice Time Q1. R={ 'Name' : 'Python', 'Age' : '20', 'Addr' : 'NJ' , 'Country' : 'USA' } id1=id(R) R2={ 'Name' : 'Python', 'Age' : '20', 'Addr' : 'NJ' , 'Country' : 'USA' } id2=id(R2) print(id1==id2) ( id( ) gives the address of the object ) Q2. D = { } D[ (1,2,4) ] =8 D[ (4,2,1) ] =10 D[ (1,2) ] =12 sum = 0 for k in D: sum+=D[k] print(sum) print(D)
  • 10. Practice Time 1. W.a.p. to verify whether the phone number is in the given format or not ( ex. 017-555-1212 is the correct format ) 2. Write a program that prints the longest word in a list. 3. Write a python program that creates a list storing first 9 terms of fibonacci series. 1, 1, 2, 3 ,5 ,8 …. 4. W.a.p. to make a list of all integers less than hundred which are multiples of 3 or 5. --- H.W.
  • 11. Practice Time 1. Write a function addDict(dict1, dict2) which computes the union of two dictionaries. If same key appears in both, pick either one. 2. Create a dictionary whose keys are month names and whose values are number of days in the corresponding months. a. Ask user to enter month name and number of days. b. Print out all of the keys in alphabetical order. c. Print out all of the months with 31 days. d. Print out the (key-value) pairs sorted by keys. e. Print out the (key-value) pairs sorted by values. Typical