TutorialAce

Python | Java | MySQL | Excel

Python Tutorial

Python While Loop with Examples

How to Use the Python While Loop

In this tutorial, we'll learn the Python While loop with examples.

A while loop is a control flow statement that allows to execute a block of code repeatedly as long as a given condition is true.

Python While Loop Syntax

The syntax of a while loop statement is as follows:

while(expression):
statement_list

Here, the statement_list can be a single statement or a block of statements. While a condition is true, the loop iterates and the program executes the statement_list.

Remarks

  • Use the break statement if you want to exit the loop.
  • Use the continue statement if you want to skip and continue the next iteration.

Examples

Example 1: Iterate While a Condition is True

The following code print the value of x as long as x is less than 4:

x = 0
while(x < 4):
print (x)
x += 1

Make sure to increment x, or the loop will continue forever.

Result:

0
1
2
3

Example 2: Iterate While a Condition is True

The following code print the value of x as long as x is greater than or equal to 0:

x = 5
while(x >= 0):
print (x)
x -= 1

Make sure to decrement x, or the loop will not stop.

Result:

5
4
3
2
1
0

Example 3: Using the Continue Statement to Continue to the Next Iteration

We can stop the current iteration, and continue with the next using the IF and continue statements.

The following code continues to the next iteration when x is 4:

x = 5
while(x >= 0):
if(x == 4):
x -= 1
continue
print (x)
x -= 1

Result:

5
3
2
1
0

Example 4: Using the Break Statement to Exit the Loop

We can stop the loop even if the condition is true using the IF and break statements.

The following code exits the loop when x is 2:

x = 5
while(x >= 0):
if(x == 2):
break
print (x)
x -= 1

Result:

5
4
3

Example 5: Find the Maximum Value in a List

The following code illustrates how to find the maximum value in a list of numbers:

list = [2, 3, 4, 5, 10, 2, 20, 9, 21]
count = len(list)
x = 0;
max = 0
while(x < count):
if(list[x] > max):
max = list[x]
x += 1
print ("The maximum value is", str(max) + ".")

Result:

The maximum value is 21.

Example 6: Find the Minimum Value in a List

The following code illustrates how find the minimum value in a list of numbers:

list = [10, 2, 3, 4, 5, 10, 2, 20, 9, 21]
count = len(list)
min = list[0]
while(count > 0):
count -= 1
if(list[count] < min):
min = list[count]
print ("The minimum value is", str(min) + ".")

Result:

The minimum value is 2.

Example 7: Sum Values in a List

The following code illustrates how to sum all values in a list of numbers:

list = [100, 20, 13, 24, 57, 109, 22, 20, 90, 120]
sum = 0;
count = len(list)
while(count > 0):
sum += list[count-1]
count -= 1
print ("The total value is", str(sum) + ".")

Result:

The total value is 575.

In this tutorial, we've learned the Python While loop with examples. A while loop iterates while a condition is true. If you want to exit the loop, use the break statement, and use the continue statement if you want to skip and continue to the next iteration. If you know the number of iterations, you should use the For loop statement.

Leave a Reply