At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

Python "for" Loops (Definite Iteration) Tutorial

  • By Aishwarya Damani
  • October 31, 2019
  • 1211 Views

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:

for i = 1 to 843
  // statement
  // statement

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(i = 0; i <= 8432; i++) {
// statement
// statement
}

For loop in python

In python web development, 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:

for i in <collection>
 // statement

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.

for i in range(5):
   print(i)
else: 
    print(“For loop ended, reached in else statement”)

OUTPUT

0
1
2
3
4
For loop ended, reached in else statement

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

for i in range(5):
   print(i)
   if(i==3): break
else: print(‘loop ended, reached else’)

OUTPUT

0
1
2
3

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.

for x in range(1,10):
    for y in range(x):
        print(x,end=' ')
    print()

OUTPUT

1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5 
6 6 6 6 6 6 
7 7 7 7 7 7 7 
8 8 8 8 8 8 8 8 
9 9 9 9 9 9 9 9 9 

Here are some of the important examples with explanation:

1. Simple for loop

for i in range(10):
    print(i)

OUTPUT

0
1
2
3
4
5
6
7
8
9

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.

list(range(5))

OUTPUT

[0, 1, 2, 3, 4]

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.

list(range(5, 10))

OUTPUT

[5, 6, 7, 8, 9]

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

list(range(5,50,5))

OUTPUT

[5, 10, 15, 20, 25, 30, 35, 40, 45]

2. For loop on lists:

for i in [1,2,3,4,5]:
   print(i)

OUTPUT

1
2
3
4
5

3. For loop on string:

for i in “Agira”:
    print(i)

OUTPUT

A
g
i
r
a

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

names = [“Java”, “Ruby”, “JavaScript”, “Dart”, “TypeScript”, “Python”]
for index in range(len(names)):
    print(names[index])

OUTPUT

Java
Ruby
JavaScript
Dart
TypeScript
Python

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?

 

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.