今日分享(四十)——python学习(7)
- 编程
- 2023-02-25
分享兴趣,传播快乐,增长见闻,留下美好!
亲爱的您,这里是LearningYard学苑。
今天小编为大家带来今日分享(四十),欢迎您的访问!
Share interests, spread happiness, increase knowledge,
and leave a good legacy!
Dear you, this is The LearningYard Academy.
Today Xiaobian brings you todays sharing (forty ), welcome your visit!
程序的控制结构(1)----分支结构
1、单分支结构:if语句
语句块是让条件满足后执行的一个或多个语句序列,语句块中语句通过if所在行形成缩进表达包含关系。if语句首先评估条件的结果值,如果结果为 True, 则执行语句块中的语句序列,然后控制转向程序的下一条语句。如果结果为 False, 语句块中的语句会被跳过。A statement block is a sequence of one or more statements executed after the conditions are met, and the statements in the statement block express the containment relationship by indenting the line where the if is located. The if statement first evaluates the result value of the condition, and if the result is true, executes the sequence of statements in the statement block, and then controls the next statement that moves to the program. If the result is False, the statement in the statement block is skipped.2、二分支结构:if-else语句
语句块1是if条件满足后执行的一个或多个语句序列,语句块2if条件不满足后执行的语句序列。二分支语句用于区分条件的两种可能,即True 或者 False,分别形成执行路径。Statement block 1 is the sequence of one or more statements executed after the if condition is met, and the sequence of statements executed after the if condition of statement block 2 is not met. The two-branch statement is used to distinguish between two possibilities of the condition, namely True or False, and form an execution path, respectively.紧凑形式:适用于简单表达式的二分支结构Compact form: A two-branch structure suitable for simple expressions<表达式1>if<条件>else<表达式2>
举个例子:
注:这种情况只支持表达式,即语句中的一部分,不支持带等号的形式,并没有赋值的过程。Note: This case only supports expressions, that is, parts of the statement, does not support forms with equal signs, and there is no process for assigning values.3、多分支结构:if-elif-else语句多分支结构是二分支结构的扩展,这种形式通常用于设置同一个判断条件的多条执行路径。Python 依次评估寻找第一个结果为 True 的条件,执行该条件下的语句块,结束后跳过整个 if-elif-else 结构,执行后面的语句。如果没有任何条件成立,else下面的语句块将被执行。else 子句是可选的。The multi-branch structure is an extension of the two-branch structure, which is usually used to set multiple execution paths for the same judgment condition. Python in turn evaluates the condition that looks for the first result to be true, executes the statement block under that condition, and skips the entire if-elif-else structure after the end and executes the subsequent statements. If none of the conditions are true, the following statement block else will be executed. The else clause is optional.
错误示范:
如果输入80,则会执行第一条语句,即输出结果为D,显然是不符合逻辑的,因此我们在设计程序的时候应该注意多条件之间的包含关系以及变量取值范围的覆盖。If you enter 80, the first statement will be executed, that is, the output result is D, which is obviously illogical, so we should pay attention to the inclusion relationship between multiple conditions and the coverage of variable value ranges when designing programs.今天的分享就到这里了。
如果您对今天的文章有什么独特的想法,
欢迎评论留言,让我们相约明天,
祝您今天过得开心快乐!
Thats it for todays sharing.
If you have any unique ideas for todays article, please leave a comment, let us meet tomorrow,
I wish you a happy day!
参考资料:《python程序设计》
翻译:谷歌翻译
本文由learningyard新学苑原创,部分文字图片来源于他处,如有侵权,请联系删除