Latest web development tutorials

Python3 operator in dictionary

Python3 dictionary Python3 dictionary


description

Python dictionary in operator is used to determine whether the key exists in the dictionary, if the key is in the dictionary dict returns true, otherwise returns false.

grammar

has_key () method syntax:

key in dict

parameter

  • key - you want to find in the dictionary key.

return value

If the key is in the dictionary returns true, otherwise it returns false.

Examples

The following example shows the in operator to use in the dictionary:

#!/usr/bin/python3

dict = {'Name': 'w3big', 'Age': 7}

# 检测键 Age 是否存在
if  'Age' in dict:
    print("键 Age 存在")
else :
	print("键 Age 不存在")

# 检测键 Sex 是否存在
if  'Sex' in dict:
    print("键 Sex 存在")
else :
    print("键 Sex 不存在")

The above example output is:

键 Age 存在
键 Sex 不存在

Python3 dictionary Python3 dictionary