Difference Between For Loop and While Loop in Python2 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:
Examples on Python 'for' LoopWe 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. ExampleExecute NowOutput: 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' LoopWe will now take a look at an example to understand the working of the 'for' loop with the range() function. ExampleExecute NowOutput: 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:
Examples on Python 'while' LoopWe 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. ExampleExecute NowOutput: 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 ExampleExecute NowOutput: 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 LoopThe following are key differences between Lists and Tuples in Python:
Difference between For Loop and While Loop in python FAQs1. 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 |
We request you to subscribe our newsletter for upcoming updates.