您当前的位置:首页 > IT编程 > python
| C语言 | Java | VB | VC | python | Android | TensorFlow | C++ | oracle | 学术与代码 | cnn卷积神经网络 | gnn | 图像修复 | Keras | 数据集 | Neo4j | 自然语言处理 | 深度学习 | 医学CAD | 医学影像 | 超参数 | pointnet | pytorch |

自学教程:Python3 字典 in 操作符

51自学网 2023-01-08 20:10:44
  python
这篇教程Python3 字典 in 操作符写得很实用,希望能帮到您。

描述

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 存在

Python3 字典 get() 方法
Python3 字典 items() 方法
51自学网,即我要自学网,自学EXCEL、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。
京ICP备13026421号-1