Difference between for loop and while loop in Python
Last Updated :
24 Apr, 2023
In this article, we will learn about the difference between for loop and a while loop in Python. In Python, there are two types of loops available which are ‘for loop‘ and ‘while loop‘. The loop is a set of statements that are used to execute a set of statements more than one time. For example, if we want to print “Hello world” 100 times then we have to write a print statement 100 times which is a tedious task but with the help of loops we can do it in just a few lines of code. In this article, we will learn both types of loops separately and then their differences.
For Loop Vs While Loop Banner
For loop in Python
In Python, a ‘for loop‘ is used to iterate over a sequence of items, such as a Python tuple, list, string, or range. The loop will execute a block of statements for each item in the sequence.
Python for Loop Flowchart
For Loop Flow chart
Syntax of Python for loop
In the below syntax for is a keyword, var is the variable name, and iterable is an object which can be looped over or iterated over with the help of a for a loop. Objects like tuples, lists, sets, dictionaries, strings, etc. are called iterable. We can also use the range() function in place of iterable.
for var in iterable:
# statements
Python for Loop (With Examples)
In the below example, we have created a list of items and then iterate through the list using for loop to print the items in the list.
Python3
items = [ 'pen' , 'notebook' ,
'pencil' , 'lunch box' ]
for item in items:
print (item)
|
Output:
pen
notebook
pencil
lunch box
While Loop in Python
In Python, a while loop is used to repeatedly execute a block of statements while a condition is true. The loop will continue to run as long as the condition remains true.
Python while Loop Flowchart
While Loop Flow chart
Syntax of Python While loop
In the while loop condition is written just after the ‘while’ keyword and then we write the set of statements to perform some task.
while condition:
# Set of statements
Python while Loop (With Examples)
In this example, we are using a while loop to perform the task that we have done in the example of for loop. Here, after declaring the items list we initialize the index with 0 and store the length of the items list in the variable ‘items_len’ after that running a while loop in which we have given a condition that runs the loop until the value of the index is less than items_len. Inside the while loop, we print the items of the items list using indexing and increment the value of the index by 1 to iterate over the list.
Python3
items = [ 'pen' , 'notebook' ,
'pencil' , 'lunch box' ]
index = 0
items_len = len (items)
while index<items_len:
print (items[index])
index = index + 1
|
Output:
pen
notebook
pencil
lunch box
When no condition is given in the for and while loop?
In this case, when the condition is not given they will run into an infinite loop.
Python For Loop:
Python3
a = [ 1 ]
for i in a:
print ( "GFG" )
a.append(i)
|
Python While Loop:
Both of the loops will run for infinite times and print GFG.
Difference between for loop and while loop in Python
Now, we will compare both loops in Python to understand where to use ‘for loop’ and where to use ‘while loop’.
For loop
|
While loop
|
For loop is used to iterate over a sequence of items.
|
While loop is used to repeatedly execute a block of statements while a condition is true.
|
For loops are designed for iterating over a sequence of items. Eg. list, tuple, etc.
|
While loop is used when the number of iterations is not known in advance or when we want to repeat a block of code until a certain condition is met.
|
For loop require a sequence to iterate over.
|
While the loop requires an initial condition that is tested at the beginning of the loop.
|
For loop is typically used for iterating over a fixed sequence of items
|
While loop is used for more complex control flow situations.
|
For loop is more efficient than a while loop when iterating over sequences, since the number of iterations is predetermined and the loop can be optimized accordingly.
|
While a loop may be more efficient in certain situations where the condition being tested can be evaluated quickly.
|