• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / For Loop vs While Loop in Python

For Loop vs While Loop in Python

Author: Aditya Raj
Last Updated: May 8, 2023

We use for loop and while loop in Python for different tasks. This article discusses for loop vs while loop in Python to uncover their similarities and differences.

Table of Contents
  1. Syntax of For Loop in Python
  2. Syntax of While Loop in Python
  3. For Loop vs While Loop in Python
  4. Conclusion

Syntax of For Loop in Python

We use a for loop in Python to iterate through a container object such as a list, tuple, dictionary, etc. It has the following syntax.

for variable_name in iterable_obj:
    #do something with variable

Here, variable_name is a temporary variable used to iterate through the elements of iterable_obj. The variable takes all the values in the iterable object one by one. We can then perform operations on the variable as per our requirements.

For example, we can print the squares of all the elements of a list using the for loop as shown below.

myList=[1,2,3,4,5]
print("The list is:",myList)
for element in myList:
    square=element**2
    print("The square of {} is {}.".format(element,square))

Output:

The list is: [1, 2, 3, 4, 5]
The square of 1 is 1.
The square of 2 is 4.
The square of 3 is 9.
The square of 4 is 16.
The square of 5 is 25.

We can also use a for loop to execute a task a fixed number of times. For this, we use the range() function to create a range object. Then, we iterate through the elements of the range object to perform the desired task as shown in the following example.

for element in range(1,6):
    square=element**2
    print("The square of {} is {}.".format(element,square))

Output:

The square of 1 is 1.
The square of 2 is 4.
The square of 3 is 9.
The square of 4 is 16.
The square of 5 is 25.

Syntax of While Loop in Python

While loop is used to perform a task in Python until a condition is True. It has the following syntax.

While condition:
   #do something

Here, the statements inside the while loop are continuously executed until the condition becomes False.  For example, we can use the while loop to print the square of numbers from 1 to 5 as shown below.

value=1
while value<=5:
    square=value**2
    print("The square of {} is {}.".format(value,square))
    value+=1

Output:

The square of 1 is 1.
The square of 2 is 4.
The square of 3 is 9.
The square of 4 is 16.
The square of 5 is 25.

We can also use a while loop to execute a task a fixed number of times. For this, we will need to use a counter variable in place of the variable “value” to enforce the condition in the while loop as shown above.

For Loop vs While Loop in Python

Although the for loops and While loops can perform similar operations, there are many differences between the two loop constructs. The following table summarizes the differences between for loop vs while loop in Python. 

For LoopWhile Loop
For loop is used in Python to iterate through the elements of an iterable object and execute some statements. While loop is used to execute statements in Python until a condition remains True.
We cannot execute for loop based on a condition or until a condition is true.We can use a while loop to iterate through the elements of an iterable object like a list or tuple by finding their length.
For loop cannot run for an infinite number of times.We can run a while loop for an infinite number of times.
We can use the continue statement to skip the execution of statements in a for loop.We can use continue statements with while loops too to skip the execution of certain statements.
We can use the break statement to get out of a for loop.We can use the break statement to get out of a while loop too. 
For Loop vs While Loop in Python

Conclusion

In this article, we discussed the syntax of for loop and while loop in Python. We also had a discussion on for loop vs while loop in Python to understand the difference between the two loop constructs.  To learn more about Python programming, you can read this article on if vs elif vs else if in Python. You might also like this article on Python finally keyword.

I hope you enjoyed reading this article. Stay tuned for more informative articles. 

Happy Learning!

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us