While loop in Python


Keywords: loops, iterations sets, conditional loops, counting loops, while loop, for loop,

Input in Python learn programming

The while loop in Python is used when you want an operation to be repeated as long as a specified condition is met.

What is a While loop in Python?

The While loop is used to iterate (repeat) part of the program several times. If the number of iterations (repetitions) you want to perform is not fixed, it is recommended to use a While loop. Generally, we can imagine the While loop as a repetition of one or more sequences that occur as long as one or more conditions are met.

How while loop in Java works? 

The while loop is used to iterate a sequence of operations several times. In other words, you repeat parts of your program several times, thus enabling general and dynamic applications because code is reused any number of times. If the number of iterations not is fixed, it’s recommended to use a while loop.

The flow chart below shows the functions of a while loop

Loops Python Programming

Let’s break it down

  1. The code sequence begins at Start and then checks if the loop conditions are met.
  2. If the condition is met, true, the program performs the operation. 
  3. When these operations are completed, the code will return to the while condition.
  4. The loop then repeats this process until the condition is false, and then the program continues.

In short, the while loop in Python: 

  • Is a loop that repeats a sequence of operations an arbitrary number of times.
  • Repeats the operations as long as a condition is true.
  • Enables general and dynamic applications because code can be reused.
  • Best suited when the number of iterations of the loop is not fixed.

Create while loop in Python

While loop in Python:

  • You create the while loop with the reserved word while, followed by a condition
  • Within the indent in the row below, you specify all operations that you want to execute as long as the condition is true
  • The loop repeats itself until the condition is no longer met, that is, false.

Syntax: Declare while loop in Python

If we use the elements in the list above and insert in the code editor:

while condition:
    code executed within the loop

All code indented after the while command will execute if the condition is True

Example: How to use while loop in Python

We’ll see a simple example of how we can use the while loop to perform an operation while the specified condition is met.

  • We will start by declaring an integer variable (named ” i “) and assign the value zero.

After that we create our while loop.

  • The condition we want to use is that, when the variable ” i ”  is less than 5.
  • The operation within the loops prints the value for the variable i for every operation.
  • After the operation is performed, the variable  i  is incremented by 1.
i = 0 

while i < 5: 
   print(i) 
   i = i +1 # add 1 to the variable i

In this example, we print the value for the variable i as long as the condition is met. In other word,  as long as i is less than 5. The program will thus print the value five times and then end the while loop. The result is

0
1
2
3
4

Note, what had happened if i ++ had not been in the loop? Exactly, the condition for the loop will always be fulfilled (zero is always less than five) and the while loop will not end.

Summary: While loop in Python

A while loop in Java is a so-called condition loop. This means repeating a code sequence, over and over again, until a condition is met. Instead of having to rewrite your code several times, we can instead repeat a code block several times. If the number of iterations not is fixed, it’s recommended to use a while loop.

Syntax: While loop in Python

while condition:
    code executed within the loop