当前位置:网站首页>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
边栏推荐
- 批量修改Shapefile属性表的一种方法(使用gdal.jar)
- try catch 对性能影响
- LeetCode(剑指 Offer)- 25. 合并两个排序的链表
- 1. The concept of flow
- Openwrt配置Aria2(Hg255d)
- 技术分享 | 如何模拟真实使用场景?mock 技术来帮你
- A little experience sharing about passing the CISSP exam at one time
- A Practical Guide to Building OWL Ontologies using Protege4 and CO-ODE Tools - Version 1.3 (7.4 Annotation Properties - Annotation Properties)
- 极域Killer 1.0代码
- MySQL常用存储引擎,你不可错过的知识点!
猜你喜欢
随机推荐
2. Thread creation
【八大排序③】快速排序(动图演绎Hoare法、挖坑法、前后指针法)
Redis 回击 Dragonfly:13 年后,Redis 的架构依然是同类最佳
LeetCode56:合并区间 C语言解法,注解详细 一看就懂!
ArrayList和LinkedList
Tom Morgan | Twenty-One Rules of Life
OSCS开源软件安全周报,一分钟了解本周开源软件安全大事
EndNoteX9 OR X 20 Guide
Ontology Development Diary 03-Understanding Code
梦笔记0809
Redis cache update strategy actively
关于一次性通过CISSP考试的一点经验分享
Ontology development diary 04 - to try to understand some aspects of protege
LeetCode75:颜色分类-C语言一次遍历求解
m个样本的梯度下降
[ASM] Bytecode operation MethodVisitor case combat generation object
抛出一个问题? Mysql环境下进行Count操作执行的时候速度很慢_需手动给主键添加索引---MySql优化001
1. The concept of flow
[Machine Learning] Detailed explanation of web crawler combat
【size_t是无符号整数 (-1 > 10) -> 1】