Python basics -- for/while LOOP, pythonwhile
Python version: 3.6.2 Operating System: Windows Author: SmallWZQ
During school hours, I often encounter this situation: in order to punish the students, the teacher will say, "XXX, you should first go to the playground for 10 laps and then come back to continue reflection ."
This is short but profound. However, at that time, I could not understand the "meaning. It wasn't until college that I realized the meaning of C ++ programming. Originally, this sentence was full of programming philosophy. What philosophy? Around the playground runway, re-run for 10 laps ...... This is not a "loop "?
To put it bluntly, what is the function of the for/while loop? The Python basics mentioned earlier do not require loops. Next, let's look at a situation ~~~
Scenario 1: sum. Calculate the sum of 1 + 2 + 3 and output the result.
1 # Haha, I don't need to loop. Check what you think. 2> sum = 1 + 2 + 33> print (sum) 4 6
Case 2: sum. Calculate the sum of 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 and output the result.
1 # Haha, I don't need to loop, see what you think about 2> sum = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 103> print (sum) 4 55
Case 3: sum. 1 + 2 + 3 + ...... + 99 + 100 and output the result.
1 # Haha, I don't need to loop. What do you do? 2 # I can do this. 3> sum = 1 + 2 + 3 + ...... + 99 + 100 4 Traceback (most recent call last): 5 File "<stdin>", line 1, in <module> 6 TypeError: unsupported operand type (s) for +: 'int' and 'ellipsis '7 # the charm of loops lies in this 8 >>> sum = 0 9 >>> for I in range (101 ): 10 sum = sum + i11 >>> print (sum) 12 5050
It seems that the loop is useless.
Range (): generates an integer sequence. For example, the sequence generated by range (3) is an integer from 0 to 2:
1 # range () method 2 >>> list (range (3) 3 [, 2] 4 >>> list (range () 5, 5]
While Loop
I can't use a stupid method to learn Python ~~~
Just like case 2 above, write the summation expression directly.
To avoid heavy code in the second scenario, you can use the while loop:
1 # while loop 2 >>> sum = 03 >>> I = 04 >>> while I <11: 5... sum = sum + i6... I = I + 17... 8 >>> print (sum) 9 55
In the while LOOP, I continues to add one after another until I changes to 11 and does not meet the (I <11) while condition. The loop exits.
A while loop can be used to repeatedly execute a code block when any condition is True.
Therefore, while loop: as long as the condition is met, the loop continues, and exit when the condition is not met.
For Loop
Generally, the while loop can deal with most loop cases. However, there are still some places where the while loop is insufficient. In this case, you need to show your skills for... in.
For example, to execute a code block for each element of a set:
1 # Set 2> L = ['I', 'can ', 'python ','! '] 3 >>> for I in L: 4... print (I) 5... 6 I7 can8 Python9!
For the summation formula, the for... in loop is more concise than the while loop:
1 #... in loop is simpler than while LOOP 2 # summation formula 3 #... in loop 4 >>> sum = 0 5 >>> for I in range (11): 6... sum = sum + I 7... 8 >>> print (sum) 9 5510 # while loop 11 >>>> sum = 012 >>> I = 013 >>> while I <11: 14... sum = sum + i15... I = I + 116... 17 >>> print (sum) 18 55
Note: If you can use the for... in loop, try not to use the while loop.
We do not recommend the break and continue statements in the loop. Why? One is error-prone; the other is to replace the break statement and the continue Statement by rewriting the loop condition or modifying the loop logic.
Endless loop kiss
I believe everyone is impressed with the endless loop. Sometimes, if the code is written incorrectly, the program will be stuck in an "endless loop", that is, the endless loop.
How can this problem be solved? The method is as follows:
1. Press Ctrl + C to exit the program directly or forcibly terminate the Python process.
This method is very effective for endless loops caused by unexpected problems when writing Python program code.
Empty code block
In Python, empty code blocks are invalid.
The solution is as follows:
Ii. pass statement: do nothing.
To avoid empty code block errors, add a pass statement to the cyclic code block.
1 # pass Statement 2 >>> for I in range (11): 3... 4... 5 File "<stdin>", line 3 6 7 ^ 8 IndentationError: expected an indented block 9 # null code block error 10 >>> for I in range (11 ): 11... pass12... 13 >>>
Summary
1. Execute a statement block for each element in the sequence, or continue to execute a statement when the condition is true;
2. You can use the continue statement and break statement, but it is not recommended;
3. For the endless loop problem, Ctrl + C will exit the program directly;
4. Loop: An Effective Way for computers to repeat tasks.