SlideShare a Scribd company logo
Python with AI – I
Dictionaries
Dictionaries in python
• A dictionary consists of two things (a) keys (b) values
Dictionaries in python
• A dictionary consists of two things (a) keys (b) values
• Use strings to represent keys
Dictionaries in python
• A dictionary consists of two things (a) keys (b) values
• Use strings to represent keys
• Values can be anything
Dictionaries in python
• A dictionary consists of two things (a) keys (b) values
• Use strings to represent keys
• Values can be anything
• There are many ways to create a dictionary, we will look at one way to
do it:
Dictionaries in python
• A dictionary consists of two things (a) keys (b) values
• Use strings to represent keys
• Values can be anything
Print the entire dictionary
• Print(my_dict)
• Please inspect the format in which it is printed.
• Where do the : symbols show up?
Print the entire dictionary
• Print(my_dict)
• Please inspect the format in which it is printed.
• Where do the colon (:) symbols show up?
• Where do the commas (,) show up?
Exercise
• Create a dictionary, where the keys are one, two and the
corresponding values are 1 and 2
Exercise
• Create a dictionary, where the keys are one, two and the
corresponding values are 1 and 2
• Create a dictionary, where the keys are aiclub, PA1, M1 and the
corresponding values are website, python class, AI class.
Dictionaries in python
• Print a value in a dictionary
Exercise
• Print values of the first dictionary you created
• Print value corresponding to the key one
• Print value corresponding to the key two
Exercise
• Print values of the first dictionary you created
• Print value corresponding to the key one
• Print value corresponding to the key two
• Print values of the second dictionary you created
• Print value corresponding to the key aiclub
• Print value corresponding to the key M1
• Print value corresponding to the key PA1
Dictionaries in python
• Print a value in a dictionary
• Delete a value in a dictionary
Exercise
• Delete and print values of new dictionaries
• For the first dictionary
• Delete the value corresponding to the key one
• Print the value of dictionary
• For the second dictionary
• Delete the value corresponding to the key aiclub
• Print the value of dictionary
Dictionaries in python
• Print a value in a dictionary
• Delete a value in a dictionary
• Print all keys of a dictionary
Exercise
• Instead of printing the entire dictionary, please print only the keys of
the two dictionaries you created
Dictionaries in python
• Print a value in a dictionary
• Delete a value in a dictionary
• Print all keys of a dictionary
• Add values to a dictionary
Exercise
• Add the keys that you deleted from the 2 dictionaries back
• Print the two dictionaries
loops and dictionaries – use together
• Loops - recap
• The for loop is of the following form.
• The range indicates the number of times a statement will be implemented.
• There is a colon value to indicate the start of for loop.
• The code below is executed many times. The number of times it gets executed
depends on the values specified in range.
• Note that the code below the for statement is indented.
Loops - recap
• The for loop is of the following form.
• Try different values inside the brackets of range
• Note that everything with indentation below for loop is executed as a
part of the loop.
Loops and Dictionaries
• Exercise
a) Create a random number in the limits 0 and 10 using a random number
generator
Loops and Dictionaries
• Exercise
a) Create a random number in the limits 0 and 10 using a random number
generator
b) Create a list of length 10 using the random number generator (pick any
limits) – please use a loop
Loops and Dictionaries
• Exercise
a) Create a random number in the limits 0 and 10 using a random number
generator
b) Create a list of length 10 using the random number generator (pick any
limits) – please use a loop
c) Now create a dictionary using the values of the list as keys and the
corresponding values for each key can be another random number (pick any
limits).
Lists - recap
• A list holds ordered collection of items.
• And item can be a string or a number
Lists - recap
• A list holds ordered collection of items.
• And item can be a string or a number
• An item can be a dictionary too
List of dictionaries - Exercise
• Create a dictionary dict_1 with keys one, two, three and values 1, 2
and 3
List of dictionaries - Exercise
• Create a dictionary dict_1 with keys one, two, three and values 1, 2
and 3
• Create a dictionary dict_2 with keys one, two and three and values I,
II, III
List of dictionaries - Exercise
• Create a dictionary dict_1 with keys one, two, three and values 1, 2
and 3
• Create a dictionary dict_2 with keys one, two and three and values I,
II, III
• Create a list with these two dictionaries: list_dict = [dict_1, dict_2]
List of dictionaries - Exercise
• Create a dictionary dict_1 with keys one, two, three and values 1, 2
and 3
• Create a dictionary dict_2 with keys one, two and three and values I,
II, III
• Create a list with these two dictionaries: list_dict = [dict_1, dict_2]
• Print the value corresponding to the key one of first dictionary dict_1
from the list list_dict
List of dictionaries - Exercise
• Create a dictionary dict_1 with keys one, two, three and values 1, 2
and 3
• Create a dictionary dict_2 with keys one, two and three and values I,
II, III
• Create a list with these two dictionaries: list_dict = [dict_1, dict_2]
• Print the value corresponding to the key one of first dictionary dict_1
from the list list_dict
• Print the value corresponding to the key one of second dictionary
dict_2 from the list list_dict
Lets try it!
http://aiclub.world

