CSC 171 - Introduction to Computer Programming

Lab Assignment #24 - Some Basic Dictionary Operations

Due Monday, December 4, 2023

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:

  1. Create a dictionary called myCar that has the following keys and their corresponding values:
  2. make Honda
    model Accord
    year 2018
    color blue
  3. Print the values for myCar (not the keys)
  4. Print the keys for myCar (not the values)
  5. Add the license plate number to the dictionary
  6. Change the model to Civic
  7. See if passengers is a key in this dictionary
  8. See if color is a key in this dictionary
  9. Replace all the values with the following information: <./tr>
    make Chevrolet
    model Malibu
    year 2009
    color white

    [Back to the Lab Assignment List]