Python While Loop Statement

Python While Loop Statement

The while statement in Python programming is used to execute a program in a loop, that is, under a certain condition, execute a certain program in a loop to process the same task that needs to be processed repeatedly. Its basic form is:

while judgment condition:
    execute statement...

The executed statement can be a single statement or a block of statements. The judgment condition can be any expression, and any non-zero, or non-null (null) value is true.

When the judgment condition is false, the loop ends.

The execution flow chart is as follows:

python_while_loop

Gif demonstrates the execution process of Python while statement

 

example

#!/usr/bin/python count = 0 while (count < 9): print 'The count is:', count count = count + 1 print "Good bye!"


Running the instance »

The output of the above code execution:

The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Good bye!

There are two other important commands in the while statement: continue, break to skip the loop, continue is used to skip the loop, break is used to exit the loop, and the "judgment condition" can also be a constant value, indicating that the loop must be Established, the specific usage is as follows:

# continue and break usage i = 1 while i < 10: i += 1 if i%2 > 0: # skip output if non-double number continue print i # output double number 2, 4, 6, 8, 10 i = 1 while 1: # If the loop condition is 1, it must be established print i # Output 1~10 i += 1 if i > 10: # When i is greater than 10, jump out of the loop break

 

Infinite loop

If the conditional statement is always true, the loop will be executed indefinitely, as in the following example:

example

#!/usr/bin/python # -*- coding: UTF-8 -*- var = 1 while var == 1 : # This condition is always true, and the loop will execute infinitely num = raw_input("Enter a number : ") print "You entered: ", num print "Good bye!"

 

The output of the above example is:

Enter a number  :20
You entered:  20
Enter a number  :29
You entered:  29
Enter a number  :3
You entered:  3
Enter a number between :Traceback (most recent call last):
  File "test.py", line 5, in <module>
    num = raw_input("Enter a number :")
KeyboardInterrupt

 

Note: In the above infinite loop you can use CTRL+C to break the loop.

 

loop using else statement

In python, while ... else executes an else block when the loop condition is false:

example

#!/usr/bin/python count = 0 while count < 5: print count, " is less than 5" count = count + 1 else: print count, " is not less than 5"

The output of the above example is:

0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 is not less than 5

 

simple statement group

Similar to the syntax of the if statement, if you have only one statement in the body of your while loop, you can write that statement on the same line as the while, like this:

example

#!/usr/bin/python flag = 1 while (flag): print 'Given flag is really true!' print "Good bye!"

Note: In the above infinite loop you can use CTRL+C to break the loop.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325906949&siteId=291194637