posted on 2023-05-07 21:21 read(1144) comment(0) like(8) collect(0)
Computers have three organizational structures when executing code:
If a program is followed step by step from beginning to end without turning points in the middle, it will not be able to complete too much work. Program design often requires a turning point, which is called flow control in programming.
Relational operators are often needed for comparisons in selection constructs and loop constructs.
The relational operators in Python are as follows ⬇️:
relational operator | describe | example |
---|---|---|
< | less than | a < b |
<= | less than or equal to | a <= b |
> | more than the | a > b |
>= | greater than or equal to | a >= b |
== | equal | a += b |
!= | not equal to | a != b |
The result returned by the operation is of boolean type. Returns True for True and False for False.
and
The following is a diagrammatic description of logical operators ⬇️:
Cleverly remember the formula:one false is false
or
The following is a diagrammatic description of logical operators ⬇️:
Cleverly remember the formula:true is true
The following is a textual description of the logical operators not
⬇️:
If the expression is true (True), the result of the not expression is false (False); if the expression is false (False), the result of the not expression is true (True).
The basic syntax of an if statement is as follows ⬇️:
if (conditional judgment):
code block
If the result of the conditional expression is true, the statement is executed, and if it is false, it is skipped.
Case ⬇️
age = 18
if age == 18: # 判断18是否等于变量age中的值
print('您已经成年了。')
If the code block has only one statement it can be simplified:
if (conditional judgment): code block
Case ⬇️
age = 18
if age >= 18: print('您已经成年了。')
A more common situation in programming is to execute a block of code when a condition is judged to be true (True); to execute another block of code when the condition is false (False).
if... else
This is where sentences are needed .
The basic syntax of the if...else statement is as follows ⬇️:
if (conditional judgment):
Code block 1
else:
Code block 2
Case ⬇️
age = 18
if age >= 18: # 判断条件,如果满足执行代码块1不满足执行代码块2
print('您已经成年了。')
else:
print('您还未成年。')
When the program requires multiple conditional judgments, relying solely on if or if...else cannot meet our needs. This is the
if...elif...else
statement we can use.
The basic syntax of the if... elif...else statement is as follows ⬇️:
if (conditional judgment):
Code block 1
elf:
Code block 2
…
else:
code block n
Case ⬇️
score = 93
if score >= 90:
print('您的成绩等级为A')
elif score >= 80:
print('您的成绩等级为B')
elif score >= 60:
print('您的成绩等级为C')
else:
print('您的成绩等级为D')
Python 3.10 adds the conditional judgment
match...case
of . The function is similar to the switch in the Java language.
Python3.10 installation package link: https://pan.baidu.com/s/1S3o10uIyhjmIkVtDc_ae0g?pwd=h1ur
Extraction code: h1ur:
The basic syntax of the match...case statement is as follows ⬇️:
match (conditional judgment):
case value 1:
Code block 1
case value 2:
Code block 2
…
case _:
code block n
_
match any result with a single underscore
Case ⬇️
score = 90
match score//10:
case 10,9:
print('您的成绩等级为A')
case 8:
print('您的成绩等级为B')
case 6,7:
print('您的成绩等级为C')
case _:
print('您的成绩等级为D')
The four steps of the while loop are: initial value, loop condition, loop body, iteration
The basic syntax of the while loop is as follows ⬇️:
while (conditional judgment) :
code block
Case ⬇️
num = 1 # 1.赋初始值
sum = 0
while num <= 100: # 循环条件
sum += num # 循环体
num += 1 # 迭代更新,改变条件变量
print('1~100之间的累加和为:',sum)
When the condition is judged to be true (True), the code block in the loop is executed; when the condition is false (Flase), the code block in the else is executed.
The basic syntax of while...else is as follows ⬇️:
while (conditional judgment) :
Code block 1
else:
Code block 2
Case ⬇️
num = 1
sum = 0
while num <= 100:
sum += num
num += 1
else:
print('1~100之间的累加和为:',sum)
The basic syntax of a for loop is as follows ⬇️:
for (loop variable) in (traversal object):
code block
The traversal object can be: string, file, list, range function, etc.
Case ⬇️
for i in range(1,11,2):
print(i) # 打印1~10之间的奇数
When the condition is judged to be true (True), the code block in the loop is executed; when the condition is false (Flase), the code block in the else is executed.
The basic syntax of for...else is as follows ⬇️:
for loop variable in (traversal object):
Code block 1
else:
Code block 2
Case ⬇️
for i in range(1,11,2):
print(i) # 打印1~10之间的奇数
else:
print('程序结束')
If you want to skip the loop when certain conditions occur, you can use it at this time
continue
, and continue generally needs to be used with if.
The basic syntax of continue is as follows ⬇️:
for (loop variable) in (traversal object):
code block
if (conditional judgment): # If the condition is true, exit this loop
continue
Case ⬇️
for i in range(1,11):
if i % 2 == 0: # 如果i是偶数跳过
continue
print(i) # 打印1~10之间的奇数
If you want to exit the entire loop when certain conditions occur, you can use it at this time
break
, and break also needs to be used in conjunction with if.
The basic syntax of break is as follows ⬇️:
for (loop variable) in (traversal object):
code block
if (conditional judgment): # If the condition is true, exit the loop
break
Case ⬇️
for i in range(1,11,2):
print(i) # 打印1~10之间的奇数
if i == 7: # 当i等于7时提前退出循环
break
Note: In the case of for...else or while...else, use break, the statement after else will not be executed.
Author:kkkkkkkkkkdsdsd
link:http://www.pythonblackhole.com/blog/article/379/39bb23523fb0028fe320/
source:python black hole net
Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.
name:
Comment content: (supports up to 255 characters)
Copyright © 2018-2021 python black hole network All Rights Reserved All rights reserved, and all rights reserved.京ICP备18063182号-7
For complaints and reports, and advertising cooperation, please contact vgs_info@163.com or QQ3083709327
Disclaimer: All articles on the website are uploaded by users and are only for readers' learning and communication use, and commercial use is prohibited. If the article involves pornography, reactionary, infringement and other illegal information, please report it to us and we will delete it immediately after verification!