How to Use Dictionary in Method in PythonPython Basics


The operator in is used to check if the key exists in the dictionary.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

dict = {'Name': 'Runoob', 'Age': 11}

if 'Age' in dict:
    print("dict has the key age")
else:
    print("dict has not the key age")

Output:

dict has the key age

Syntax

key in dict

Parameters

Name Description
key The key to looking up in the dictionary.

Return Value

It returns True if the key exists in the dictionary, otherwise False.

© 2024 Learnwithgpt.org, all rights reserved. Privacy Policy | Contact Us