Python while Loop with Examples

How to Use the While Loop Statement in Python

In this tutorial, you will learn about the while loop statement in Python with examples. The while loop is a control flow statement that allows a code block to be executed repeatedly as long as the given condition remains true.

See also:

Python WHILE Loop Syntax

The syntax of the while loop statement is as follows:

while(expression):
statement_list

A statement_list can be a single statement or a block of statements. While the condition remains 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 to the next iteration.

Examples

Iterating while a condition is true

The following code prints the value of x while x is less than 4.

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

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

Output:

0
1
2
3

The following code prints the value of x while x is greater than or equal to 0:

  1. x = 5
  2. while(x >= 0):
  3. print (x)
  4. x -= 1

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

Output:

5
4
3
2
1
0

Continue and while loop statements

The if and continue statements can be used to stop the current iteration and continue with the next iteration.

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

  1. x = 5
  2. while(x >= 0):
  3. if(x == 4):
  4. x -= 1
  5. continue
  6. print (x)
  7. x -= 1

Output:

5
3
2
1
0

Using the break statement to terminate the loop

You can use if and break statements to exit the loop even if the condition remains true.

The following code terminates the loop when x is 2:

  1. x = 5
  2. while(x >= 0):
  3. if(x == 2):
  4. break
  5. print (x)
  6. x -= 1

Output:

5
4
3

Finding the maximum value in the list

The following code shows an example of finding the maximum value from a list of numbers:

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

Output:

The maximum value is 21.

Finding the minimum value in the list

The following code is an example of how to find the smallest number in the list:

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

Output:

The minimum value is 2.

Sum of values in the list

The following code shows an example of summing all the numbers in a list:

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

Output:

The total value is 575.

Summary

In this tutorial, you learned how to use the while loop statement in Python with examples. The while loop iterates through a code block while certain conditions remain True.
To exit the while loop, you can use the break statement. To skip the current iteration and continue to the next iteration, you can use the break statement. Instead of a while loop, use a for loop statement when you know how many times you want to loop through a code block.


See also:
Python return Statement with Examples
Python List Methods and Functions with Examples
Python abs() – Absolute Value in Python
Python Factorial – Find the Factorial of a Number
Python min() – Find the Smallest Value in Python

Leave a Comment