A dictionary contains entries that include a key and a value. For
example, I can use a dictionary to hold certain information about my car:
my_dict = {'make': 'Honda', 'model': 'CR-V'}
The dictionary has keys ('make'
and
'model'
in this case) and values
('Honda'
and 'CR-V'
)
I can add additional information by assigning a value:
my_dict['color'] = 'red'
If I want to see how many entries I have in the dictionary, I can write
print(len(my_dict))
If I want to change an entry, I simply assign it a new value:
my_dict['color'] = 'black'
I can use the key-value pairs by writing:
for key, value in my_dict:
print(key, value)
I can use just the key by writing:
for key in my_dict:
print(key)
I can use just the value by writing:
for value in my_dict:
print(value)
What you will do is to use the dictionary operations to:
make | Honda |
model | Accord |
year | 2018 |
color | blue |
myCar
(not the keys)
myCar
(not the values)
Civic
color
is a key in this
dictionarymake | Chevrolet |
model | Malibu | <./tr>
year | 2009 |
color | white |