Python的哈希映射:字典
# 创建一个字典
my_dict = {'name': 'John', 'age': 25, 'city': 'New York'}
# 访问字典中的值
print(my_dict['name']) # 输出: John
# 添加新的键值对
my_dict['gender'] = 'Male'
# 更新字典中的值
my_dict['age'] = 26
# 删除键值对
del my_dict['city']
# 检查键是否存在
if 'name' in my_dict:
print('Name is present in the dictionary.')
# 遍历字典
for key, value in my_dict.items():
print(f'{key}: {value}')