Chapter 7 of python3, python3
To allow computers to compute thousands of repeated operations, we need loop statements.
Loop statements in Python include
The execution process of loop statements, such:
While Loop
The general form of the while statement in Python:
While judgment condition: Statement
The execution process of the preceding syntax is:
If the condition is True, execute the statement in Block 1.
If the condition is False, execute the statement in Block 2.
We use while to do one thing and calculate 1 ~ 100 sum of all numbers:
N = 100sum = 0 counter = 1 while counter <= n: sum = sum + counter + = 1 print ("1 to", n, "sum:", sum)
The above code outputs:
The sum of 1 to 100 is: 5050
Have you found that using programs to do something is still quite worry-free? The future world is "machine" and "intelligent", which is a little far away. What happens if the condition of our loop statement is constant to True?
While 2> 1: # The expression is always true name = input ('enter YOUR name: ') print ("hello", name) print ("Good bye! ")
The above code outputs:
Enter your name: roy Hello roy enter your name:
You should have also discovered that the program will constantly ask you to enter the name, and the program is in an infinite loop (also known as an endless loop ).
While can also be used with else. When the condition is false, the statement block of else is executed:
While judgment condition: Statement 1 else: Statement 2
Code example:
Count = 0
While count <5:
Print (count, "Less than 5 ")
Count = count + 1
Else:
Print (count, "greater than or equal to 5 ")
The above code outputs:
0 less than 51 less than 52 less than 53 less than 54 less than 55 greater than or equal to 5
For Loop
Basic Syntax:
For variable in sequence: Statement Block
The execution process of the preceding syntax is:
Take an element from the sequence, assign it to the variable, and execute the statement block until all the elements of the sequence are assigned a value. This is similar to foreach in other languages.
Code example:
languages = ["C", "C++", "Perl", "Python"]for x in languages: print(x)
The above code outputs:
CC++PerlPython
For can also be used with else. When all values in the sequence are assigned once, execute the else statement block. The syntax is as follows:
For variable in sequence: Statement Block 1 else: Statement Block 2
Instance
Ages = ["C", "C ++", "Perl", "Python"] for x in ages: print (x) else: print ('here is else ')
The above code outputs:
CC ++ PerlPython here is else
Range () function
If you need to traverse numeric sequences, you can use the built-in range () function. It generates a series, for example:
for i in range(5): print(i)
The above code outputs:
01234
You can also use range to specify the range value:
for i in range(5, 9): print(i)
The above code output:
5678
Note that the above Code output contains 5, not 9
You can also set range to start with a specified number and specify different increments (or even negative numbers, sometimes called "step size "):
for i in range(0, 10, 3): print(i)
The above code outputs:
0369
3 is the step size.
Negative:
for i in range(-1, -10, -3): print(i)
The above code outputs:
-1-4-7
-3 indicates the step size.
Thinking: how to print 1 ~ The odd number of 100, all the methods you know are written. The following two types are provided. do not view them before writing the Code:
Method 1:
N = 1 while n <= 100: # <= meaning, see the Python comparison operator if n % 2 = 1: # % in chapter 5, see the meaning of the Python Arithmetic Operator print (n) n + = 1 # + = in Chapter 5. See the Python assignment operator in chapter 5.
View Code
Method 2:
for n in range(1, 101): if n % 2 == 1: print(n)
View Code
Break
In a loop, the break statement can exit the loop in advance. For example, if you want to print 1 ~ Number 5:
n = 1while n <= 5: print(n) n = n + 1print('END')
The above code outputs:
12345END
If we want to exit the loop when n = 3, we can do this:
n = 1while n <= 5: if n == 3: break print(n) n = n + 1print('END')
The above code outputs:
12END
The break statement can jump out of the for and while LOOP bodies. If you terminate a for or while LOOP, no corresponding loop else block will be executed. Example:
n = 1while n <= 5: if n == 3: break print(n) n = n + 1else: print('ELSE') print('END')
The above code outputs:
12END
Continue
During the loop process, you can also skip the remaining statements in the current loop block through the continue statement and continue the next loop. Similarly, circular printing is required ~ 5, but I want to do not print it when n = 3:
1 n = 1 2 3 while n <= 5: 4 5 if n == 3: 6 n = n + 1 7 continue 8 print(n) 9 n = n + 110 11 print('END')
The above code outputs:
1245END
Think about what will happen if you delete 6th lines of code n = n + 1? Why?
Continue only terminates the current loop and does not terminate the entire loop. Therefore, the else statement will continue to be executed:
n = 1while n <= 5: if n == 3: n = n + 1 continue print(n) n = n + 1else: print('ELSE')print('END')
The above code outputs:
1245ELSEEND
Note:
- The break statement can exit the loop directly during the loop process, while the continue statement can end the current loop in advance and start the next loop directly. These two statements must be used together with the if statement.
- Note that do not abuse the break and continue statements. Break and continue may cause too many code execution logic forks and are prone to errors. The break and continue statements are not required for most cycles. In the preceding example, you can rewrite the loop conditions or modify the loop logic to remove the break and continue statements. Try to rewrite the statements by yourself.
Pass statement
Pass is an empty statement to maintain the integrity of the program structure. Pass does not do anything. It is generally used as a placeholder statement, as shown in the following example:
while True: pass
Let's take a look at the following code:
N = 1 while n <= 5: if n = 3: pass print ('execution pass Block') print (n) n = n + 1 print ('end ')
The above code outputs:
12. Execute the pass block 345END