20220319-day8:in判断字典键是否存在

Python 字典 in 操作符用于判断键是否存在于字典中,如果键在字典 dict 里返回 true,否则返回 false。

not in 操作符刚好相反,如果键在字典 dict 里返回 false,否则返回 true。

 

in 操作符语法:

key in dict

实例

thisdict = {'Name': 'Runoob', 'Age': 7} 

# 检测键 Age 是否存在

if 'Age' in thisdict: print("键 Age 存在")

else : print("键 Age 不存在")

# 检测键 Sex 是否存在

if 'Sex' in thisdict: print("键 Sex 存在")

else : print("键 Sex 不存在")

# not in

# 检测键 Age 是否存在

if 'Age' not in thisdict: print("键 Age 不存在")

else : print("键 Age 存在")
posted @ 2022-03-19 15:13  美丽小鱼  阅读(467)  评论(0)    收藏  举报