当前位置:网站首页>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
边栏推荐
猜你喜欢
通过程序发送 Gmail 邮件
实用小技能:一键获取Harbor中镜像信息,快捷查询镜像
Redis 回击 Dragonfly:13 年后,Redis 的架构依然是同类最佳
快速解决MySQL插入中文数据时报错或乱码问题
Super detailed MySQL basic operations
Ontology Development Diary 05-Strive to Understand SWRL (Part 2)
Quick sort eight sorts (3) 】 【 (dynamic figure deduction Hoare, digging holes, front and rear pointer method)
【ASM】字节码操作 MethodVisitor 案例实战 生成对象
Attentional Feature Fusion
[ASM] Bytecode operation MethodVisitor case combat generation object
随机推荐
7.Collections工具类
循环嵌套以及列表的基本操作
写一个通讯录小程序
在anaconda环境中配置cuda和cudnn
常用的一些制表符号
安装torch_sparse失败解决方法
日期操作比较全面得代码
cannot import name ‘load_offloaded_weights‘ from ‘accelerate.utils‘ (/home/huhao/anaconda3/envs/huha
Sweet alert
Practical skills: a key for image information in the Harbor, quick query image
文件操作
4. Character stream
OSCS开源软件安全周报,一分钟了解本周开源软件安全大事
Golang Protobuf 处理
ORA-00600 [16703], [1403], [20]问题分析及恢复
Ontology development diary 02 - simple sparql query
极域Killer 1.0代码
socket实现TCP/IP通信
MySQ事务控制语言-TCL,进来学习!
7.FileFilter interface