Dictionary Basics¶
-
Write code to create a dictionary
grades
that maps a student’s name to their assignment grade.grades
has two entries in it:Sam Reimer –> 84.3
Steve Bardeau –> 90.5
You create a dictionary with{
and}
, and you put entries in it separating the key from the value with:
.
-
Write code to retrieve from dictionary
grades
the grade of ‘Colette Gonzales’. Store her grade in a variablecolette_score
.Suppose you have a dictionaryaDict
. You can print the value with keykey
by doingprint(aDict[key])
-
Write code to map floating point numbers to strings in a dictionary posOrNeg, initially containing these entries:
-3.14 –> ‘negative’
40.79 –> ‘positive’
0.01 –> ‘positive’
You create a dictionary with{
and}
, and you put entries in it separating the key from the value with:
.
-
Given a dictionary
grades
, write code to add an entry mapping John Fleenor to the grade 92.5.After a dictionary has been created you add entries using the syntax:
aDict[key] = value
-
Given a dictionary
grades
containing a mapping of Sam Reimer to the grade 84.3, update the dictionary to change the grade to 94.3.After a dictionary has been created you can update an entry using the syntax:
aDict[key] = value
(Just like creating a new entry.)
-
Given a dictionary
grades
containing a mapping of Sam Reimer to a floating point number, write code to add 1 to the grade.Your code will have to access the value, add one to 1, and then update the entry. This can be done in one line of code.