Implement switch functionality in Python __python

Source: Internet
Author: User
Preface

In the course of learning Python today, I found that Python did not switch this syntax. So I wanted to do it in Python. Body

In this article, we simulate the use of the switch to the normal database additions and deletions to the operation of the corresponding, such as ' Select
Corresponds to ' Select action ' and so on. 1. Simple If-else

As we know, there are if statements in Python, and when you learn C, the alternative that comes out when you learn if-else is switch, which can be a perfect substitute for each other, and you need to be aware that else if it is simplified to elif in Python. As shown below:

#!/usr/bin/env python user_cmd = raw_input ("please input your choice:\n") if usercmd = = "selec
    t" 
OPS = "select action"
    elif Usercmd = = "update" 
OPS = "update action"
    elif Usercmd = = "delete" 
OPS = "delete action"
    elif Usercmd = = "insert" 
OPS = "insert action"
else Ops = &quot;invalid choice!&quot; Print Ops ' </pre> # 2. Using the dictionary here we use the dictionary function: Dict.get (key, Default=none). key--the value to find in the dictionary, default--returns the default value if the value of the specified key does not exist. As follows: <pre> ' #!/usr/bin/env python usercmd = raw_input (&quot;please input your choice:\n&quot;) dic = {' Sele CT ': ' Select Action ', ' Update ': ' Update action ', ' delete ': ' Delete action ', ' Insert ': ' Insert action '} Defaultitem = ' Invalid choice! ' Ops = Dic.get (usercmd,defaultitem) print Ops ' </pre> # 3. The general form of combining a lambda function with a dictionary lambda is the keyword lambda followed by the One or more arguments, followed by a colon, and then an expression. Lambda isAn expression instead of a statement. It can appear where the Python syntax does not allow DEF to appear, and this is no longer described here. As follows: <pre> ' #!/usr/bin/env python usrcmd = raw_input (&quot;please input your choice:\n&quot;) dic = {' Selec T ': Lambda: &quot;select action&quot;, ' Update ': lambda: &quot;update action&quot;, ' delete ': Lamb
DA: &quot;delete action&quot;, ' Insert ': lambda: &quot;insert action&quot;
 Print Cho[usr_cmd] ()
Conclusion

Six months before the rough study of Python, and then has not how to use, now to review, feel a lot of knowledge to forget, to continue reviewing ...



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.