Python3 字典 in 操作符
Python 3 教程
· 2019-02-06 15:57:45
描述
Python 字典 in 操作符用于判断键是否存在于字典中,如果键在字典 dict 里返回 true,否则返回 false。
而 not in 操作符刚好相反,如果键在字典 dict 里返回 false,否则返回 true。
语法
in 操作符语法:
key in dict
参数
- key -- 要在字典中查找的键。
返回值
如果键在字典里返回true,否则返回false。
实例
以下实例展示了 in 操作符在字典中的使用方法:
实例(Python 3.0+)
#!/usr/bin/python3
dict = {'Name': 'Codercto', 'Age': 7}
# 检测键 Age 是否存在
if 'Age' in dict:
print("键 Age 存在")
else :
print("键 Age 不存在")
# 检测键 Sex 是否存在
if 'Sex' in dict:
print("键 Sex 存在")
else :
print("键 Sex 不存在")
# not in
# 检测键 Age 是否存在
if 'Age' not in dict:
print("键 Age 不存在")
else :
print("键 Age 存在")
以上实例输出结果为:
键 Age 存在 键 Sex 不存在 键 Age 存在
点击查看所有 Python 3 教程 文章: https://codercto.com/courses/l/10.html
Python带我起飞
李金洪 / 电子工业出版社 / 2018-6 / 79
《Python带我起飞——入门、进阶、商业实战》针对Python 3.5 以上版本,采用“理论+实践”的形式编写,通过大量的实例(共42 个),全面而深入地讲解“Python 基础语法”和“Python 项目应用”两方面内容。书中的实例具有很强的实用性,如对医疗影像数据进行分析、制作爬虫获取股票信息、自动化实例、从一组看似混乱的数据中找出规律、制作人脸识别系统等。 《Python带我起飞——......一起来看看 《Python带我起飞》 这本书的介绍吧!