When to use "while" or "for" in Python
Yes, there is a huge difference between while and for.
The for statement iterates through a collection or iterable object or generator function.
The while statement simply loops until a condition is False.
It isn't preference. It's a question of what your data structures are.
Often, we represent the values we want to process as a range
(an actual list), or xrange
(which generates the values) (Edit: In Python 3, range
is now a generator and behaves like the old xrange
function. xrange
has been removed from Python 3). This gives us a data structure tailor-made for the for statement.
Generally, however, we have a ready-made collection: a set, tuple, list, map or even a string is already an iterable collection, so we simply use a for loop.
In a few cases, we might want some functional-programming processing done for us, in which case we can apply that transformation as part of iteration. The sorted
and enumerate
functions apply a transformation on an iterable that fits naturally with the for statement.
If you don't have a tidy data structure to iterate through, or you don't have a generator function that drives your processing, you must use while.
The for
is the more pythonic choice for iterating a list since it is simpler and easier to read.
For example this:
for i in range(11):
print i
is much simpler and easier to read than this:
i = 0
while i <= 10:
print i
i = i + 1
for loops is used when you have definite itteration (the number of iterations is known).
Example of use:
- Iterate through a loop with definite range:
for i in range(23):
. - Iterate through collections(string, list, set, tuple, dictionary):
for book in books:
.
while loop is an indefinite itteration that is used when a loop repeats unkown number of times and end when some condition is met.
Note that in case of while loop the indented body of the loop should modify at least one variable in the test condition else the result is infinite loop.
Example of use:
The execution of the block of code require that the user enter specified input:
while input == specified_input:
.When you have a condition with comparison operators:
while count < limit and stop != False:
.
Refrerences: For Loops Vs. While Loops, Udacity Data Science, Python.org.
while
is useful in scenarios where the break condition doesn't logically depend on any kind of sequence. For example, consider unpredictable interactions:
while user_is_sleeping():
wait()
Of course, you could write an appropriate iterator to encapsulate that action and make it accessible via for
– but how would that serve readability?¹
In all other cases in Python, use for
(or an appropriate higher-order function which encapsulate the loop).
¹ assuming the user_is_sleeping
function returns False
when false, the example code could be rewritten as the following for
loop:
for _ in iter(user_is_sleeping, False):
wait()