Python for Loop explained with examples | Tutorial
loader image
Python for Loops (Definite Iteration)
Python Web Development

Python “for” Loops (Definite Iteration) Tutorial

What is Python for loops?

Python For loop, as the name indicates, it is a looping statement that is popularly used by Python developers in all the programming languages. It is also referred to as a definite iteration loop

Here is the blog on how Python “for” loops work. Before getting in detail, you should start with the basics of for loop.

For loop in other languages:

Earlier in languages like BASIC, algol, and pascal, defining for loop is entirely different. It is defined as:

Languages like C, C++, java, etc, has 3 divisions:

The variable has to be initialized i.e, the starting point should be given so as to check where the loop needs to get started. Next, the condition given by the user should be satisfied, which would require the endpoint. So, to check where the loop needs to get stopped. And finally, comes the iteration. It is defined as:

For loop in python

In python web developmentfor loop is not defined in the above syntax. It has its own way of creating a definite for loop with a very easily understandable syntax. In python, for loop iterates over a collection of objects and not by specifying the numerical values or conditions that were seen in the above example. It is defined as:

The above syntax takes the value of the next object from the mentioned collection. The for loop of this type is arguably the mostly generalized and abstract. PHP and Perl are the languages which support syntax similar to python but they do it using a keyword “foreach” and not “for”. In python we use “for”.

Else statement in for loop

This is something different from all other languages, Python has an optional else option in for loop. You can add else at the end of for loop, once the loop gets over, it will enter inside the else statement and execute the else part.


Note – Else statement will not execute if you break the loop using ‘break’ keyword.


It will not go to the else statement as we break the loop manually by adding the break keyword.

Nested for loops in Python

As we already know nested loop is a loop inside a loop. Similar to other languages, Python also follows the same way. The only difference is the syntax. 

Let’s see one of the most common examples of pattern printing using nested for loop.


Here are some of the important examples with explanation:

1. Simple for loop

Now, What is the range() function?

Range function will generate a sequence of the given arguments. It also accepts multiple arguments. Let’s look at that with an example.

(A) One argument

Range with One argument will give you the sequence of given arguments starting from 0.


Note – we are using ‘list’ to convert the object of range into the object of lists for easy iterating format.

(B) Two arguments

Range with Two arguments will give you the sequence starting with the first argument value and ending with the second argument.


(C) Three arguments

Range with three arguments works as same as the above two arguments. The only difference is that it works as an interval between the sequence.


2. For loop on lists:


3. For loop on string:


4. For loop on list of strings.(using len() method)


That’s all for ‘for – loop’. Keep looping.!

Moreover, we have got more interesting blogs on Python such as Python use cases and Applications and How To Convert Data Types in Python 3 and much more. Subscribe to us now to get the latest updates from us.

Hire talented Python Developers to build your high-end web applications and websites with exceptional user experience. Get in touch with us right now.

Looking for a Tech partner to dominate the digital world?


The following two tabs change content below.
Aishwarya Damani

Aishwarya Damani

An aspiring adolescent started her career as Software Engineer Trainee and slowly she's mastering all the rising technologies like Nodejs, Express.js, React.js, MongoDB, Sails. On the other hand, a perfect decision maker who is passionate about coding. Wait! there's half a lie! Yeah, this crazy foodie has an intense long lasting affair on trying different foods & exploring places.
Aishwarya Damani

Latest posts by Aishwarya Damani (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *

Join our 30,000+ subscribers, never miss out anything on our latest blogs, tips, tutorials, updates & more.