SoFunction
Updated on 2024-11-20

Python dictionary get method usage analysis

This article example describes python dictionary get() method usage. Shared for your reference. Specific analysis is as follows:

If we need to get the dictionary value, we have two methods, one is through dict['key'], the other is the () method.

Here we are sharing the get() method of dictionary.

Here we can use the dictionary to make a small game, assuming that the user in the terminal to enter the string: "1" or "2" or "3", return the corresponding content, if it is to enter the other, then return "error".

>>> info = {'1':'first','2':'second','3':'third'}
>>> number = raw_input('input type you number:')
input type you number:3
>>> print (number,'error')
third
>>> number = raw_input('input type you number:')
input type you number:4
>>> print (number,'error')
error

I hope that what I have described in this article will help you in your Python programming.