视频位置:138:40
字典
用大括号定义字典。{}
customer = {
"name":"John Smith",
"age":30,
"is_verified":True
}
print(customer["name"])
输出为:
John Smith
如果是没有的值会提示出错。区分大小写。
customer = {
"name":"John Smith",
"age":30,
"is_verified":True
}
print(customer["Name"]) #此处N大写了
会提示:
print(customer["Name"])
KeyError: 'Name'
可以用get功能,防止程序报错。
customer = {
"name":"John Smith",
"age":30,
"is_verified":True
}
print(customer.get("Name")) #此处N大写了
print(customer.get("age"))
输出为:
None
30
<