Editor settings

General settings

Save settings

Learn to program with Python

Adding values to a dictionary #

When we assign a dictionary with a key that does not exist, the value will be added to the dictionary.

For example:

my_dict = {}
my_dict["country"] = "USA"
print(my_dict) # output: {'country': 'USA'}

In the example above, we created an empty dictionary. We then assigned the value "USA" to the key "country" with the code my_dict["country"] = "USA". The value is then added to the dictionary.


Exercise #

We have created a dictionary job_position. Add a new key "salary" and set it to any number value.

Tests #

  • job_position["salary"] should be a number value
Output will be displayed here