Python

Python for loop

Loops are very crucial and essential components of any programming language. Loops are used for various purposes, such as executing the same block of code and traversing the containers. Like other programming languages, Python also provides loops, i.e., while loop and for loop.

The for loop in Python iterates over the sequence (list, tuples, dictionaries, etc.) and traverse it. For loop is beneficial for performing the iterative tasks. We can also run the same block of code multiple times by using the for loop.

This article will explain the for loop with examples.

Syntax

The for loop is declared by using the for keyword. The syntax of the for loop is as follows:

for iterator_variable in sequence:
statement(s) or body of for loop

The iterator_variable is used to iterate through the sequence. The value of the item is taken from the sequence, and the operation is performed. The for loop doesn’t terminate unless the last item in the sequence is traversed. The indentation is used to separate the body of for loop from its declaration.

Now, let’s see the examples of for loops in Python.

Examples 1: Printing the items of the list

Let’s declare a list and use the for loop to print the list items.

#declaring a list of animals
animal= ["cow","dog","cat","camel","lion"]
#declaring a for loop
#x is the iterator variable
for x in animal:
     #printing each item of the list
     print(x)

Output

Example 2: Calculating the sum of list items

Now, let’s write a program and calculate the sum of all the items of a list.

#declaring a list of numbers
mylist=[1,2,3,4,5,6,7,8,9,10]
#declaring a variable to store sum value
sum=0
#declaring the for loop
for x in mylist:
    sum=sum+x
print("The sum is: ",sum)

Output

Example 3: Traversing the string using for loop

In Python, we can also iterate the string using for loop. Let’s see an example of this.

#declaring a website variable
website="linuxhint"
#declaring a for loop
for x in website:
     print(x)

Output

Python for loop with else statement

Python allows us to use else statement in combination with the loops. The else statement will be executed if the loop is terminated or the list is iterated. Let’s see an example of this.

#declaring a website variable
website="linuxhint"
#declaring a for loop
for x in website:
     print(x)
#declaring an else statement
else:
     print("Executing the else statement")
     print("The end of for loop")

Output

Using break statement with the for loop

The break statement is used to control the iteration of for loop. The break statement stops the iteration of for loop after the particular iteration. It also terminates the for loop when a test condition is true. Let’s see an example of this. In the given example, the for loop is terminated when the value of the iterator is equal to the cat.

#declaring a list of animals
animal= ["cow","dog","cat","camel","lion"]
#declaring a for loop
#x is the iterator variable
for x in animal:
     #printing each item of the list
     print(x)
     if (x=="cat"):
          #using the break statement
          break

Output

Using continue statement with the for loop

The continue statement is also used to control the iteration of for loop. The continue statement can be used to skip the current iteration, while the for loop continues from the next iteration. The continue statement skip the iteration of the for loop when a test condition is true. Let’s see an example of this.

#declaring a list of animals
animal= ["cow","dog","cat","camel","lion"]
#declaring a for loop
#x is the iterator variable
for x in animal:
     #printing each item of the list
     if (x=="cat"):
          #using the continue statement
          continue
     print(x)

Output

In the given example, the current iteration is skipped when the value of the iterator is equal to the cat.

Using range() function in for loop

The range() function generates the numbers in sequence. We can specify the start, stop, and step size value within the range function. If the step size value is not defined, then it is 1 by default. The range() function is also used to access the indexes of the declared sequence. Let’s just take a look at the examples of the range function. We are writing the simplest program, which uses the range function to print the number 10. The range() function prints the number from 0 to 9.

#using the range function with the for loop
for num in range(10):
     #printing the value of num
     print(num)

Output

Now, let’s use start, stop, and step size value with range() function.

#using the range function with the for loop
#the start value is 1, the stop value is 30, and the step value is 3.
for num in range(1,30,3):
     #printing the value of num
     print(num)

Output

The range() function is also used to the get the indexes of the sequence. Let’s see an example of this where the len() function is used to return the list’s length.

#declaring a list of animals
animal= ["cow","dog","cat","camel","lion"]
#declaring a for loop
#x is the iterator variable
#getting the length of animal list by using the len() function
for x in range(len(animal)):
     #printing each item of the list
     print(animal[x])

Output

Conclusion

This article explains the use of for loop in Python with the help of simple examples. The for loop is used to iterate the container and access the items of the container. This article will be beneficial for beginners.

About the author

Kamran Sattar Awaisi

Kamran Sattar Awaisi

I am a software engineer and a research scholar. I like to write article and make tutorial on various IT topics including Python, Cloud Computing, Fog Computing and Deep Learning. I love to use Linux based operating systems.