Python While


The while Loop

With the while loop we can execute a set of statements as long as a condition is true.

Example

Print i as long as i is less than 6:

i = 1
while i < 6:
  print(i)
  i += 1
Try it Yourself »

Note: remember to increment i, or else the loop will continue forever.

The while loop requires relevant variables to be ready, in this example we need to define an indexing variable, i, which we set to 1.



Copyright 1999-2023 by Refsnes Data. All Rights Reserved.