Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Dictionary has_key() method

Python Dictionary has_key() method

Python Dictionary Python Dictionary


Description

The Python Dictionary has_key() function checks whether a key exists in the dictionary, returning true if the key is in dict and false otherwise.

**Note:** Python 3.x does not support this method.

Syntax

has_key() method syntax:



dict.has_key(key)

Parameters

Return Value

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

Example

The following example shows how to use the has_key() function:

Example

#!/usr/bin/python

tinydict

=

{

'Name'

:

'Zara'

,

'Age'

:

7

}

print

"Value : %s"

%  tinydict.

has_key

(

'Age'

)

print

"Value : %s"

%  tinydict.

has_key

(

'Sex'

)

The output of the above example is:



Value : True

Value : False

Python Dictionary Python Dictionary