Day 4 loop structure (if and while)

Keywords: Python

Day 4 cycle

1, Loop structure - allows code to be executed repeatedly, code to be written once, and executed multiple times. for loops and while loops

1. For loop: python is completely different from java's for loop

Syntax:

for variable in sequence:
Circulatory body
Description: for, in - Keywords
Variable - variable name (the variable name of a variable that has been defined, or it can be undefined)
Sequence - data corresponding to container data type in python. (for example: string, list, ancestor, iterator, generator, dictionary)
: - Fixed writing
Loop body - one or more statements (at least one condition) that maintain an indentation with for. The loop body is the code that will be executed repeatedly
Execution principle: let variables take values in the sequence, and take a value to execute the loop body once. Take it one by one until it is finished.
The number of for loops is related to the number of elements in the sequence

Execution principle of for loop in java:

Syntax mechanism and operation principle of for loop in Java:
For (initialization expression; Boolean expression; update expression){
Loop body (composed of java statements);
}
be careful:
1. The initialization expression is executed first and only once throughout the loop.
2. The result of the conditional expression must be a boolean type, that is, true or false.
Execution principle: first execute the initialization expression, and the initialization expression is executed only once. Then judge the result of the conditional expression. If it is true, execute the loop body.
After the loop body ends, execute the update expression. After the update, judge the result of the conditional expression. If it is still true, continue to execute the loop body.
Until the execution of the update expression is completed, when the condition is judged again, the condition is false,
The for loop terminates.
The function of the update expression is to control the number of cycles. In other words, the update expression will update the value of a variable, so that the result of the conditional expression may change from true to false
Terminates the execution of the for loop. If an update expression is missing, it is likely to cause an endless loop.

2. range function

1. Function: generate an equal difference sequence, which is called step size in the program

2. Usage:

1,range(N)  _ Generate [0, N),The difference is an equal difference sequence of 1. N Must be a positive integer left closed right open interval,  range(3)The result is 0,1,2
2,range(M,N) - Produce[ M,N), The difference is an equal difference sequence of 1 M Must be less than N
3,range(M,N,step)  produce[M,N) Bad is step Arithmetic sequence of. range(10,4,-1) - 10,9,8,7,6,5
for i in range(3):
#     print(i)
#     print("===================")
# for x in range(-3, 3):
#     print(x)
for i in range(5):
    print(i)
    print("5 times")
for x in range(1, 5):
    print(x)
    print("4 times")
for m in range(1, 5, 2):
    print(m)
    print("Execute twice")

3. Summation routine:

(1) Define a variable to save the last sum. The default value of the variable is 0;

(2) Obtain the required and data through circulation.

(3) When you get a number, add a data to the saved and variable.

sum=0
for i in range(1,101):
  sum+=i
pri(sum)

4. Statistical number routine

# Count the number less than 60 in the score
scores = [45, 78, 90, 45, 67, 100, 90, 98, 76, 55]
# Define the last number of variables to save. The default value is 0
cnt = 0
# Get all statistical objects through loop
for g in scores:
    if g < 60:
        cnt += 1
print("Number less than 60", cnt)
# Count the number of numbers that can be divided by 3 within 100 (1-99)
count = 0
for f in range(1, 100):
    if f % 3 == 0:
        count += 1
print("The number of numbers that can be divided by 3", count)
# Method 2
count1 = 0
for x in range(3, 100, 3):
    count += 1
print("Number divisible by 3", count1)

3. while loop

Syntax:
while conditional statement:
Circulatory body
while - keyword: fixed writing
Conditional statement - any expression with results, such as specific data, assigned variables and operation expressions (except assignment operation)
: fixed writing
Loop body - one or more statements (at least one) that maintain an indentation with while; the loop body is the code that needs to be executed repeatedly
Execution process: first judge whether the condition is true. If it is true, execute the loop body. After execution, judge whether the result of the condition statement is true. Then continue to execute the loop body
Until the result of the conditional statement is FALSE, the loop ends.
Extreme: if it is false at the beginning, it will not be executed. It is also possible that if the result is always true, it will enter an endless loop, resulting in the non execution of subsequent code.

# Print 1,2,3,4,5
for x in range(1, 6):
    print(x)
num = 1
while num < 5:
    print(num)
    num += 1

Selection of for and while

# If the number of cycles is certain, select for. If the number of cycles is uncertain, select while
Exercise: prompt the user to enter data until the value entered is 0
a = 1  # The initial value of a is OK as long as it is not 0. If it is not defined, an error will be reported in while because there is no variable defined
while a != 0:
    a = int(input("Please enter a number"))
# Practice and complete the number guessing game
'''
The game begins to produce a 0-100 Let the user continuously input numbers until the input numbers are equal to the generated random numbers. If you guess wrong, it will be typed or small
 Tips for
'''
import random
value = random.randint(0, 100)  # The value of value is a random number from 0 to 100
num = int(input("Please enter a number"))
while num != value:
    if num > value:
        print("The number you entered is too large")
    else:
        print("The number you entered is too small")
    num = int(input("Please enter a number"))  # Without this line of code, the loop body will continue to loop with the data entered for the first time
print(value)

Posted by meigwil on Fri, 03 Dec 2021 04:45:43 -0800