Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What does the Switch Case syntax look like in Python

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is to share with you what the Switch Case grammar in Python is like. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

A syntax feature has been added to Python3.10.

It is the switch-case sentence that has been hotly debated recently.

When I first came into contact with Python, I wondered that Python didn't have a switch statement.

Later, I figured it out, because Python's philosophy advocates simplicity and practicality, and one or two can be seen from the Zen of Python.

There should be one and preferably only one, obvious solution to any problem.

Without switch... How do we solve this problem when using case statements?

Use if... Elif...

For example:

If code = 200: return "ok" elif code = 404: return "no found" elif code = 500 return "error" else: return "unknow"

This code looks intuitive, but the only thing that is a little verbose is that every line has an expression judgment statement for code==xxx

Therefore, in some Python best practices, some scenarios are recommended to be implemented in dictionaries, such as when different results are returned based on the value of a variable.

Data = {200: "ok", 404: "no found", 500: "error",} value = data.get (code, "unkonw")

A good solution to the tedious problem of the code.

Let's take a look at how switch case in Python3.10 works.

Http_code = "418" match http_code: case "200": print ("OK") do_something_good () case "404": print ("Not Found") do_something_bad () case "418": print ("I'm a teapot") make_coffee () case _: print ("Code not found")

Similar to switch case syntax in other languages, the match keyword is added, and the "break" keyword is less than Java.

It has been 30 years since Python was released in 1991.

To tell you the truth, the lack of switch case does not affect the efficiency of development at all, and the introduction of such a syntax candy on March 10 can only be regarded as an innocuous function.

Really used in the production environment, but also five or six years later, unless there is a major release, who has nothing to do with you to upgrade the version every year.

You know, there are still a lot of old systems running Python2.x.

Personally, I have nothing to be excited about the improvement in the sugar level of this grammar! If you want to ask me if I support it, I will certainly support it, so that I can confidently tell others that Python does not support swtich case.....

This is what the Switch Case grammar in Python is like. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 209

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report