Python dictionary (dict) Get method application

Source: Internet
Author: User

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

What we are sharing today is the dictionary's Get () method.

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

Some friends here may use if elif else judgment statement to operate, indeed can, but more cumbersome. It is very convenient to recommend the Get () method of a dictionary to everyone.

info = {' 1 ': ' First ', ' 2 ': ' Second ', ' 3 ': ' Third '}
Number = raw_input (' input type ' you number: ')
Print info.get (number, ' ERROR ')

This way, a single line of code get will be able to complete the Python judgment statement a few lines of code.

Python dictionary (dict) Get method application

Related Article

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.