More Related Content

Similar to Pa1 dictionaries subset

Pa1 session 2
Pa1 session 2 Pa1 session 2
Pa1 session 2
aiclub_slides
 
Python-Basics.pptx
Python-Basics.pptxPython-Basics.pptx
Python-Basics.pptx
TamalSengupta8
 
Pa2 session 1
Pa2 session 1Pa2 session 1
Pa2 session 1
aiclub_slides
 
Python Demo.pptx
Python Demo.pptxPython Demo.pptx
Python Demo.pptx
ParveenShaik21
 
Python Demo.pptx
Python Demo.pptxPython Demo.pptx
Python Demo.pptx
ParveenShaik21
 
introduction to python
 introduction to python introduction to python
introduction to python
Jincy Nelson
 
Chapter7-Introduction to Python.pptx
Chapter7-Introduction to Python.pptxChapter7-Introduction to Python.pptx
Chapter7-Introduction to Python.pptx
lemonchoos
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
Ayshwarya Baburam
 
Programming in python Unit-1 Part-1
Programming in python Unit-1 Part-1Programming in python Unit-1 Part-1
Programming in python Unit-1 Part-1
Vikram Nandini
 
Python Programming and GIS
Python Programming and GISPython Programming and GIS
Python Programming and GIS
John Reiser
 
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
yashar Aliabasi
 
Python Data types like mutable and immutable
Python Data types like mutable and immutablePython Data types like mutable and immutable
Python Data types like mutable and immutable
ramireddyobulakondar
 
Getting started in Python presentation by Laban K
Getting started in Python presentation by Laban KGetting started in Python presentation by Laban K
Getting started in Python presentation by Laban K
GDSCKYAMBOGO
 
Mastering Python lesson3b_for_loops
Mastering Python lesson3b_for_loopsMastering Python lesson3b_for_loops
Mastering Python lesson3b_for_loops
Ruth Marvin
 
Engineering CS 5th Sem Python Module -2.pptx
Engineering CS 5th Sem Python Module -2.pptxEngineering CS 5th Sem Python Module -2.pptx
Engineering CS 5th Sem Python Module -2.pptx
hardii0991
 
Q-Step_WS_02102019_Practical_introduction_to_Python.pdf
Q-Step_WS_02102019_Practical_introduction_to_Python.pdfQ-Step_WS_02102019_Practical_introduction_to_Python.pdf
Q-Step_WS_02102019_Practical_introduction_to_Python.pdf
Michpice
 
unit1 python.pptx
unit1 python.pptxunit1 python.pptx
unit1 python.pptx
TKSanthoshRao
 
CH05.ppt
CH05.pptCH05.ppt
CH05.ppt
afsheenfaiq2
 
1. python programming
1. python programming1. python programming
1. python programming
sreeLekha51
 

Similar to Pa1 dictionaries subset (20)

Pa1 session 2
Pa1 session 2 Pa1 session 2
Pa1 session 2
 
