Member-only story
The Poetry of Python: Loops
Python is such a simple language, it looks like a poem to your computer. Loops are no exception.
This tutorial is a chapter from Python Grammar (my book) which you can Get PDF (7 books for price of 1) or Buy it on Amazon.
Loops are useful when you need to go through a list of values. This is sometimes called iterating. One of the most basics forms of a loop is the for loop. It’s present in many other languages. Let’s take a look at few examples of for loops first.
In contrast to Python, a standard for-loop in many languages looks like:
/* In JavaScript simplest for loop takes on this form */
for (statement; statement; statement) {
// do something repeatedly
}
However, Python’s syntax is much more simple.
for in loop
The most basic loop in Python is the for in
loop.
for-in loop syntax
for x in iterable: repeatable statement
For in loop requires an iterable object like a string or a list for example.
This example loops through each character in string name
: