python 判断与循环

条件判断

Python 实现条件判断的语句叫做 if 语句

• 真、假两个条件借⽤布尔类型:True、False

if 语句语法

if True :

print("条件为真,执⾏该条语句")

* True、False 是布尔值,T 和 F 要⼤写

* 冒号、缩进也是语法部分

python 复制代码
number = 10
if number % 2 == 1 :
 print("number是奇数")
elif number %2 == 0 :
 print("number是偶数")
else :
 print(f"number 的值是 {number}")

match 语句的语法

python 复制代码
match http_response_status:
 case 400:
  print("Bad request" )
 case 404:
  print( "Not found")
 case 418:
  print( "I'm a teapot")
 case _:
  print( "Something's wrong with the internet")

• match 语句需 Python 3.10 以上版本⽀持

• 变量名 _ 为通配符,确保 match 必定会匹配成功

match 的模式组合

python 复制代码
match http_response_status:
 case 400 | 403 | 404:
  print("4xx error" )
 case 500 | 501 | 503:
  print( "5xx error")

可以使⽤"|" 在⼀个模式中组合多个字⾯值

循环

while 循环语法

python 复制代码
number = 1
while number <= 3:
 print(f"number is {number}")
 number += 1

for 循环

使⽤ for 循环遍历字典

1

相关推荐
二十雨辰6 小时前
[爬虫]-Urllib
爬虫·python
小林ixn6 小时前
从零到一理解 React 父子组件通信:手写一个 Todo 应用带你彻底搞懂单向数据流
前端·javascript·react.js
萧瑟余晖7 小时前
JDK 26 新特性详解
java·开发语言
马优晨7 小时前
Freemarker 完整讲解(后端 Java 模板引擎)
java·开发语言·freemarker·freemarker 完整讲解·freemarker模板引擎
醇氧8 小时前
CountDownLatch / CyclicBarrier / Semaphore 面试高频问答清单
前端·面试·职场和发展
玉鸯8 小时前
Agent Hook:在概率推理之上,为 Agent 叠加确定性控制
python·langchain·agent
weixin_446260859 小时前
HACO:面向动态部署环境的对冲式智能计算可靠多智能体调度框架
后端·python·flask
我的xiaodoujiao9 小时前
API 接口自动化测试详细图文教程学习系列32--Allure测试报告2
python·学习·测试工具·pytest
qetfw9 小时前
MXU:Tauri 2 + React 的 MaaFramework 跨平台 GUI 源码
前端·python·react.js·前端框架·开源项目·效率工具