Python-Basics.pptx
Python-Basics.pptxPython-Basics.pptx
Python-Basics.pptx
 
Pa2 session 1
Pa2 session 1Pa2 session 1
Pa2 session 1
 
Python Demo.pptx
Python Demo.pptxPython Demo.pptx
Python Demo.pptx
 
Python Demo.pptx
Python Demo.pptxPython Demo.pptx
Python Demo.pptx
 
introduction to python
 introduction to python introduction to python
introduction to python
 
Python Tutorial Part 1
Python Tutorial Part 1Python Tutorial Part 1
Python Tutorial Part 1
 
Chapter7-Introduction to Python.pptx
Chapter7-Introduction to Python.pptxChapter7-Introduction to Python.pptx
Chapter7-Introduction to Python.pptx
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Programming in python Unit-1 Part-1
Programming in python Unit-1 Part-1Programming in python Unit-1 Part-1
Programming in python Unit-1 Part-1
 
Python Programming and GIS
Python Programming and GISPython Programming and GIS
Python Programming and GIS
 
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 Data types like mutable and immutable
Python Data types like mutable and immutablePython Data types like mutable and immutable
Python Data types like mutable and immutable
 
Getting started in Python presentation by Laban K
Getting started in Python presentation by Laban KGetting started in Python presentation by Laban K
Getting started in Python presentation by Laban K
 
Mastering Python lesson3b_for_loops
Mastering Python lesson3b_for_loopsMastering Python lesson3b_for_loops
Mastering Python lesson3b_for_loops
 
Engineering CS 5th Sem Python Module -2.pptx
Engineering CS 5th Sem Python Module -2.pptxEngineering CS 5th Sem Python Module -2.pptx
Engineering CS 5th Sem Python Module -2.pptx
 
Q-Step_WS_02102019_Practical_introduction_to_Python.pdf
Q-Step_WS_02102019_Practical_introduction_to_Python.pdfQ-Step_WS_02102019_Practical_introduction_to_Python.pdf
Q-Step_WS_02102019_Practical_introduction_to_Python.pdf
 
unit1 python.pptx
unit1 python.pptxunit1 python.pptx
unit1 python.pptx
 
CH05.ppt
CH05.pptCH05.ppt
CH05.ppt
 
1. python programming
1. python programming1. python programming
1. python programming
 

More from aiclub_slides

Linear regression middleschool
Linear regression middleschoolLinear regression middleschool
Linear regression middleschool
aiclub_slides
 
Pa2 project template
Pa2 project templatePa2 project template
Pa2 project template
aiclub_slides
 
Knn intro advanced_middleschool
Knn intro advanced_middleschoolKnn intro advanced_middleschool
Knn intro advanced_middleschool
aiclub_slides
 
M1 regression metrics_middleschool
M1 regression metrics_middleschoolM1 regression metrics_middleschool
M1 regression metrics_middleschool
aiclub_slides
 
Pa1 json requests
Pa1 json requestsPa1 json requests
Pa1 json requests
aiclub_slides
 
Mnist images
Mnist imagesMnist images
Mnist images
aiclub_slides
 
Mnist images
Mnist imagesMnist images
Mnist images
aiclub_slides
 
Ai in real life face detection
Ai in real life   face detectionAi in real life   face detection
Ai in real life face detection
aiclub_slides
 
Res net high level intro
Res net high level introRes net high level intro
Res net high level intro
aiclub_slides
 
Neural networks and flattened images
Neural networks and flattened imagesNeural networks and flattened images
Neural networks and flattened images
aiclub_slides
 
What is a_neural_network
What is a_neural_networkWhat is a_neural_network
What is a_neural_network
aiclub_slides
 
How neural networks learn part iii
How neural networks learn part iiiHow neural networks learn part iii
How neural networks learn part iii
aiclub_slides
 
Introduction to deep learning image classification
Introduction to deep learning   image classificationIntroduction to deep learning   image classification
Introduction to deep learning image classification
aiclub_slides
 
