Skip to main content
Python

For Loop in Python Explained With Practical Examples

Learn the concept of for loop in Python with these practical examples.

Ankush Das

Warp Terminal

If you are just getting started to learn Python, you must be in search of something to explore for loop in Python.

Of course, our list of free python resources should help you learn about it quickly.

In either case, we shall help you learn more about the ‘for‘ loop in python using a couple of important examples.

For loop in Python

Python For Loop

If you know any other programming languages, chances are – you already know what it does.

A for loop in Python is a statement that helps you iterate a list, tuple, string, or any kind of sequence.

Essentially, the for loop is only used over a sequence and its use-cases will vary depending on what you want to achieve in your program.

A Few Key Points Before You Start Using For Loop

You will come across a few things while using the for loop which might be confusing – so, I shall mention a few things before getting started with for loop in Python.

What Is A Sequence?

A sequence is essentially a collection of similar objects, it might be a data set, a list of numbers, or a list of strings.

Membership Tests / Membership Operators

You will notice the use of in operator in Python’s for loop when I tell you how to use it in the later section of this article.

This lets you test a condition of whether something exists (or belongs to) the sequence (list or tuple) that we are working with.

It will return true until it finds a value in the sequence, else it will return false.

The range() function

To work with a range of numbers, you might need to use the range() function.

For example, when you write range (3,10), it will consider numbers starting from 3 till 9, excluding 10.

Suppose, you pass a single argument like range (3). It will consider numbers from 0 to 2 excluding 3 i.e 0,1,2.

By default, the range increments numbers by 1. However, if you want to explicitly specify the increment, you can write:

range (3,10,2)

Here, the third argument considers the range from 3-10 while incrementing numbers by 2. In this case, our list will be: 3,5,7,9.

Now, you are ready to get started learning for loops in Python.

Python for loop examples

I shall show you some examples that you can practice for yourself to know more.

1. Printing a range of numbers in Python

A simple example where you use for loop to print numbers from 0 to 3 is:

for numbers in range (4):
    print(numbers)

Here, numbers is a variable and you can specify any valid variable name.

Attention!

You will notice 4 spaces before the print function because Python needs an indentation. When you use whitespaces, it will specify the level of the code. For instance, here, the print function belongs inside the for loop and if you add another statement after that without whitespaces, it will be outside the for loop. You can learn more about Indentation in the official Python documentation.

2. Print each item from a list in Python

Suppose you have a list that names some Linux distributions:

distro = ["ubuntu", "mint", "elementary"]

If you want to print the items of the list distro, here’s what you have to write:

distro = ["ubuntu", "mint", "elementary"]
for xyz in distro:
    print (xyz)

The output to this should look like:

ubuntu
mint
elementary

3. Printing each letter of a string in Python

With for loop, you can easily print all the letters in a string separately, here’s how to do that:

for xyz in "ubuntu":
    print(xyz)

The output will be:

u
b
u
n
t
u

4. Using the break statement to stop the loop

Along with a for loop, you can specify a condition where the loop stops working using a break statement.

For instance, the code is:

distro = ["ubuntu", "mint", "elementary"]
for xyz in distro:
    print (xyz)
    if xyz == "mint":
        break

As you can observe, we have also used the if statement here.

So, here, the for loop will print till it encounters mint from the list. After it prints the second item from the list, it will match the if condition and the break statement will kick in to stop the for loop.

Similarly, you can use the break statement as per your requirement stop the loop anywhere you want.

5. Using the continue statement to continue the loop

With the break statement, you can stop the loop. But, with the continue statement, you will go back to the loop again (continue the loop – literally).

For instance, this is the code:

distro = ["ubuntu", "mint", "elementary"]
for xyz in distro:
    if xyz == "mint"
     continue
    print (xyz)

Here, it prints the elements but skips the print statement and returns to the loop again when it encounters “mint“.

For clarity, here’s what the output will be:

ubuntu
elementary

6. Using nested for loops in Python

Using multiple for loops (one or more) inside a for loop is known as a nested for loop.

distro = ["ubuntu", "mint", "elementary"]
linux = ["secure", "superior"]
for x in distro:
   for y in linux:
     print (x,y)

Here, we are just printing two sequences together using a nested for loop. The output will look like:

ubuntu secure
ubuntu superior
mint secure
mint superior
elementary secure
elementary superior

7. Using else statement with for loop

Just like we used if condition statements above, you can also use else statement in for loop. Here’s an example:

for numbers in range (4):
    print(numbers)
else:
    print("Good Job")

The output for this will be:

0
1
2
3
Good Job

8. Using pass statement with for loop

In case you do not have any conditions or statements inside a for loop, you can simply add a pass statement to avoid any error.

for numbers in range (4):
    pass

Wrapping Up

These were some of the simplest examples where you can use the for loop. Potentially, you can do a lot more things with it depending on what you want to achieve in your program.

I shall be covering similar articles to help you learn Python with examples. Until then, you can refer to my list of free python resources to get a head start.

Did you find this resource useful? Let me know your thoughts in the comments below.

Ankush Das