Posted in

什么是 Python 的 Match Case 语句以及为什么使用它?_AI阅读总结 — 包阅AI

包阅导读总结

1. 关键词:Python 3.10、Match Case 语句、程序流程控制、代码可读性、条件匹配

2. 总结:本文介绍了 Python 3.10 新增的 Match Case 语句,它能更好地控制程序流程。解释了其工作原理、语法和使用场景,如根据不同输入进行输出,还可结合 OR 操作符和 if 条件,使代码更易读和调试。

3. 主要内容:

– Python 3.10 引入重要新特性 Match Case 语句

– 能更精准控制程序流程

– 需使用 Python 3.10 及以上版本

– 工作原理

– 定义变量,与不同情况进行比较,匹配则执行相应代码

– 语法

– `match parameter case pattern1: code to execute for pattern1 case pattern2: code to execute for pattern2 … case pattern10 code to execute for pattern10 case _: default code to execute`

– 使用场景

– 示例:根据不同数字或输入执行不同输出

– 可使用 OR 操作符

– 结合 if 条件判断正负零

思维导图:

文章地址:https://thenewstack.io/what-is-the-python-match-case-statement-and-why-use-it/

文章来源:thenewstack.io

作者:Jack Wallen

发布时间:2024/8/8 20:32

语言:英文

总字数:1212字

预计阅读时间:5分钟

评分:84分

标签:Python,编程,Match Case 语句,代码控制流,Python 3.10


以下为原文内容

本内容来源于用户推荐转载,旨在分享知识与观点,如有侵权请联系删除 联系邮箱 media@ilingban.com

When Python 3.10 was released, it included a very important new feature called the match case statement. What this does is give you even more control over the flow of your programs. With match case statements, you control what parts of code are executed if/when conditions (aka cases) are met.

Before we continue, understand that, because the match case statement was introduced in Python 3.10, you need to be running that version or newer to use this feature.

Back to the statement at hand.

Here’s how it works.

Let’s say you define x as 5. You could then create match case statements that would print out different things based on the number. For example, you could have the following match case statements:

  • If X is 5, print X equals 5
  • If X is 10, print X does not equal 5
  • Otherwise, print I have no idea what X is

Of course, the above is not the syntax of a match case statement, but we’ll get to that.

Essentially, what you do is define X (whatever X is) and then compare different cases against the variable. If a case matches, the program executes whatever is associated with the match (in this case it would print X equals 5).

This sounds similar to if-elif-else statements, so why would you opt to go the match case route? Simply put, it makes it easier to simplify flow control. By doing this your code is not just easier to read but easier to debug. Any time you can achieve those two things it’s a win.

But how do you actually use match case statements?

The syntax of the match case statement looks like this:

match parameter

case pattern1:

code to execute for pattern1

case pattern2:

code to execute for pattern2

case pattern10

code to execute for pattern10

case _:

default code to execute

Let’s start with our X = 5 example. That code would look like this:

x = ‘5’

match x:

case ‘5’:

print(‘X equals 5’)

case ’10’:

print(‘X does not equal 5’)

case other:

print(‘I have no idea what X is’)

If you were to run the above code, the output would be:

X equals 5

We can do the same thing with a Hello, World! app like this:

command = ‘Hello, New Stack!’

match command:

case ‘Hello, New Stack!’:

print(‘Hello, Jack’)

case ‘Hello, World!’:

print(‘Hello to everyone!’)

case other:

print(‘Goodbye’)

The output of the above command would be: Hello, Jack.

Let’s make this a bit more useful. Instead of defining X as a hard-coded variable, let’s accept user input. We’ll do this by first defining a function like this:

Next, we have our input statement that asks the user to enter a number between 1 and 5. That line looks like this:

num = int(input(“Enter a number between 1 and 5: “))

We now create our match case statement which looks like this:

match num:

case 1:

print(‘You typed 1’)

case 2:

print(‘You typed 2’)

case 3:

print(‘You typed 3’)

case 4:

print(‘You typed 4’)

case 5:

print(‘You typed 5’)

case _:

print(‘Your number is not between 1 and 5’)

We finally execute our function with:

The entire code looks like this:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

def numMatch():

num = int(input(“Enter a number between 1 and 5: “))

match num:

case 1:

print(‘You typed 1’)

case 2:

print(‘You typed 2’)

case 3:

print(‘You typed 3’)

case 4:

print(‘You typed 4’)

case 5:

print(‘You typed 5’)

case _:

print(‘Your number is not between 1 and 5’)

numMach()

When you run the above, you’ll be asked to type a number between 1 and 5 and the app will print out the number you typed. If you type any number outside of those parameters, the app will print out: Your number is not between 1 and 5.

You can also use OR operators with match case statements. For the case statement, you would use something like:

This means if the case is either 1 or 2, print what follows. Change the entire block of code from above to use the OR operator and it looks like this:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

def numMatch():

num = int(input(“Enter a number between 1 and 10: “))

match num:

case 1 | 2:

print(‘You typed 1 or 2’)

case 3 | 4:

print(‘You typed 3 or 4’)

case 5 | 6:

print(‘You typed 5 or 6’)

case 7 | 8:

print(‘You typed 7 or 8’)

case 9 | 10:

print(‘You typed 9 or 10’)

case _:

print(‘Your number is not between 1 and 10’)

numMatch()

Another way to use match case statements is with an if condition. Let’s allow our user to enter any number (negative or positive). The match case statement will then match the inputted number with a negative, zero, or positive statement. That code looks like this:

Run the above and if the user inputs a negative number, it’ll print Your number is negative. If your user inputs a positive number, it’ll print: Your number is positive. Otherwise, it’ll print: Your number is zero.

And that’s the gist of using the Python match case statement.

YOUTUBE.COM/THENEWSTACK

Tech moves fast, don’t miss an episode. Subscribe to our YouTubechannel to stream all our podcasts, interviews, demos, and more.

GroupCreated with Sketch.