当前位置:网站首页>conditional control statement
conditional control statement
2022-08-09 09:59:00 【Wu Xizhong】
条件控制语句
一、.A non-boolean AND-OR budget
二、条件运算符(三目运算符)
三、input 函数
四、运算符的优先级
五、条件控制语句
1.if语句
2.if-else语句
3.if-elif-else语句
六、while循环语句
一、非布尔值的与或运算
1.与运算
1.Non-boolean values participate in AND operations,First, it will be converted into a boolean value to participate in the operation,And the result of the calculation returns itself
2.AND operation to findFalse,If the first value is FalseThen the direct calculation result directly returns the first value,Otherwise the result of the calculation returns the second value.
例子1:
0 and 3
输出结果为
0
例子2:
11 and 13
输出结果为
13
2.或运算
1.Non-boolean values participate in the OR operation,First, it will be converted into a boolean value to participate in the operation,And the result of the calculation returns itself
2.或运算找True,If the first value is TrueThen the direct calculation result directly returns the first value,Otherwise the result of the calculation returns the second value.
例子1:
3 or 5
输出结果
3
例子2
0 or 9
输出结果
9
二.条件运算符(三目运算符)
条件运算符的格式:
语句1 if 条件表达式 else 语句2
举例1:
print(i**2) if i <10 else print(i)
举例2:This conditional control operator nests another conditional control operator
print(i) if i <10 else print(i+1) if i<50 else print(i*2)
三.input 函数
1.input ()Functions have blocking capabilities in the program,程序执行遇到inputThe function will temporarily stop running until the user presses itEnterThe program continues to execute only when the Enter key is pressed.
2.input() The function waits for user input at the terminal,When the user enters something and pressesEnterThe program will continue to execute.
3.无论用户输入什么内容,input()All functions return a string type.
举例:
input('请输入:')
终端将‘’出现请输入:‘’and wait for user input
请输入:
四.运算符的优先级
Operator precedence table view link:https://docs.python.org/3/reference/expressions.html#operator-precedence
The further down the priority table in the link, the higher the priority
五.条件控制语句
一.if语句
1.if 语句执行流程:
if 语句在执行时,会先对条件表达式进行求值判断,如果为True则执行if 后面的语句,如果为False则不执行.
2.if 语句的语法:
if 条件表达式 :
代码块
Code Blocks Code blocks hold a set of code,同一个代码块中的代码,要么都执行要么都不执行
代码块以缩进开始,直到代码恢复到之前的缩进级别时结束
Code blocks are a mechanism for grouping code
if True:
代码块
二.if-else语句
1.if-else语句执行流程:
if-else语句在执行时,先对if后的条件表达式进行求值判断
如果为True,则执if后的代码块
如果为False,则执行else后的代码块
2.if-else语句的语法
if 条件表达式 :
代码块
else :
代码块
if True:
代码块
else:
代码块
三.if-elif-else语句
1.if-elif-else语句的执行流程:
if-elif-else语句在执⾏时,Conditional expressions are evaluated in order from top to bottom
断,
如果表达式的结果为True,则执行当前代码块,然后语句结束
如果表达式的结果为False,则继续向下判断,直到找到True为止
如果所有的表达式都是False,则执行else后的代码块
2.if-elif-else 语句的语法:
if 条件表达式 :
代码块
elif 条件表达式 :
代码块
elif 条件表达式 :
代码块
············
else:
代码块
if True:
代码块
elif 条件表达式:
代码块
elif 条件表达式:
代码块
···········
else:
代码块
总结: if-elif-else中只会有一个代码块会执行
六.while语句
循环语句可以使指定的代码块重复指定的次数.循环语句分成两种,while循环和 for循环
while语句的语法:
while 条件表达式 :
代码块
else:
代码块
while 条件表达式:
代码块
else:
代码块
注意:当whileExecution continues when the loop ends normallyelse里面的代码块.
七.whileThe three elements of a sentence
1.初始化表达式
2.条件表达式
3.Update conditional expressions,That is, modify the value of the initializer expression
边栏推荐
猜你喜欢
随机推荐
《刷题日记》2
Tom Morgan | Twenty-One Rules of Life
Arrays类、冒泡排序、选择排序、插入排序、稀疏数组!
LeetCode(剑指 Offer)- 25. 合并两个排序的链表
Golang Protobuf 处理
LPP code and its comments
goproxy.io 证书过期
安装torch_sparse失败解决方法
3. Coding method
拿下跨界C1轮投资,本土Tier 1高阶智能驾驶系统迅速“出圈”
OSCS开源软件安全周报,一分钟了解本周开源软件安全大事
8. Recursively traverse and delete cases
诡秘番外:在现代
浏览器的报错分类
免费下载天地图全国基础地理信息矢量数据的一种方法
LeetCode75:颜色分类-C语言一次遍历求解
MySQL全文索引
收到人生第一笔五位数工资
7.Collections tool class
nacos从下载到安装集群的




![[ASM] Bytecode operation MethodVisitor case combat generation object](/img/a9/df07614f875794d55d530bd04dc476.jpg)




