GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.runoob.com — Python match…case 语句 | 菜鸟教程
Python 3 教程
Python3 教程
Python3 简介
Python3 环境搭建
Python3 Pycharm
Python3 VScode
Python3 基础语法
Python3 基本数据类型
Python3 数据类型转换
Python3 解释器
Python3 注释
Python3 运算符
Python3 数字(Number)
Python3 字符串
Python3 列表
Python3 元组
Python3 字典
Python3 集合
Python3 条件控制
Python3 循环语句
Python3 编程第一步
Python3 推导式
Python3 迭代器与生成器
Python3 with
Python3 函数
Python3 lambda
Python3 装饰器
Python3 数据结构
Python3 模块
Python __name__
Python3 输入和输出
Python3 File
Python3 OS
Python3 错误和异常
Python3 面向对象
Python3 命名空间/作用域
Python 虚拟环境的创建
Python 类型注解
Python3 标准库概览
Python3 实例
Python 测验
Python3 高级教程Python3 正则表达式 Python3 CGI编程 Python3 MySQL(mysql-connector) Python3 MySQL(PyMySQL) Python3 网络编程 Python3 SMTP发送邮件 Python3 多线程 Python3 XML 解析 Python3 JSON Python3 日期和时间 Python3 内置函数 Python3 MongoDB Python3 urllib Python uWSGI 安装配置 Python3 pip Python3 operator Python math Python requests Python random Python OpenAI Python 有用的资源 Python AI 绘画 Python statistics Python hashlib Python 量化 Python pyecharts Python selenium 库 Python 爬虫 Python Scrapy 库 Python Markdown Python sys 模块 Python Pickle 模块 Python subprocess 模块 Python queue 模块 Python StringIO 模块 Python logging 模块 Python datetime 模块 Python re 模块 Python csv 模块 Python threading 模块 Python asyncio 模块 Python PyQtPython match...case 语句match...case 提供了一种更强大的模式匹配方法。 模式匹配是一种在编程中处理数据结构的方式,可以使代码更简洁、易读。 match...case 是 Python 3.10 版本引入的新语法。 match...case 语法结构如下: match expression:
case pattern1:
# 处理pattern1的逻辑
case pattern2 if condition:
# 处理pattern2并且满足condition的逻辑
case _:
# 处理其他情况的逻辑
参数说明:
实例1. 简单的值匹配 实例
def match_example(value): match value: case 1: print("匹配到值为1") case 2: print("匹配到值为2") case _: print("匹配到其他值") match_example(1) # 输出: 匹配到值为1 match_example(2) # 输出: 匹配到值为2 match_example(3) # 输出: 匹配到其他值 以上代码中,match 语句用于匹配 value 的不同情况,每个 case 语句表示一种可能的匹配情况,_ 通配符表示其他情况。 输出结果为: 匹配到值为1 匹配到值为2 匹配到其他值 2. 使用变量 实例
def match_example(item): match item: case (x, y) if x == y: print(f"匹配到相等的元组: {item}") case (x, y): print(f"匹配到元组: {item}") case _: print("匹配到其他情况") match_example((1, 1)) # 输出: 匹配到相等的元组: (1, 1) match_example((1, 2)) # 输出: 匹配到元组: (1, 2) match_example("other") # 输出: 匹配到其他情况 输出结果为: 匹配到相等的元组: (1, 1) 匹配到元组: (1, 2) 匹配到其他情况 3. 类型匹配 实例
class Circle: def __init__(self, radius): self.radius = radius class Rectangle: def __init__(self, width, height): self.width = width self.height = height def match_shape(shape): match shape: case Circle(radius=1): print("匹配到半径为1的圆") case Rectangle(width=1, height=2): print("匹配到宽度为1,高度为2的矩形") case _: print("匹配到其他形状") match_shape(Circle(radius=1)) # 输出: 匹配到半径为1的圆 match_shape(Rectangle(width=1, height=2)) # 输出: 匹配到宽度为1,高度为2的矩形 match_shape("other") # 输出: 匹配到其他形状 输出结果为: 匹配到半径为1的圆 匹配到宽度为1,高度为2的矩形 匹配到其他形状
AI 思考中...
点我分享笔记取消
分类导航
Advertisement
|
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |