Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Difference between for loop and while loop in Python
Next article icon

Difference between for loop and while loop in Python

Last Updated : 28 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Difference between for loop and while loop in Python
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 in Python
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
# 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 in Python
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
while True:     print("GFG") 

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.


Next Article
Difference between for loop and while loop in Python

S

sagar99
Improve
Article Tags :
  • Python
Practice Tags :
  • python

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). In this article, we will look at Python loops and understand their working with the help of examples. While Loop in PythonIn Python, a while loop is us
    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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences