Loops are used in programming to repeat a specific block of code. In this article, you will learn to create a while loop in Python.
Video: Python while Loop
What is while loop in Python?
The while loop in Python is used to iterate over a block of code as long as the test expression [condition] is true.
We generally use this loop when we don't know the number of times to iterate beforehand.
Syntax of while Loop in Python
while test_expression: Body of while
In the while loop, test expression is checked first. The body of the loop is entered
only if the test_expression
evaluates to True
. After one iteration, the test expression is checked again. This process continues until the test_expression
evaluates to False
.
In Python, the body of the while loop is determined through indentation.
The body starts with indentation and the first unindented line marks the end.
Python interprets any non-zero value as True
. None
and 0
are interpreted as False
.
Flowchart of while Loop
Example: Python while Loop
# Program to add natural
# numbers up to
# sum = 1+2+3+...+n
# To take input from the user,
# n = int[input["Enter n: "]]
n = 10
# initialize sum and counter
sum = 0
i = 1
while i