一、概述
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"块语句
注意:
1、每个条件后面要使用冒号(:),表示接下来是满足条件后要执行的语句块。
2、使用缩进来划分语句块,相同缩进数的语句在一起组成一个语句块。
3、在Python中没有switch – case语句。
三、常用运算符
|
操作符
|
描述
|
|
<
|
小于
|
|
<=
|
小于或等于
|
|
>
|
大于
|
|
>=
|
大于或等于
|
|
==
|
等于,比较对象是否相等
|
|
!=
|
不等于
|
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/24851054/viewspace-2148199/
,如需转载,请注明出处,否则将追究法律责任。