Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python While Loop Statements

Python While Loop Statements

In Python programming, the while statement is used to execute a loop, i.e., to repeatedly execute a block of code under a certain condition in order to handle the same task that needs to be repeated. Its basic form is:


while 判断条件(condition):
    执行语句(statements)……

The executed statement can be a single statement or a block of statements. 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 diagram is as follows:

A GIF demonstrating the execution process of a Python while statement

A bit more complex:

Example

#!/usr/bin/python

count

=

0

while

(

count

<

9

)

:

print

'

The count is:

'

,

count

count

=

count

1

print

"

Good bye!

"

Run Example »

The output of the above code:


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 with the while statement: continue and break, used to skip parts of the loop. continue is used to skip the current iteration, while break is used to exit the loop. In addition, the condition can be a constant value, meaning the loop is always true. The specific usage is as follows:

Usage of continue and break

i

=

1

while

i

<

10

:

i

+=

1

if

i

%

2

0

:

Skip output when the number is not even

continue

print

i

Output the even numbers 2, 4, 6, 8, 10

i

=

1

while

1

:

The loop condition of 1 is always true

print

i

Output 1 to 10

i

+=

1

if

i

10

:

Exit the loop when i is greater than 10

break


Infinite loop

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

Example

#!/usr/bin/python

-- coding: UTF-8 --

var

=

1

while

var

==

1

:

This condition is always true, so the loop will execute indefinitely

num

=

raw_input

(

"

Enter a number :

"

)

print

"

You entered:

"

,

num

print

"

Good bye!

"

The example above produces the following output:


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 
    num = raw_input("Enter a number :")
KeyboardInterrupt

**Note:** You can use CTRL+C to interrupt the infinite loop above.


Using the else Statement in Loops

In Python, while ... else executes the 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 an if statement, if your while loop body contains only one statement, you can write that statement on the same line as while, as shown below:

Example

#!/usr/bin/python

flag

=

1

while

(

flag

)

:

print

'

Given flag is really true!

'

print

"

Good bye!

"

**Note:** You can use CTRL+C to interrupt the infinite loop above.