Python for loop – A method to iterate sequence.


By using a for loop in Python, an execution flow can iterate a body or block of code a fixed number of times. The number of iterations depends on the size of the iterable object (such as a range, list, tuple, dictionary, or string) given while using the loop.

In this tutorial, we will learn how to use a for loop in Python with various examples.

What is for loop in Python?

The for loop starts with a line that starts with a keyword for and ends with a sequence. After this line, the loop body starts. No special marks are used for starting and ending the loop (e.g., in C programming, { } contains loop code block) code block. Indentation determines the body of the loop.

The following examples show the syntax of for loop.

for item in Iterable-Object:
	# Statement 1
	# Statement 2
	#.......
	# Statement N
# Statement Outside of loop body

What is a nested for loop?

In a nested loop, the body contains another loop. The number of levels of nested loops is unlimited. The following is the syntax for the most straightforward nested loop. Indentation is again used to distinguish between the scopes of the two loops.

for item_1 in Iterable-Object_1: # Outer Loop
	for item_2 in Iterable-Object_2: # Inner Loop
        # Statement 1 - Inner Loop
		# Statement 2 - Inner Loop
		#.......
		# Statement N - Inner Loop
		
	# Statement 1 - Outer Loop
	# Statement 2 - Outer Loop
	#.......
	# Statement N - Outer Loop
# Statement Outside of all loops body

Loop example using a list –

# Example Program to Iterate a List 
#Using for loop. Program

myList = [100, "Python", 3, "For Loop"];

for ele in myList:
	print(ele);
print("Done with for loop");
100
Python
3
For Loop
Done with for loop
  • Creates a list sequence of name myList and Initializes with four items.
  • The for loop traverses the sequence and holds each item in ele during traversal.
  • Prints the item till it reaches the end of the list.
  • After printing the last item, control comes outside the loop.

Loop for a more significant number of iterations?

In the above example, we have seen that a list is created. What if we want to execute a loop many times, say 50000000? Do we need to create a list that long? No, we have a built-in function range(). With range, we can create a sequence just by passing a number. The following example executes a for-loop ten times using the range() function.

# Example Program to Iterate 
#Using for loop with range() function. Program

for ele in range(10):
	print(ele);
print("Done with for loop");
0
1
2
3
4
5
6
7
8
9
Done with for loop
  • The range(number) function returns an object for an iterable sequence. The values in sequence start from zero and end with the number -1.
  • We will discuss Other variants for the range function in another Python tutorial.

One More Example – Calculate the Length of the string using a Python for-loop.

# Example Program to Iterate a string and calculate the length

myString = "Learn Python String";
len = 0;
for ele in myString:
	len = len + 1;
print("Length of string = ", len);
Done with for loop : Length of string = 19