bing
Flat 10% & upto 50% off + 10% Cashback + Free additional Courses. Hurry up
×
UPTO
50%
OFF!
Intellipaat
Intellipaat
  • Live Instructor-led Classes
  • Expert Education
  • 24*7 Support
  • Flexible Schedule

Control Flow Statements – For Loop

For loops in Python, just like any other language are used to repeat a block of code for a fixed number of times. For loop is yet another control flow statement since the control of the program is continuously transferred to the beginning of the for loop to execute the body of for loop for a fixed number of times. While other languages contain conditions and increment expression in the syntax of for loop, in python, the iteration and incrementing value is controlled by generating a sequence. In this module, we will learn about the for loops in Python. Following is the list of all the topics that we will cover this module, in case you need to jump to a specific one.

Learn Python in 16 hrs from experts

 

For loops in Python

For loops in Python are used for sequential iterations for a certain number of times, that is, the length of the sequence. Iterations on sequences in Python is called traversal.

Syntax of the for loop in Python:

for a in sequence:
body of for

The following flowchart explains the working of for loop in Python.

For loop
As depicted by the flowchart, the loop will continue until the last item in the sequence is reached. The body of the for loop, like the body of the while loop, is indented from the rest of the code in the program.

square = 1
numbers_list = [1,2,3,4,5,6,7]
for i in numbers_list:
square = i*i
print(“The square of”, i, “is”, square)

Output:

The square of 1 is 1
The square of 2 is 4
The square of 3 is 9
The square of 4 is 16
The square of 5 is 25
The square of 6 is 36
The square of 7 is 49

In the above example, we are calculating the squares of all the elements present in the list of numbers that we have created, named numbers_list, using a for loop. The variable i iterates from the beginning to the end of the list and when the program enters the body of the loop, square of each element is calculated using the variable i and stored in a variable named square.

Wish to Learn Python? Click Here

The range() function

In python, we can specify a particular range using an inbuilt function named range(), to iterate the loop a specified number of times through that range.

Example:

range(10)

Note that the range here does not start from 1 to 10, the range starts from 0 to 9 (10 numbers).
Specifying the start and stop points in range() function.
Example:

range(3,7)

Here in this example, the starting point is 3 and the ending point is 7, So the list will be generated from 3 to 6 ( 4 numbers).
Using for loops with range() function
Example:
We can simply use for loop with range() function as shown in the example below

For i in range(2,10):
print(i)

Output:

2
3
4
5
6
7
8
9

By default the increment in range() function when used with loops is set to 1, however we can change or specify a particular increment by including a third parameter in range() function, as illustrated in the following example:

For i in range(2,10,2)
print(i)

Output:

2
4
6
8
Become Python Certified in 16 hrs.
CLICK HERE

Loop interruptions

1. Break statement

Just as in while loops, for loops can also be prematurely terminated using the break statement. The break statement will immediately terminate the execution of the loop and transfer the control of the program to the end of the loop.
Example:

# creating a list of numbers
number_list = [2,3,4,5,6,7,8]
for i in number_list:
print(i)
if i == 5:
break

Output:

2
3
4
5

2. Continue Statement

Just as in while loop, continue statement can also be used with for loops to terminate the ongoing iteration and transfer the control to the beginning of the loop to continue the next iteration.

number_list = [2,3,4,5,6,7,8]
for i in number_list:
if i == 5:
continue
print(i)

Output:

2
3
4
6
7
8

Note that the fifth iteration was interrupted as we have used continue statement when the value of i is 5. Therefore, the control was passed to the beginning of the loop and program starting executing sixth iteration and did not print the value of i when it was 5.

Else in for loop

For loop in Python can have an optional else block. The else block will be executed only when all the iterations are completed. When break is used in for loop to terminate the loop before all the iterations are completed, the else block is ignored.
Example:

for i in range(1,6):
print(i)
else:
print(” All iterations completed”)

Output:

1
2
3
4
5
All iterations completed

Nested for Loop

As the name suggests, Nested loops are the loops that are nested inside an existing loop. That means that the nested loops are the body of another loop.
Example:

for i in range(1,9,2):
for j in range(i):
print( i, end = ‘ ‘)
print()

Output:

1
3 3 3
5 5 5 5 5
7 7 7 7 7 7 7

In the above example, we have used nested loops to print a number pyramid pattern. The outer loop is used to handle the number of rows in the pattern and the inner loop, or the nested loop is used to handle the number of columns in pattern. Note that we have also used the third parameter in range() function and set the increment to two, that is why the number in every column in incremented by two.
With this we come to an end of this module, the next module will highlight Python functions. See you there!

Next, check out Intellipaat’s Python Training to excel your career to the new heights. Also check our Python interview questions, which will help a lot to crack any Python Interview.

Previous Next

Download Interview Questions asked by top MNCs in 2019?

"0 Responses on Python For Loops"

Leave a Message

100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.
top

Sales Offer

Sign Up or Login to view the Free Python For Loops.