In Python, the for loop and while loop are two of the basic operations used to repeat a specified action multiple times. Both loops do share similarities; however, they have distinct mechanics and applications. We generally use the for loop in case the number of iterations is known in advance, and we use the while loop for an unknown number of iterations.
The Python for loop is a programming mechanism used for iterating over a sequence until a given condition is met.
The for loop in Python allows us to iterate over iterable objects, such as tuples, lists, dictionaries, and strings, until it reaches the termination of the sequence or the condition is fulfilled. The for loop proceeds to the next step after each iteration is completed.
It has the following syntax:
Parameter(s):
We will now look at some examples of Python 'for' loop:
Basic Example of a For Loop in Python
In this example, we will see a basic example of Python For Loop:
Let us see through a simple example to understand how the For Loop works:
Output:
1 2 3 4 5 6
Explanation:
In the above example, we used a for loop with the range() function to print numbers ranging from 1 to 6.
Another Example: 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:
Enter number of rows: 5
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
Explanation:
The above code is an example of a Nested loop, where we have printed star patterns using a Nested For Loop.
Python 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:
We will now look at some examples of Python while loops:
Basic Example of While Loop
In this example, we will see how the While loop functions in Python.
Output:
0 Hello 1 Hello 2 Hello 3 Hello 4 Hello
Explanation:
In the above example, we have initialized a variable counter with the value 0. Then, we enter a while loop that executes the block of code as long as the counter is less than 5. Within the loop, we have printed the current value of the counter, followed by "Hello". After each iteration, the counter is incremented by 1. Once the counter reaches 5, the condition counter < 5 becomes False, causing the loop to terminate.
Another Example: 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 that runs when the condition becomes False.
There are several differences between the for loop and the while loop in Python. Some main differences are as follows:
| Feature | for Loop | while Loop |
|---|---|---|
| Definition | Iterates over a sequence (like a list, string, or range). | Repeats a block of code as long as a condition is True. |
| Usage | When the number of iterations is known or a sequence is available. | When the number of iterations is unknown or depends on a condition. |
| Syntax | for variable in iterable: # code block | while condition: # code block |
| Loop Control | Controlled by an iterable (e.g., list, tuple, range). | Controlled by a condition that must eventually become false. |
| Loop Variable | Automatically updates with each iteration. | Needs to be manually updated inside the loop. |
| Risk of Infinite Loop | Low, since it depends on an iterable. | Higher, if the condition is never made false. |
| Common Use Cases | Iterating over items in a list, string, dictionary, etc. | Waiting for user input, checking sensor data, or running until a condition. |
| Structure Simplicity | More concise when working with sequences. | More flexible for complex or dynamic conditions. |
| Termination | Ends automatically when the iterable is exhausted. | Ends only when the condition becomes false. |
We request you to subscribe our newsletter for upcoming updates.