Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Loops in Python - For, While and Nested Loops
Next article icon

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

InPython, a'for loop' is used to iterate over a sequence of items, such as aPython tuple,list,string, orrange. 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.

forvar initerable:

   # 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 itemsitems=['pen','notebook','pencil','lunch box']# Run a loop to print# items in a listforiteminitems:print(item)

Output:

pennotebookpencillunch box

While Loop in Python

InPython, 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.

whilecondition:

    # 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 theindex is less thanitems_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 itemsitems=['pen','notebook','pencil','lunch box']# Declare a indexindex=0# Store length of items listitems_len=len(items)# Run a loop to print# items in a listwhileindex<items_len:print(items[index])index=index+1

Output:

pennotebookpencillunch 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]foriina:print("GFG")a.append(i)

Python While Loop:

Python3
whileTrue:print("GFG")

Both of the loops will run for infinite times and printGFG.

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.


Improve
Article Tags :
Practice Tags :

Similar Reads

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 ourCookie 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

[8]ページ先頭

©2009-2025 Movatter.jp