Dictionaries are a powerful data structure in Python that allow you to store and manipulate collections of key-value pairs. In a dictionary, each key is associated with a value, and the keys and values can be of any data type.


#Dictionary Example
phonebook = {}
phonebook["John"] = 938477566
phonebook["Jack"] = 938377264
phonebook["Jill"] = 947662781
print(phonebook)

You can also add, modify, and delete key-value pairs in a dictionary using various methods and operations. For example, you can use the update() method to add or modify key-value pairs, and the del keyword to delete key-value pairs.


#Dictionary Alternative Example
phonebook = {
    "John" : 938477566,
    "Jack" : 938377264,
    "Jill" : 947662781
}
print(phonebook)

#Removing from a Dictionary
phonebook = {
   "John" : 938477566,
   "Jack" : 938377264,
   "Jill" : 947662781
}
del phonebook["John"]
print(phonebook)

#Alternatively
phonebook = {
   "John" : 938477566,
   "Jack" : 938377264,
   "Jill" : 947662781
}
phonebook.pop("John")
print(phonebook)

Dictionaries can be used in a wide range of applications, from data analysis and manipulation to web development and data science. They are particularly useful for storing and manipulating structured data, such as JSON and XML files.


#Dictionary Iterations
phonebook = {"John" : 938477566,"Jack" : 938377264,"Jill" : 947662781}
for name, number in phonebook.items():
    print("Phone number of %s is %d" % (name, number))

In summary, dictionaries are a powerful data structure in Python that allow you to store and manipulate collections of key-value pairs.

Dictionary Exercise Solution


#Code Completed
phonebook = {  
    "John" : 938477566,
    "Jack" : 938377264,
    "Jill" : 947662781
}  

# your code goes here
phonebook["Jake"] = 938273443  
del phonebook["Jill"]  

# testing code
if "Jake" in phonebook:  
    print("Jake is listed in the phonebook.")
    
if "Jill" not in phonebook:      
    print("Jill is not listed in the phonebook.")  

With a wide range of methods and operations for adding, modifying, and deleting key-value pairs, dictionaries are a versatile tool for a wide range of applications. Whether you’re working with structured data or building web applications, dictionaries are an essential part of any Python developer’s toolkit.