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
Getting Started with Python
Data Types
Python Functions
Statements in Python
Basic Debugging in Python
Basic Algorithm
Object-Oriented Programming
Error Handling
Intermediate Algorithm
Python Modules