学习Python3 的基础语法后,学习了些条件控制语句。发现这种语句是在编程开发中是常用的一种计算机判断语句,浅理解是对程序执行的流程就行控制,也被称为“流程控制语句”。下面对学习过程进行一些内容记录。
一般格式:
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。
示例:
var1 = 100
if var1:
print ("1 - if 表达式条件为 true")
print (var1)
var2 = 0
if var2:
print ("2 - if 表达式条件为 true")
print (var2)
print ("Good bye!")
输出结果:
1 - if 表达式条件为 true
100
Good bye!
从结果可以看到由于变量 var2 为 0,所以对应的 if 内的语句没有执行。
多个条件语句示例:
注意:
- 1、每个条件后面要使用冒号 :,表示接下来是满足条件后要执行的语句块。
- 2、使用缩进来划分语句块,相同缩进数的语句在一起组成一个语句块。
- 3、在 Python 中没有 switch...case 语句,但在 Python3.10 版本添加了 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 都无法匹配时,匹配这条,保证永远会匹配成功。
示例:
mystatus=400
print(http_error(400))
def http_error(status):
match status:
case 400:
return "Bad request"
case 404:
return "Not found"
case 418:
return "I'm a teapot"
case _:
return "Something's wrong with the internet"
一个 case 也可以设置多个匹配条件,条件使用 | 隔开,例如:
case 401|403|404:
return "Not allowed"
if中常用的操作运算符:
在if语句中的嵌套
在嵌套 if 语句中,可以把 if...elif...else 结构放在另外一个 if...elif...else 结构中。
示例:
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")