Fully parse the usage of the While LOOP statement in Python and fully parse the python
In Python programming, the while statement is used to execute a program cyclically. That is, under a certain condition, a program is executed cyclically to process the same task that needs to be processed repeatedly. The basic form is:
While judgment condition: execution statement ......
The execution statement can be a single statement or statement block. The condition can be any expression, and any non-zero or non-null value is true.
When the condition is false, the loop ends.
The execution flow chart is as follows:
Instance:
#!/usr/bin/pythoncount = 0while (count < 9): print 'The count is:', count count = count + 1print "Good bye!"
The output result of the above code execution:
The count is: 0The count is: 1The count is: 2The count is: 3The count is: 4The count is: 5The count is: 6The count is: 7The count is: 8Good bye!
When the while statement is run, there are two other important commands: continue, break to skip the loop. continue is used to skip this loop, and break is used to exit the loop, in addition, the "judgment condition" can also be a constant value, indicating that the cycle must be true. The specific usage is as follows:
# Continue and break usage I = 1 while I <10: I + = 1 if I % 2> 0: # Skip output continue print I # output dual number 2, 4, 6, 8, 10i = 1 while 1: # print I # output 1 ~ must be set when the loop condition is 1 ~ 10 I + = 1 if I> 10: # break out of the loop when I is greater than 10