The Poetry of Python: Loops

Ghost Together
7 min readMay 15, 2022
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:

name = "python"for element in name:
print(element, end='\n')



You can create an empty loop in Python.

To create a loop that does nothing you can use pass keyword:

for x in iterable:

This loop has number of iterations equal to the number of items in iterable value. In this case no statements will be executed because pass simply hands execution over to next iteration step.

By the way the following syntax is also correct:

for x in iterable: pass

Looping through lists

Let’s try something more meaningful.

First we will create a list of alphabet letters as a list:

alphabet = ["a", "b", "c"]
Ghost Together

Ghost Together @ is an alternative to Twitter. Sign up to meet other makers of things.