# 示例代码:使用 dict.get() 方法
# 创建一个字典
my_dict = {'name': 'Alice', 'age': 25, 'city': 'Beijing'}
# 使用 get() 方法获取键对应的值
name = my_dict.get('name') # 返回 'Alice'
print(f"Name: {name}")
age = my_dict.get('age') # 返回 25
print(f"Age: {age}")
# 如果键不存在,返回 None
country = my_dict.get('country') # 返回 None
print(f"Country: {country}")
# 如果键不存在,可以指定默认值
country_with_default = my_dict.get('country', 'Unknown')
print(f"Country with default: {country_with_default}")
dict.get(key[, default])
是 Python 字典的一个方法,用于获取指定键的值。None
。下一篇:python repr
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站