56 / 100

In Python, you can add a key-value pair to a dictionary using the following syntax:


  my_dict[key] = value

Here’s an example:

# Create an empty dictionary


  my_dict = {}

# Add key-value pairs to the dictionary


my_dict['grapes'] = 3
my_dict['banana'] = 5
my_dict['apple'] = 2

# Print the dictionary


print(my_dict) 

Output


print(my_dict) 

 

{‘grapes’: 3, ‘banana’: 5, ‘apple’: 2}

In this example, we created an empty dictionary my_dict and then added three key-value pairs to it using the my_dict[key] = value syntax. The keys are strings (‘grapes’, ‘banana’, and ‘apple’), and the values are integers (3, 5, and 2). Finally, we printed the dictionary to verify that the key-value pairs were added correctly.