Accuracy middleschool
Accuracy middleschoolAccuracy middleschool
Accuracy middleschool
aiclub_slides
 
Introduction to classification_middleschool
Introduction to classification_middleschoolIntroduction to classification_middleschool
Introduction to classification_middleschool
aiclub_slides
 
Introduction to the cloud
Introduction to the cloudIntroduction to the cloud
Introduction to the cloud
aiclub_slides
 
Basics of data
Basics of dataBasics of data
Basics of data
aiclub_slides
 
Ai basics
Ai basicsAi basics
Ai basics
aiclub_slides
 
Ai lifecycle and navigator
Ai lifecycle and navigatorAi lifecycle and navigator
Ai lifecycle and navigator
aiclub_slides
 

More from aiclub_slides (20)

Linear regression middleschool
Linear regression middleschoolLinear regression middleschool
Linear regression middleschool
 
Pa2 project template
Pa2 project templatePa2 project template
Pa2 project template
 
Knn intro advanced_middleschool
Knn intro advanced_middleschoolKnn intro advanced_middleschool
Knn intro advanced_middleschool
 
M1 regression metrics_middleschool
M1 regression metrics_middleschoolM1 regression metrics_middleschool
M1 regression metrics_middleschool
 
Pa1 json requests
Pa1 json requestsPa1 json requests
Pa1 json requests
 
Mnist images
Mnist imagesMnist images
Mnist images
 
Mnist images
Mnist imagesMnist images
Mnist images
 
Ai in real life face detection
Ai in real life   face detectionAi in real life   face detection
Ai in real life face detection
 
Cnn
CnnCnn
Cnn
 
Res net high level intro
Res net high level introRes net high level intro
Res net high level intro
 
Neural networks and flattened images
Neural networks and flattened imagesNeural networks and flattened images
Neural networks and flattened images
 
What is a_neural_network
What is a_neural_networkWhat is a_neural_network
What is a_neural_network
 
How neural networks learn part iii
How neural networks learn part iiiHow neural networks learn part iii
How neural networks learn part iii
 
Introduction to deep learning image classification
Introduction to deep learning   image classificationIntroduction to deep learning   image classification
Introduction to deep learning image classification
 
Accuracy middleschool
Accuracy middleschoolAccuracy middleschool
Accuracy middleschool
 
Introduction to classification_middleschool
Introduction to classification_middleschoolIntroduction to classification_middleschool
Introduction to classification_middleschool
 
Introduction to the cloud
Introduction to the cloudIntroduction to the cloud
Introduction to the cloud
 
Basics of data
Basics of dataBasics of data
Basics of data
 
Ai basics
Ai basicsAi basics
Ai basics
 
Ai lifecycle and navigator
Ai lifecycle and navigatorAi lifecycle and navigator
Ai lifecycle and navigator
 

Recently uploaded

How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 

Recently uploaded (20)

How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 

