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 Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is
8 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read