Difference Between For Loop and While Loop in Python

2 Jun 2025 | 5 min read

In Python, 'for' and 'while' loops are two of basic operations used to repeat a certain action for multiple times. Although they share similarities, they have distinct mechanics and applications. We generally use the 'for' loop in case the number of iteration is known beforehand; and use the 'while' loop for unknown number of iterations.

What is the 'for' Loop?

The 'for' loop in Python is a control flow statement that allows us to execute a block of code multiple times, once for each item in a sequence or iterable (such as lists, tuples, strings, dictionaries, or ranges).

Syntax:

  • variable: A temporary name used to refer to each item in the iterable.
  • iterable: A collection of elements (e.g., list, string, range).
  • The loop executes once for each item in the iterable.
  • The indented block under the loop runs on every iteration.

Examples on Python 'for' Loop

We will now look at some examples of Python 'for' loop:

Example 1: Iterating Over a List

In this example, we will see how to iterate over the items in a list using the 'for' loop.

Output:

apple
banana
cherry

Explanation:

In the above example, we used the 'for' loop to iterate through each element in the list. During each iteration, the element from the given list is stored in a variable and value is printed. The loop automatically exits once all elements from the list is traversed.

Example 2: Use of range() with 'for' Loop

We will now take a look at an example to understand the working of the 'for' loop with the range() function.

Output:

1
2
3
4
5

Explanation:

In this example, we have used the range(1, 6) function to create a range object having numbers ranging from 1 to 5. We then used the 'for' loop to iterate over the numbers from this object and print them.

What is the 'while' Loop?

Python 'while' loop is used to repeatedly execute a block of code as long as a given condition remains True. It is useful when the number of iterations is unknown beforehand.

Syntax:

  • condition: A Boolean expression that is evaluated before each iteration.
  • The code block inside the loop is executed only if the condition is true.
  • If the condition never becomes False, the loop will run infinitely, potentially causing an infinite loop.

Examples on Python 'while' Loop

We will now look at some examples of Python 'while' loop:

Example 1: Basic while Loop

In this example, we will see how 'while' loop functions in Python.

Output:

1
2
3
4
5

Explanation:

In this example, we have created a counter with an initial value of 1. We have then used the 'while' loop to iterate under a specified condition. Inside this loop, we printed the counter value and increment it by 1. This loop iterates until the condition remains True.

Example 2: Use of the 'while' loop with 'else'

In this example, we will see how to use the 'while' loop with the 'else' statement

Output:

1
2
3
Loop completed successfully!

Explanation:

In this example, we have created a counter with an initial value of 1. We have then used the 'while' loop with the 'else' statement to iterate under a specified condition. Inside this loop, we printed the counter value and increment it by 1. This loop iterates until the condition remains True. Under the 'else' block, we have printed a statement which runs when the condition becomes False.

Key Differences Between For and While Loop

The following are key differences between Lists and Tuples in Python:

Featurefor Loopwhile Loop
DefinitionIterates over a sequence (like list, string, or range).Repeats a block of code as long as a condition is True.
UsageWhen the number of iterations is known or a sequence is available.When the number of iterations is unknown or depends on a condition.
Syntaxfor variable in iterable:
# code block
while condition:
# code block
Loop ControlControlled by an iterable (e.g., list, tuple, range).Controlled by a condition that must eventually become false.
Loop VariableAutomatically updates with each iteration.Needs to be manually updated inside the loop.
Risk of Infinite LoopLow, since it depends on an iterable.Higher, if the condition is never made false.
Common Use CasesIterating over items in a list, string, dictionary, etc.Waiting for user input, checking sensor data, or running until a condition.
Structure SimplicityMore concise when working with sequences.More flexible for complex or dynamic conditions.
TerminationEnds automatically when the iterable is exhausted.Ends only when the condition becomes false.

Difference between For Loop and While Loop in python FAQs

1. What is the difference between a 'for' loop and a 'while' loop in Python?

The major distinction is that a 'for' loop is specifically meant for situations when the number of iterations is known or when traversing a sequence (a list, tuple, string, etc.), while a 'while' loop is used in situations where the number of iterations is unknown and is based on a certain condition that is checked prior to each iteration.

2. In what situation can we apply a 'for' loop?

A 'for' loop is applicable in cases where you are dealing with a sequence or the iteration cycles is known. This is ideal for going through a set of values, a collection of items or even characters in a word or phrase.

3. When is a 'while' loop appropriate?

A 'while' loop is more appropriate in a case where it needs to run the loop based on a certain condition instead of a designated number of times. Common examples of this are input validation, waiting for a condition to become false, or looping until a certain event occurs.

4. Can both loops create infinite loops

Indeed, both iterations could result in infinite loops. As we have learned, 'while' loops become infinite if the condition provided is never met and thus assumed to be false. While less common, a 'for' loop can also undergo infinite looping, particularly if one is iterating over a generator function or an open-ended iterable that lacks a termination condition.

5. In python, do break, continue, and even else are applicable in both loops?

Yes, as both the 'for' and 'while' loops exist in Python, they all support break to prematurely exit the loop, continue to skip the current iteration, and else to execute a block of code only if the loop completes without a break being encountered.


Next TopicPython Lists