Use of the python dictionary built-in get method

Source: Internet
Author: User
Python dictionary built-in method get application, if we need to obtain the dictionary value, we have two methods, one is through dict [& amp; & amp; 39; key & amp; 39;]. The other method is the dictget () method. Python dictionary built-in method get application. if we need to obtain the dictionary value, we have two methods: dict ['key'] and dict. get () method.

Today we will share with you the get () method of the dictionary.

Here we can use a dictionary to make a small game. assume that the user enters the string "1", "2", or "3" on the terminal and returns the corresponding content, "error" is returned"

Some friends may use the if elif else judgment statement to perform operations, which is indeed acceptable, but tedious. It is very convenient to recommend a dictionary get () method.

Info = {'1': 'First ', '2': 'second', '3': 'third '}

Number = raw_input ('input type you number :')

Print info. get (number, 'error ')

In this way, a line of code get may be able to complete several lines of code in the python judgment statement.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.