How to Use Dictionary Access Method in PythonPython Basics


The function of the operator[] is used to access the dictionary values.

Example

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

dict = {'Name': 'Gerryzhang', 'Age': 24, 'Sex': 'man','ID':123}

print("dict['Name']: ", dict['Name'])
print("dict['Age']: ", dict['Age'])
print("dict['Sex']: ", dict['Sex'])
print("dict['ID']: ", dict['ID'])

Output:

dict['Name']:  Gerryzhang
dict['Age']:  24
dict['Sex']:  man
dict['ID']:  123

Syntax

dict['key']

Parameters

Name Description
key The key value in the dictionary.

Return Value

It returns the corresponding value of the key.

© 2024 Cheerlearn.com, all rights reserved. Privacy Policy | Contact Us