在嵌套 if 语句中,可以把 if...elif...else 结构放在另外一个 if...elif...else 结构中。
if 表达式1:
语句
if 表达式2:
语句
elif 表达式3:
语句
else:
语句
elif 表达式4:
语句
else:
语句
4. match…case
Python 3.10 增加了 match...case 的条件判断,不需要再使用一连串的 if-else 来判断了。
match 后的对象会依次与 case 后的内容进行匹配,如果匹配成功,则执行匹配到的表达式,否则直接跳过,_ 可以匹配一切。
语法格式如下:
match subject:
case <pattern_1>:
<action_1>
case <pattern_2>:
<action_2>
case <pattern_3>:
<action_3>
case _:
<action_wildcard>
case _: 类似于 C 和 Java 中的 default:,当其他 case 都无法匹配时,匹配这条,保证永远会匹配成功。
一个 case 也可以设置多个匹配条件,条件使用 | 隔开,例如:
case 401|403|404:
return "Not allowed"
#!/usr/bin/env python3# -*- coding: UTF-8 -*-# @Date : 2024/3/27 15:49# @Author : water# @Description : 条件循环语句"""
Python3 条件控制
Python 条件语句是通过一条或多条语句的执行结果(True 或者 False)来决定执行的代码块。
if 语句
Python中if语句的一般形式如下所示:
if condition_1:
statement_block_1
elif condition_2:
statement_block_2
else:
statement_block_3
如果 "condition_1" 为 True 将执行 "statement_block_1" 块语句
如果 "condition_1" 为False,将判断 "condition_2"
如果"condition_2" 为 True 将执行 "statement_block_2" 块语句
如果 "condition_2" 为False,将执行"statement_block_3"块语句
Python 中用 elif 代替了 else if,所以if语句的关键字为:if – elif – else。
注意:
1、每个条件后面要使用冒号 :,表示接下来是满足条件后要执行的语句块。
2、使用缩进来划分语句块,相同缩进数的语句在一起组成一个语句块。
3、在 Python 中没有 switch...case 语句,但在 Python3.10 版本添加了 match...case,功能也类似。
if 嵌套
在嵌套 if 语句中,可以把 if...elif...else 结构放在另外一个 if...elif...else 结构中。
if 表达式1:
语句
if 表达式2:
语句
elif 表达式3:
语句
else:
语句
elif 表达式4:
语句
else:
语句
match...case
Python 3.10 增加了 match...case 的条件判断,不需要再使用一连串的 if-else 来判断了。
match 后的对象会依次与 case 后的内容进行匹配,如果匹配成功,则执行匹配到的表达式,否则直接跳过,_ 可以匹配一切。
语法格式如下:
match subject:
case <pattern_1>:
<action_1>
case <pattern_2>:
<action_2>
case <pattern_3>:
<action_3>
case _:
<action_wildcard>
case _: 类似于 C 和 Java 中的 default:,当其他 case 都无法匹配时,匹配这条,保证永远会匹配成功。
一个 case 也可以设置多个匹配条件,条件使用 | 隔开,例如:
case 401|403|404:
return "Not allowed"
"""
flag =Trueif flag:print("flag is True")else:print("flag is False")
age =int(input("请输入你家狗狗的年龄: "))print("")if age <=0:print("你是在逗我吧!")elif age ==1:print("相当于 14 岁的人。")elif age ==2:print("相当于 22 岁的人。")elif age >2:
human =22+(age -2)*5print("对应人类年龄: ", human)# 退出提示input("点击 enter 键退出")# 无限循环# 该实例演示了数字猜谜游戏
number =7
guess =-1print("数字猜谜游戏!")while guess != number:
guess =int(input("请输入你猜的数字:"))if guess == number:print("恭喜,你猜对了!")elif guess < number:print("猜的数字小了...")elif guess > number:print("猜的数字大了...")# if嵌套
num =int(input("输入一个数字:"))if num %2==0:if num %3==0:print("你输入的数字可以整除 2 和 3")else:print("你输入的数字可以整除 2,但不能整除 3")else:if num %3==0:print("你输入的数字可以整除 3,但不能整除 2")else:print("你输入的数字不能整除 2 和 3")# match...casedefhttp_error(status):match status:case400:return"Bad request"case404:return"Not found"case418:return"I'm a teapot"case401|403|404:return"Not allowed"case_:return"Something's wrong with the internet"if __name__ =="__main__":
my_status =403print(http_error(my_status))
代码概述(循环语句)
#!/usr/bin/env python3# -*- coding: UTF-8 -*-# @Date : 2024/3/27 15:49# @Author : water# @Description : 循环语句"""
Python3 循环语句
Python 中的循环语句有 for 和 while。
while 循环
Python 中 while 语句的一般形式:
while 判断条件(condition):
执行语句(statements)……
注意:同样需要注意冒号和缩进。另外,在 Python 中没有 do..while 循环。
无限循环
我们可以通过设置条件表达式永远不为 false 来实现无限循环,
你可以使用 CTRL+C 来退出当前的无限循环。
无限循环在服务器上客户端的实时请求非常有用。
while 循环使用 else 语句
如果 while 后面的条件语句为 false 时,则执行 else 的语句块。
语法格式如下:
while <expr>:
<statement(s)>
else:
<additional_statement(s)>
expr 条件语句为 true 则执行 statement(s) 语句块,如果为 false,则执行 additional_statement(s)。
循环输出数字,并判断大小:
简单语句组
类似 if 语句的语法,如果你的 while 循环体中只有一条语句,你可以将该语句与 while 写在同一行中
for 语句
Python for 循环可以遍历任何可迭代对象,如一个列表或者一个字符串。
for循环的一般格式如下:
for <variable> in <sequence>:
<statements>
else:
<statements>
for...else
在 Python 中,for...else 语句用于在循环结束后执行一段代码。
语法格式如下:
for item in iterable:
# 循环主体
else:
# 循环结束后执行的代码
当循环执行完毕(即遍历完 iterable 中的所有元素)后,会执行 else 子句中的代码,如果在循环过程中遇到了 break 语句,则会中断循环,此时不会执行 else 子句。
break 和 continue 语句及循环中的 else 子句
break 语句可以跳出 for 和 while 的循环体。如果你从 for 或 while 循环中终止,任何对应的循环 else 块将不执行。
continue 语句被用来告诉 Python 跳过当前循环块中的剩余语句,然后继续进行下一轮循环。
注意:循环语句可以有 else 子句,它在穷尽列表(以for循环)或条件变为 false (以while循环)导致循环终止时被执行,但循环被 break 终止时不执行。
pass 语句
Python pass是空语句,是为了保持程序结构的完整性。
pass 不做任何事情,一般用做占位语句
"""# 循环语句: 求和
m =100
n =0
sm =0while n <= m:
sm += n
n +=1print("1 到 %d 之间的整数的和为:%d"%(m, sm))# 无限循环whileTrue:
s =input("请输入:")if s =="q":breakprint("你输入的内容是:", s)# while 循环使用 else 语句whileTrue:print("请输入用户名和密码:")
name =input("UserName:")
password =input("Password:")if name =="admin"and password =="123456":print("登录成功!")breakelse:print("用户名或密码错误!")# 简单语句组
flag =1# while flag: print('欢迎访问菜鸟教程!')print("Good bye!")# for 循环# 整数范围值可以配合 range() 函数使用:for i inrange(5):print(i)# 列表循环
sites =["Baidu","Google","Runoob","Taobao"]for site in sites:print(site)# 字符串循环for i in"Runoob":print(i)# for...elsefor i inrange(5):print(i)else:print("i is no longer less than 5")# 以下 for 实例中使用了 break 语句,break 语句用于跳出当前循环体,不会执行 else 子句for i inrange(5):if i ==3:breakprint(i)else:print("i is no longer less than 5")# range() 指定区间的值:for i inrange(5,30):if i ==15:breakprint(i)# range() 以指定数字开始并指定不同的增量(甚至可以是负数,有时这也叫做'步长'):for i inrange(5,30,3):print(i)# 步长是负数时,range() 函数的用法如下:for i inrange(30,5,-3):print(i)# 结合 range() 和 len() 函数以遍历一个序列的索引for i inrange(len(sites)):print(i, sites[i])# break 和 continue 语句及循环中的 else 子句for i inrange(10):if i ==5:breakprint(i)else:print("i is no longer less than 5")# continuefor i inrange(10):if i ==5:continueprint(i)# pass 语句for i inrange(10):pass