当前位置:网站首页>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
边栏推荐
- 3D打印了这个杜邦线理线神器,从此桌面再也不乱了
- Super detailed MySQL basic operations
- 实用小技能:一键获取Harbor中镜像信息,快捷查询镜像
- MySQ事务控制语言-TCL,进来学习!
- 五个不同事物隔离级别,七个事物传播行为
- 图表示学习(Graph Representation Learning)笔记
- LeetCode75:颜色分类-C语言一次遍历求解
- What is the reason for the suspended animation of the migration tool in the GBase database?
- Apache Log4j 2 远程代码执行漏洞详解
- Tom Morgan | Twenty-One Rules of Life
猜你喜欢
随机推荐
Redis 回击 Dragonfly:13 年后,Redis 的架构依然是同类最佳
BigDecimal用法常用操作记录
try catch 对性能影响
Celebrate ranked 18
Ontology Development Diary 03 - When debugging is in progress
8. Recursively traverse and delete cases
5.Set接口与实现类
STM32F103实现IAP在线升级应用程序
4. Character stream
Technology Sharing | How to simulate real usage scenarios?mock technology to help you
快速解决MySQL插入中文数据时报错或乱码问题
3.List interface and implementation class
文件操作
【八大排序②】选择排序(选择排序,堆排序)
pycharm在创建py文件时如何自动注释
1.流的概念
EndNote User Guide
Sweet alert
vgg网络结构
如何快速打通镜像发布流程?