Home  >  Article  >  Backend Development  >  How to access values ​​in python dictionary? (Example analysis)

How to access values ​​in python dictionary? (Example analysis)

乌拉乌拉~
乌拉乌拉~Original
2018-08-18 14:06:132986browse

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 = {&#39;Name&#39;: &#39;Zara&#39;, &#39;Age&#39;: 7, &#39;Class&#39;: &#39;First&#39;};<br/><br/>print "dict[&#39;Name&#39;]: ", dict[&#39;Name&#39;];<br/>print "dict[&#39;Age&#39;]: ", dict[&#39;Age&#39;];<br/></span>

Output result of the above example:

<span style="color: rgb(0, 0, 0);">dict[&#39;Name&#39;]:  Zara<br/>dict[&#39;Age&#39;]:  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 = {&#39;Name&#39;: &#39;Zara&#39;, &#39;Age&#39;: 7, &#39;Class&#39;: &#39;First&#39;}; <br/>print "dict[&#39;Alice&#39;]: ", dict[&#39;Alice&#39;];<br/></span>

Above Example output result:

<span style="color: rgb(0, 0, 0);">dict[&#39;Alice&#39;]: <br/>Traceback (most recent call last):<br/>  File "test.py", line 5, in <module><br/>    print "dict[&#39;Alice&#39;]: ", dict[&#39;Alice&#39;];<br/>KeyError: &#39;Alice&#39;<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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn