Difference between for loop and while loop in Python
Last Updated :
28 Apr, 2025
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 BannerFor 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 chartSyntax 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
# Create a list of items
items = ['pen', 'notebook',
'pencil', 'lunch box']
# Run a loop to print
# items in a list
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
# Create a list of items
items = ['pen', 'notebook',
'pencil', 'lunch box']
# Declare a index
index = 0
# Store length of items list
items_len = len(items)
# Run a loop to print
# items in a list
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:
Python3
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.
|
Similar Reads
Python - Difference between := and == In this article, we will be discussing the major differences between Walrus(:=) and the Comparison operator (==):= in PythonThe := operator in Python, also known as the walrus operator or assignment expression, was introduced in Python 3.8. It enables assignment and evaluation to happen simultaneous
2 min read
Loops in Python - For, While and Nested Loops Loops in Python are used to repeat actions efficiently. The main types are For loops (counting through items) and While loops (based on conditions). Additionally, Nested Loops allow looping within loops for more complex tasks. While all the ways provide similar basic functionality, they differ in th
9 min read
Decrement in While Loop in Python A loop is an iterative control structure capable of directing the flow of the program based on the authenticity of a condition. Such structures are required for the automation of tasks. There are 2 types of loops presenting the Python programming language, which are: for loopwhile loop This article
3 min read
Difference between continue and pass statements in Python Using loops in Python automates and repeats the tasks in an efficient manner. But sometimes, there may arise a condition where you want to exit the loop completely, skip an iteration or ignore that condition. These can be done by loop control statements. Loop control statements change execution from
3 min read
Different Input and Output Techniques in Python3 An article describing basic Input and output techniques that we use while coding in python. Input Techniques 1. Taking input using input() function -> this function by default takes string as input. Example: Python3 #For string str = input() # For integers n = int(input()) # For floating or deci
3 min read
Loops and Control Statements (continue, break and pass) in Python Python supports two types of loops: for loops and while loops. Alongside these loops, Python provides control statements like continue, break, and pass to manage the flow of the loops efficiently. This article will explore these concepts in detail.Table of Contentfor Loopswhile LoopsControl Statemen
2 min read
Python Do While Loops In Python, there is no construct defined for do while loop. Python loops only include for loop and while loop but we can modify the while loop to work as do while as in any other languages such as C++ and Java.In Python, we can simulate the behavior of a do-while loop using a while loop with a condi
6 min read
Use for Loop That Loops Over a Sequence in Python In this article, we are going to discuss how for loop is used to iterate over a sequence in Python. Python programming is very simple as it provides various methods and keywords that help programmers to implement the logic of code in fewer lines. Using for loop we can iterate a sequence of elements
3 min read
Using Else Conditional Statement With For loop in Python Using else conditional statement with for loop in python In most of the programming languages (C/C++, Java, etc), the use of else statement has been restricted with the if conditional statements. But Python also allows us to use the else condition with for loops. The else block just after for/while
2 min read
Loops in R (for, while, repeat) Loops are fundamental constructs in programming that allow repetitive execution of code blocks. In R loops are primarily used for iterating over elements of a vector, performing calculations and automating repetitive tasks. In this article we will learn about different types of loops in R.1. For Loo
6 min read