所谓判断就是在程序中满足某些条件,才会做某些事情,不满足则不回去做
-
Judgment statement
if Judgment content
Action to be performed when my condition is met
Example:
Judging age, if age is greater than 18, print adult
age=int(input("Please enter your age"))
if age > 18:
print(" adults") -
If-else uses the format
if the condition to be judged
After the condition is met, the thing to be executed, the thing to be executed after the
else
condition is not established
Example:
Judging the age, if age is greater than or equal to 18, enter adult, otherwise minor
age =18
if age>=18 :
print("Adult")
-
if-elif-else
if condition to be judged: the
condition meets the code to be executed
elif the condition to be judged: the
condition meets the code to be executed
else:
the result is executed when none of the above conditions are met
4.if nested
if to be judged Condition:
what to do when the condition is true
if condition to be judged:
what to do when the condition is true else: what to do when the condition is not true else: what to do
when the condition is not true
-
Loop statement
The code that needs to be repeatedly executed many times can be completed in a loop. The
loop is not necessary, but in order to improve the repetition rate of the code -
while loop
while condition:
condition is met, need to be executed
condition is met, need to execute
...
-
Calculate the sum of even numbers between 1–100 (including 1 and 100)
-
while loop nested
while condition:
when the condition is true, what to do when the
condition is true, what to do
...
while condition:
when the condition is true, what to do when the
condition is true, what to do
...
-
Print the formula for the multiplication of ninety-nine
-
for loop
for temporary variable in list or string and other iterable objects:
the code to execute when the loop meets the condition,
print the data from 0 to 9 The
function of break: when the condition is met, immediately end the loop where the break is located The
function of continue: when the condition is met , is used to end this loop, and then execute the next loop
pass.
Pass does nothing, and is generally used as a placeholder statement.