Control statements
- 1.1 Branch Statements
- 1.2 Loop Statements
-
- 1.2.1 while statement
-
- 1.2.2 for statement
- 1.3 Jump Statement
- 1.4 Application Scope
Control statements: order, branch, and loop
8.1 Branch Statements
The if… Elif… else
score = int(input())
if score >= 90:
grade = 'A'
elif score >= 80:
grade = 'B'
elif score >= 70:
grade = 'C'
elif score >= 60:
grade = 'D'
else:
grade = 'F'
print(grade)
Copy the code
Conditional expression: this is actually the if-else expression 1 if conditional else expression 2
Score = int(input()) result = 'score' if score >= 60 else 'score' print(result)Copy the code
1.2 Loop Statements
1.2.1 while statement
Note that improper loop conditions can lead to dead loops
Else: # Execute after the loop ends normally. If the loop breaks, return, etc., the statement group is not executedCopy the code
1.2.2 for statement
The most widely used and powerful type of loop, also known as traversal, is used only for sequences, including strings, lists, and tuples
For iterates variable in sequence: statement group else: # Execute after loop ends normally. If loop breaks, return, etc., the statement group is not executedCopy the code
1.3 Jump Statement
Break and continue break is to break out of the current for or while loop. Continue is to skip the next loop, and the loop continues.
For I in range(5): if I == 3: break print(I) #Copy the code
For I in range(5): if I == 3: Continue print(I) #Copy the code
1.4 Application Scope
Range () is also a function that we use a lot. Range (start,stop,step), for example, range(0,10,2), which is to start at 0, step 2, and take numbers within 10, but not including 10
This article is from the SDK community: www.sdk.cn