Many coders who are just starting with Python struggle with a thorough understanding of Python loops:
” I struggle a lot with for loops, well all types of loops! “ — Leonardo, Finxter “Coffee Break Python” Email Subscriber
Every time you want to repeat a certain behavior, you use loops. For example, if you want to print the first ten numbers to the shell, you’ll use a for loop:
Python For Loop
Here’s an example of the for loop:
for i in range(10): print(i) ''' 0 1 2 3 4 5 6 7 8 9 '''
The general formula of the for loop is:
for <loop variable> in <iterable>: <loop body>
So you define a loop variable (in our example i
) that takes on all values in an iterable (in our example range(i)
) and executes the loop body for each selection of the loop variable (in our example print(i)
).
These three selections (loop variable, iterable, and loop body) define your unique for loop. Here’s another example:
for x in ["John", "Mary", "Alice"]: print(x + " Doe")
What would be the output of this code snippet? See answer below! (*)
However, as all iterables are finite, there’s no way of executing a for loop a variable number of time! But what if you want to repeat a certain behavior until a condition is met?
Python While Loop
In this case, you’ll use the while loop that is more powerful because you can execute the loop body forever.
For example, a webserver internally executes a while loop that never stops (the server executes forever). Each time a new user requests the web page, the webserver serves the page and waits for the next user.
Here’s an example of a while loop that executes forever:
# Your fancy web app while True: print("<html> ... </html>") ''' <html> ... </html> <html> ... </html> <html> ... </html> <html> ... </html> ... '''
The while loop executes the loop body as long as the Boolean condition directly after the while statement evaluates to True. In the example, this is always the case. Therefore, the program executes forever until you terminate it forcefully.
But you can also use more complicated while conditions as shown in the next example:
# Puzzle x = "hello world" while not len(x) == 5: x = x[1:] print(x)
What’s the output of this code puzzle? Find the solution below(**).
π Recommended Tutorial: How to Emulate a Do-While Loop in Python?
Summary
In this short tutorial, you learned about both the for and the while loop. If you want to dive deep into Python, you should get yourself a book and/or a thorough Python course.
In my best-selling Python textbooks “Coffee Break Python” and “Coffee Break Python Workbook”, I give you everything you need to know to push you to advanced intermediate level. You just solve the dozens of Python puzzles in the books and your skill level with increase rapidly. Check them out!
(*)Here’s the output of the code snippet:
for x in ["John", "Mary", "Alice"]: print(x + " Doe") ''' John Doe Mary Doe Alice Doe '''
(**) Here’s the output of the second code snippet:
x = "hello world" while not len(x) == 5: x = x[1:] print(x) # world