欢迎您 本站地址:  
返回首页   返回编程教学   哲理故事  主机域名源码  数据库相关源码  网络开发源码  三国历史  除湿利水茶  学习CSS  学习Python2.x 

Python3 字典 in 操作符

Python3 字典 Python3 字典


描述

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

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

语法

in 操作符语法:

key in dict

参数

返回值

如果键在字典里返回true,否则返回false。

实例

以下实例展示了 in 操作符在字典中的使用方法:

实例(Python 3.0+)

#!/usr/bin/python3 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 存在")

以上实例输出结果为:

键 Age 存在
键 Sex 不存在
键 Age 存在

Python3 字典 Python3 字典

小库提示

扫描下方二维码,访问手机版。