How to add key-value pairs to an empty dictionary in Python.
To add key-value pairs to an empty dictionary, you can use the following two methods:
Option 1:
Method 1: Adding key-value pairs using the index operator[].
# 创建一个空字典
my_dict = {}
# 添加键值对
my_dict["key1"] = "value1"
my_dict["key2"] = "value2"
print(my_dict)
Option 2: Adding key-value pairs using the update() method.
# 创建一个空字典
my_dict = {}
# 使用update()方法添加键值对
my_dict.update({"key1": "value1", "key2": "value2"})
print(my_dict)
You can add key-value pairs to an empty dictionary using any method.