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

相关推荐
子兮曰2 天前
jev-ultrafast 深度解析:7 秒订机票的浏览器 Agent 是如何炼成的
前端·后端·agent
小羊没烦恼!2 天前
微服务化的基石——持续集成
java·大数据·word·powerpoint·.net
子兮曰2 天前
Jev 爆发一周:7 秒 Agent 背后的 System One 生态与三场争议
前端·后端·ai编程
默_笙2 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
俊昭喜喜里2 天前
java中的继承和多态的区别
java
小羊没烦恼!2 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
前端小万2 天前
写公众号赚了 3000 块后,我做了一款叫 "一键成稿" 的软件
前端·微信小程序
爱勇宝2 天前
ZCode 开源 24 小时:一份没有历史的账本,回答不了"有没有偷代码"
前端·后端·chatglm (智谱)
qq_426003962 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
譕痕2 天前
JSONObject与JSONArray封装数据格式区别
java·json