Python Dictionary Practice Exercise
1. Create a Dictionary with Key-Value Pairs
Write a program that creates a dictionary with the following key-value pairs:
- name: 'John'
- age: 30
- city: 'New York'
Example Output: {'name': 'John', 'age': 30, 'city': 'New York'}
Solution:
```python
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
print(my_dict) # Output: {'name': 'John', 'age': 30, 'city': 'New York'}
```
2. Access a Value Using a Key
Write a program that accesses the value for the key 'age' from the dictionary.
Example Input: {'name': 'John', 'age': 30, 'city': 'New York'}
Example Output: 30
Solution:
```python
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
print(my_dict['age']) # Output: 30
```
3. Add a New Key-Value Pair to a Dictionary
Write a program that adds a new key-value pair to the dictionary, for example, adding 'job'
with the value 'Engineer'.
Example Input: {'name': 'John', 'age': 30, 'city': 'New York'}
Example Output: {'name': 'John', 'age': 30, 'city': 'New York', 'job': 'Engineer'}
Solution:
```python
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
my_dict['job'] = 'Engineer'
print(my_dict) # Output: {'name': 'John', 'age': 30, 'city': 'New York', 'job': 'Engineer'}
```
4. Update the Value of an Existing Key
Write a program that updates the value of the 'age' key in the dictionary to 35.
Example Input: {'name': 'John', 'age': 30, 'city': 'New York'}
Example Output: {'name': 'John', 'age': 35, 'city': 'New York'}
Solution:
```python
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
my_dict['age'] = 35
print(my_dict) # Output: {'name': 'John', 'age': 35, 'city': 'New York'}
```
5. Remove a Key-Value Pair from a Dictionary
Write a program that removes the key-value pair with the key 'city' from the dictionary.
Example Input: {'name': 'John', 'age': 30, 'city': 'New York'}
Example Output: {'name': 'John', 'age': 30}
Solution:
```python
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
del my_dict['city']
print(my_dict) # Output: {'name': 'John', 'age': 30}
```
6. Check if a Key Exists in a Dictionary
Write a program that checks if the key 'name' exists in the dictionary and prints a message
accordingly.
Example Input: {'name': 'John', 'age': 30}
Example Output: The key 'name' exists in the dictionary.
Solution:
```python
my_dict = {'name': 'John', 'age': 30}
if 'name' in my_dict:
print("The key 'name' exists in the dictionary.")
else:
print("The key 'name' does not exist in the dictionary.")
```
7. Get a Value Using `get()` Method
Write a program that uses the `get()` method to access the value of the 'city' key.
Example Input: {'name': 'John', 'age': 30, 'city': 'New York'}
Example Output: New York
Solution:
```python
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
print(my_dict.get('city')) # Output: New York
```
8. Get All Keys, Values, and Items
Write a program that retrieves all the keys, values, and items (key-value pairs) from the
dictionary.
Example Input: {'name': 'John', 'age': 30, 'city': 'New York'}
Example Output: Keys: ['name', 'age', 'city'], Values: ['John', 30, 'New York'], Items: [('name',
'John'), ('age', 30), ('city', 'New York')]
Solution:
```python
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
print('Keys:', list(my_dict.keys()))
print('Values:', list(my_dict.values()))
print('Items:', list(my_dict.items()))
```
9. Merge Two Dictionaries
Write a program that merges two dictionaries into one.
Example Input: Dictionary 1: {'name': 'John', 'age': 30}, Dictionary 2: {'city': 'New York', 'job':
'Engineer'}
Example Output: {'name': 'John', 'age': 30, 'city': 'New York', 'job': 'Engineer'}
Solution:
```python
dict1 = {'name': 'John', 'age': 30}
dict2 = {'city': 'New York', 'job': 'Engineer'}
[Link](dict2)
print(dict1) # Output: {'name': 'John', 'age': 30, 'city': 'New York', 'job': 'Engineer'}
```
10. Clear All Elements in a Dictionary
Write a program that clears all the elements in a dictionary.
Example Input: {'name': 'John', 'age': 30, 'city': 'New York'}
Example Output: {}
Solution:
```python
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
my_dict.clear()
print(my_dict) # Output: {}
```
11. Nested Dictionaries
Write a program that accesses a value inside a nested dictionary.
Example Input: {'person': {'name': 'John', 'age': 30, 'city': 'New York'}}
Example Output: John
Solution:
```python
my_dict = {'person': {'name': 'John', 'age': 30, 'city': 'New York'}}
print(my_dict['person']['name']) # Output: John
```
12. Create a Dictionary from Two Lists
Write a program that creates a dictionary from two lists: one for keys and one for values.
Example Input: Keys: ['name', 'age'], Values: ['John', 30]
Example Output: {'name': 'John', 'age': 30}
Solution:
```python
keys = ['name', 'age']
values = ['John', 30]
my_dict = dict(zip(keys, values))
print(my_dict) # Output: {'name': 'John', 'age': 30}
```
13. Check if Two Dictionaries are Equal
Write a program that checks if two dictionaries are equal (same keys and values).
Example Input: dict1: {'name': 'John', 'age': 30}, dict2: {'name': 'John', 'age': 30}
Example Output: The dictionaries are equal.
Solution:
```python
dict1 = {'name': 'John', 'age': 30}
dict2 = {'name': 'John', 'age': 30}
if dict1 == dict2:
print('The dictionaries are equal.')
else:
print('The dictionaries are not equal.')
```