googleads
Python "for" Loops (Definite Iteration) | Python Web Development
preloder
Python Web Development

Python “for” Loops (Definite Iteration)

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

What is for loop?

For loop, as the name indicates is a looping statement which is popularly used in all the programming languages including python. It is also referred to as a definite iteration 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, for 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.

OUTPUT

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

OUTPUT

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.

OUTPUT

Here are some of the important examples with explanation:

1. Simple for loop

OUTPUT
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.

OUTPUT

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.

OUTPUT

(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.

OUTPUT

2. For loop on lists:

OUTPUT

3. For loop on string:

OUTPUT

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

OUTPUT

That’s all for ‘for – loop’… Keep looping 🙂

Moreover, we have got more interesting blogs on Python such as What’s New with Python 3.8? Latest Features and Updates and How To Convert Data Types in Python 3 and much more. Subscribe to us now to get the latest

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

Turn your vision to magnificent reality with
Our Web and Mobile Solutions

 

The following two tabs change content below.

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.

Latest posts by Aishwarya Damani (see all)

Leave a Reply

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

[contact-form-7 id="120788" title="Web Page Form"]

Schedule Your Call