With Examples, Demonstrate Python For Loop – Q&A

When iterating over a sequence of statements repeatedly, a loop is utilized. Three different loop types exist in Python: for, while, and do-while. The for loop will be covered in this manual, while the other two loops are covered in separate courses.

Python’s For loop syntax

for <variable> in <sequence>:  
   # body_of_loop that has set of statements
   # which requires repeated execution

Here, the word “variable” refers to a variable that is used to loop through a sequence. The following value from the sequence is taken on each iteration until the conclusion of the sequence is reached.

To understand how the for loop is used, let’s look at a few instances.

Example of a for loop in Python

The usage of a for loop to iterate through a list of numbers is demonstrated in the following example. Every number in the list is squared in the for loop’s body, and the result is displayed.

# Program to print squares of all numbers present in a list

# List of integer numbers
numbers = [1, 2, 4, 6, 11, 20]

# variable to store the square of each num temporary
sq = 0

# iterating over the given list
for val in numbers:
    # calculating square of each number
    sq = val * val
    # displaying the squares



Function spectrum ()

In the preceding example, a list was iterated over using a for loop. However, we can also iterate through the integers defined by range by using the range() method in a for loop ().

range(n) produces a range of whole numbers between 0 and (n-1).
For instance, the value of range(8) is [0, 1, 2, 3, 4, 5, 6, 7].

range(start, stop) produces a range of whole numbers starting at start and going all the way to stop-1.
For instance, range(5, 9) equals [5, 6, 7, 8].

range(start, stop, step size): Since the step size by default is 1, the numbers produced when it is not specified have a difference of 1. However, by supplying step size, we can get numbers with a step size difference.
For instance:
[1, 3, 5, 7, 9] is similar to range(1, 10, 2).

Let’s utilize the for loop’s range() function:

Example of a loop in Python using the function range()

Here, the sum of the first five natural numbers is calculated and shown using the range() method.

# Program to print the sum of first 5 natural numbers

# variable to store the sum
sum = 0

# iterating over natural numbers using range()
for val in range(1, 6):
    # calculating sum
    sum = sum + val

# displaying sum of first 5 natural numbers



Blocking the for loop with else

Unlike Java, Python allows us to associate the loop with an optional “else” section. Only when all iterations of the loop have been completed does the “else” block go into effect. Take this as an example:

for val in range(5):
	print("The loop has completed execution")


The loop has completed execution

Note: The else block only executes when the loop is finished.

Python nested for loop

Nested for loops are for loops that are included within other for loops. Let’s look at a nested for loop example.
for num1 in range(3):
	for num2 in range(10, 14):
		print(num1, ",", num2)


0 , 10
0 , 11
0 , 12
0 , 13
1 , 10
1 , 11
1 , 12
1 , 13
2 , 10
2 , 11
2 , 12
2 , 13

Related Articles

Leave a Reply

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

Back to top button