Home >Backend Development >Python Tutorial >How to access values in python dictionary? (Example analysis)
In today's article, we will learn about dictionaries in python. In this article, I will explain python dictionary access, and python usage. Access dictionary Example analysis. Okay, without further ado, let’s get started with the article.
Access the values in the dictionary
Place the corresponding keys in familiar square brackets, as in the following example:
<span style="color: rgb(0, 0, 0);"># !/usr/bin/python<br/><br/>dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};<br/><br/>print "dict['Name']: ", dict['Name'];<br/>print "dict['Age']: ", dict['Age'];<br/></span>
Output result of the above example:
<span style="color: rgb(0, 0, 0);">dict['Name']: Zara<br/>dict['Age']: 7<br/></span>
If you access data with a key that is not in the dictionary, the error will be output as follows:
<span style="color: rgb(0, 0, 0);">#!/usr/bin/python<br/><br/> dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}; <br/>print "dict['Alice']: ", dict['Alice'];<br/></span>
Above Example output result:
<span style="color: rgb(0, 0, 0);">dict['Alice']: <br/>Traceback (most recent call last):<br/> File "test.py", line 5, in <module><br/> print "dict['Alice']: ", dict['Alice'];<br/>KeyError: 'Alice'<br/></span>
The above is all the content of this article, Dictionary access in python. I hope what I said and the examples I gave can be helpful to you.
For more related knowledge, please visit the Python tutorial column on the php Chinese website.
The above is the detailed content of How to access values in python dictionary? (Example analysis). For more information, please follow other related articles on the PHP Chinese website!