Python3 Dictionary in Operator

Python3 字典Python3 Dictionary


Description

Python DictionaryinThe in operator is used to determine whether a key exists in the dictionary. If the key is in the dictionary dict, it returns true, otherwise it returns false.

andnot inThe not in operator is exactly the opposite. If the key is in the dictionary dict, it returns false, otherwise it returns true.

Syntax

Syntax of the in operator:

key in dict

Parameters

  • key -- The key to search for in the dictionary.

Return Value

Returns true if the key is in the dictionary, otherwise returns false.

Examples

The following example demonstrates how to use the in operator with dictionaries:

Example (Python 3.0+)

#!/usr/bin/python3 thisdict = {'Name': 'Example', 'Age': 7} # Check whether key Age exists if 'Age' in thisdict: print("Key Age exists") else : print("Key Age does not exist") # Check whether key Sex exists if 'Sex' in thisdict: print("Key Sex exists") else : print("Key Sex does not exist") # not in # Check whether key Age does not exist if 'Age' not in thisdict: print("Key Age does not exist") else : print("Key Age exists")

The output of the above example is:

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

Python3 字典Python3 Dictionary

Other Extensions