Pa1 dictionaries subset

  • 1. Python with AI – I Dictionaries
  • 2. Dictionaries in python • A dictionary consists of two things (a) keys (b) values
  • 3. Dictionaries in python • A dictionary consists of two things (a) keys (b) values • Use strings to represent keys
  • 4. Dictionaries in python • A dictionary consists of two things (a) keys (b) values • Use strings to represent keys • Values can be anything
  • 5. Dictionaries in python • A dictionary consists of two things (a) keys (b) values • Use strings to represent keys • Values can be anything • There are many ways to create a dictionary, we will look at one way to do it:
  • 6. Dictionaries in python • A dictionary consists of two things (a) keys (b) values • Use strings to represent keys • Values can be anything
  • 7. Print the entire dictionary • Print(my_dict) • Please inspect the format in which it is printed. • Where do the : symbols show up?
  • 8. Print the entire dictionary • Print(my_dict) • Please inspect the format in which it is printed. • Where do the colon (:) symbols show up? • Where do the commas (,) show up?
  • 9. Exercise • Create a dictionary, where the keys are one, two and the corresponding values are 1 and 2
  • 10. Exercise • Create a dictionary, where the keys are one, two and the corresponding values are 1 and 2 • Create a dictionary, where the keys are aiclub, PA1, M1 and the corresponding values are website, python class, AI class.
  • 11. Dictionaries in python • Print a value in a dictionary
  • 12. Exercise • Print values of the first dictionary you created • Print value corresponding to the key one • Print value corresponding to the key two
  • 13. Exercise • Print values of the first dictionary you created • Print value corresponding to the key one • Print value corresponding to the key two • Print values of the second dictionary you created • Print value corresponding to the key aiclub • Print value corresponding to the key M1 • Print value corresponding to the key PA1
  • 14. Dictionaries in python • Print a value in a dictionary • Delete a value in a dictionary
  • 15. Exercise • Delete and print values of new dictionaries • For the first dictionary • Delete the value corresponding to the key one • Print the value of dictionary • For the second dictionary • Delete the value corresponding to the key aiclub • Print the value of dictionary
  • 16. Dictionaries in python • Print a value in a dictionary • Delete a value in a dictionary • Print all keys of a dictionary
  • 17. Exercise • Instead of printing the entire dictionary, please print only the keys of the two dictionaries you created
  • 18. Dictionaries in python • Print a value in a dictionary • Delete a value in a dictionary • Print all keys of a dictionary • Add values to a dictionary
  • 19. Exercise • Add the keys that you deleted from the 2 dictionaries back • Print the two dictionaries
  • 20. loops and dictionaries – use together • Loops - recap • The for loop is of the following form. • The range indicates the number of times a statement will be implemented. • There is a colon value to indicate the start of for loop. • The code below is executed many times. The number of times it gets executed depends on the values specified in range. • Note that the code below the for statement is indented.
  • 21. Loops - recap • The for loop is of the following form. • Try different values inside the brackets of range • Note that everything with indentation below for loop is executed as a part of the loop.
  • 22. Loops and Dictionaries • Exercise a) Create a random number in the limits 0 and 10 using a random number generator
  • 23. Loops and Dictionaries • Exercise a) Create a random number in the limits 0 and 10 using a random number generator b) Create a list of length 10 using the random number generator (pick any limits) – please use a loop
  • 24. Loops and Dictionaries • Exercise a) Create a random number in the limits 0 and 10 using a random number generator b) Create a list of length 10 using the random number generator (pick any limits) – please use a loop c) Now create a dictionary using the values of the list as keys and the corresponding values for each key can be another random number (pick any limits).
  • 25. Lists - recap • A list holds ordered collection of items. • And item can be a string or a number
  • 26. Lists - recap • A list holds ordered collection of items. • And item can be a string or a number • An item can be a dictionary too
  • 27. List of dictionaries - Exercise • Create a dictionary dict_1 with keys one, two, three and values 1, 2 and 3
  • 28. List of dictionaries - Exercise • Create a dictionary dict_1 with keys one, two, three and values 1, 2 and 3 • Create a dictionary dict_2 with keys one, two and three and values I, II, III
  • 29. List of dictionaries - Exercise • Create a dictionary dict_1 with keys one, two, three and values 1, 2 and 3 • Create a dictionary dict_2 with keys one, two and three and values I, II, III • Create a list with these two dictionaries: list_dict = [dict_1, dict_2]
  • 30. List of dictionaries - Exercise • Create a dictionary dict_1 with keys one, two, three and values 1, 2 and 3 • Create a dictionary dict_2 with keys one, two and three and values I, II, III • Create a list with these two dictionaries: list_dict = [dict_1, dict_2] • Print the value corresponding to the key one of first dictionary dict_1 from the list list_dict
  • 31. List of dictionaries - Exercise • Create a dictionary dict_1 with keys one, two, three and values 1, 2 and 3 • Create a dictionary dict_2 with keys one, two and three and values I, II, III • Create a list with these two dictionaries: list_dict = [dict_1, dict_2] • Print the value corresponding to the key one of first dictionary dict_1 from the list list_dict • Print the value corresponding to the key one of second dictionary dict_2 from the list list_dict