Python For Loops: Quick Answers and Examples

Learning about Python for loops

Much of the benefit we get from using computers is from programming them to do the same task multiple times in a row, which requires repeating the same block of code again and again.  This is where for each loops are useful in Python, or any other object-oriented programming (OOP) language. We will use for loop and for each loop interchangeably, as the Python for loop is always associated with some collection of items to which the each refers, and it is helpful to think about the items to be worked with. Officially, the Python documentation refers to the for loop as the “for statement.”

In this post, we’ll cover all of the basics about using loops in Python, specifically for loops, showing how they work and the different types of loops that can be used for various repetitive tasks.

What is a for loop?

Here is a technical definition of a loop: to iterate over items of any sequence such as a list or a string. In Python, iterative for-loops, or repeated executions of a block of code, are simply called a loop.  They are used for iterating over a dictionary, list, set, string, or tuple.

One thing you should make a distinction between is a while loop which is another primitive loop command within Python.  This command executes a set of statements as long as a condition is true.  For example, you may want to print something until a count reaches a specific number. This type of iteration is conditional and indefinite.

It will be easier to understand what a loop is, and where it will be beneficial after you have read through the examples given here in the next section:

Basic usage of for loops in Python

Often the best way to understand a language feature is to see it in action, and to have a template to refer back to. So, without further ado, here are a few examples. Feel free to get some practice by trying these with your Python interpreter, and optionally stepping through them with a debugger to see what the for loop is applied to at each step.

Looping through a string

We can refer to the elements of a string object, because it is iterable: a string consists of a sequence of characters, and that sequence is the finite collection of items we can supply to the for keyword.

Imagine we need to learn how many times a letter occurs in a word, or how many times a word occurs in a paragraph.  For example, if we need to figure out how many times “i” occurs in the word “Mississippi” –

>>> word = 'Mississippi'
>>> count = 0
>>> for letter in word:
>>>     if letter == 'i':
>>>         count = count + 1
>>> print(count)


Note: there are easier ways to count letters within a word through built-in tools such as ‘Mississippi’.count(‘i’)

Note the basic format here:

for <item> in <iterable>: 
	<Work to be done to item>

Any other statements within the for loop are followed by additional indentation, as in the if statement above. Also note that while the iterable must refer to (or be a declaration of) of an actual iterable, the item variable name can be whatever you want, as the contents change from one iteration to the next.

It is a good practice to name this something which clearly describes the type of element in the iterable. If brevity is desired, it is also acceptable to use a single letter, also preferably indicating the type of the object on which work is performed.

Back to Top