21
NovDifference between For Loop and While Loop in Python
Difference Between for loop and while loop in Python: An Overview
Difference between For and While loops is easy to understand if you know their usage and applicationin aPython program. In this Python Tutorial, we will try to understand topics likeWhat is Python for loop?,What is Python while loop?, and the major differences between for loop and while loop in Python. You can explore more about different other concepts of python programming by enrolling in our Python Certification Training.
Read More: Top 50 Python Interview Questions and Answers
What is Python for loop?
Flowchart of Python for loop:
Python for loop Syntax
for item in iterable:
# Code block
- The variable 'item' will take each value from the specified sequence..
- The code block under the for loop will execute for each value.
Example of Python for loop
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
Explanation:
Output
apple
banana
cherry
What is Python while loop?
Python while loop is the control flow statement that repeats a block of code till the time a specified condition is 'True'. It keeps the iteration going until the condition is 'True' and as soon as the condition is 'False', the while loop in Python is terminated and the next line of code is executed which is just ahead of it.
Flowchart of Python while loop:
Python while loop Syntax
The while loop syntax in Python looks like this:
while condition:
# Code block
- The while loop will evaluate the condition and execute the code block if it is 'True'.
- Once the condition becomes 'False', the loop will terminate itself.
Read More: Python Developer Salary
Example of Python while loop
# Initialize a variable to store the current number
number = 1
# Start the while loop
while number <= 5:
# Print the current number
print(number)
# Increment the number for the next iteration
number += 1
Explanation:
Output
1
2
3
4
5
Difference Between For Loop and While Loop in Python
Aspect | For Loop | While Loop |
Syntax | for item in iterable: | while condition: |
Iteration behavior | It iterates through the specified sequence by itself. | It depends on the condition, whether it is 'True' or 'False'. |
Number of iterations | The number of iterations is known or finite. | The number of iterations is not known. |
Execution | Execution depends on the specific number of iterations. | It repeats the execution based on the specified condition. |
Termination | It will terminate once all elements have gone through iteration. | It may result in an infinite loop if the condition never evaluates to 'False'. |
Efficiency | It is considered more efficient. | It is less efficient as compared to the other. |
Error Potential | It iterates through a sequence, hence, less prone to infinite loops. | It may lead to frequent infinite loops if conditions are not managed carefully. |
When to use For Loop and While Loop in Python?
For Loop
- When there is a need of iteration over elements that are in sequence like lists, tuples, strings or dictionaries.
- Use for loops when a known number or a finite number of iteration is required.
- Another situation when you want that each element of a sequence should pass through iteration one by one.
- for loops can also be used when a task requires a sequential access of elements of a sequence.
While Loop
- When there is a need to repeat execution of a block of code until a specified condition becomes False.
- Use while loops when the number of iteration is not determined.
- while loops are can be used when there is a need to repeat execution based on a condition.
- When dynamic adaptation is required based upon condition that may change.
What happens in the Absence of Condition
For Loop
# This for loop won't execute any iterations since there's no iterable to iterate over
for i in range(): # range() without arguments doesn't generate any values
print("This loop won't execute any iterations!")
Output
(no output)
While Loop
# This while loop will run infinitely since there's no condition to stop it
while True:
print("This loop will run forever!")
Output
This loop will run forever!
This loop will run forever!
This loop will run forever!
...
Summary
FAQs
while condition: # Code block to execute while condition is True