For Loop in Python | Explained

Python is a trendy and powerful scripted language that targets the simplicity and readability of programs. In programming, code repetition is often necessary to complete a certain task or operation. In this regard, Python provides a variety of Looping functions, i.e., While, For, Do While, etc. This write-up will provide a complete overview of the Python “for loop” with multiple examples. The following contents are explained in this article:

So let’s get started!

What is a Python For loop?

The “for loop” is an inbuilt iterating function of Python. The “for loop” iterates over the sequence of any iterable object such as a string, tuple, list, etc. The “for loop” executes every element of the iterable object in series and operates on it. The syntax of for loop is given below:

for value in sequence:
    Statement-1
    . . .
    Statements-n

The syntax components are described as:

  • The “value” represents the elements of any iterable object.
  • The “sequence” is any variable list, tuple or string, etc in which the operation is repeated.
  • The body of “for loop” may comprise single or multiple code statements.
How to install Kali Linux in Window...

Let’s head over to a few examples to implement “for loop” in Python.

Example 1: Using for loop to Iterate Over the List

Python “for loop” iterates over the list elements and performs certain operations. In the example given below “for loop” is being iterated over a list to get the sum of all elements:

Code

# using for loop to iterate over the list

List_value = [16, 15, 13, 18, 14]
sum_var = 0
for val in Lits_value:
    sum_var = sum_var+val

print("The sum of the List is: ", sum_var)

In the above code:

  • The list variable named “List_value” is initialized.
  • Another variable named “sum_var” is initialized, which stores the value “0”.
  • The “for loop” iterates over the list numbers to calculate the sum. The addition of the variable is then stored in a variable named “sum_var”.
  • Lastly, the final sum of the list is shown by the “print()” function.

Output

In the above output, the sum of the list is “76”.

Example 2: Using for loop to Iterate Over the range() Function

The “for loop” statement, along with  “range()” function, iterates the elements. The “range()” function operated on any data at a specific number of times. The “for” loop iterates through “range()” function to generate the sequence using the specific range. The following code provides you the detailed understanding of “for loop” with range() function:

Code

# using for loop to iterate over the range function

value_1 = ['car', 'bike', 'ship']

# iterate over the list using index
for i in range(len(value_1)):
    print("I Love my", value_1[i])

In the above code:

  • The list variable named “value_1” is initialized.
  • The “for loop” iterates over the list named “value_1” using the “range()” function.
  • The “range()” function uses the index number of the list and performs the operation using the index number, i.e., the element “car” is placed on the index number value “0”.
  • The “print()” function prints the string “I Love my” with all the elements of the list variable named “value_1” one by one.

Note: Using the “range()” function with for loop can help us to perform operations on all the elements of the list individually or combined.

Output

The above output shows all the elements of the list “car, bike, ship” with the string ”I Love my”.

Example 3: Using for loop with if-else Function

The “for loop” with the “if-else” function can increase the functionality of the program, such as filtering out data with multiple conditions. Let’s understand this concept with an example of code given below:

Code

# using for loop with if-else function
value_1 = [1, 2, 3,14,5,16,7]

for value in value_1:
    if value%2==0:
        print('Even')
    else:
        print('Odd')

In the above code:

  • The list variable named “value_1” is initialized.
  • The “for loop” statement is used for iteration over the variables list.
  • The “if statement” defines the condition that if the element’s value divided by “2” is equal to zero, then “Even” is printed on output.
  • Else” statement executes when the condition does not satisfy.

Output

The first line of output shows that “1” is “Odd”. Similarly, each line shows the number is “Even” or “Odd”.

Example 4: Using Nested for loop

Nested for loop” means that there is a “for loop” initialized inside the “for loop”. The inner “for loop” executes on every iteration of the outer/main “for loop”. Let’s see its working via the following code:

Code

# using Nested for loop
Numbers = [1, 2, 3]
value_1 = ['car','bike','ship']
for num in Numbers:
    for val in value_1:
        print(num,val)

In the above code:

  • Two variables named “Numbers” and “value_1” are initialized.
  • Two “for loops” are used in the above example code.
  • One “for loop” is used to iterate over the elements of the Numbers variable, and the other “for loop” is a nested for loop iterating over the sequence of elements in the list variable named “value_1”.
  • When the main “for loop” execution is performed, then on each iteration of the main loop, the inner loop is executed 
  • The “nested for loop” prints all the numbers of the variable named “Numbers variable” one by one with the elements of the list variable named “value_1”.

Output

The above output shows that all the numbers of one variable are printed one by one with elements of another variable.

Conclusion

In Python, the “for loop” function iterates over the sequence of elements of different iterable objects like tuples, strings, and lists. The “for loop” is used with many different inbuilt functions such as “range() function”, “if else”, etc. Moreover, the “nested for loop” can also be utilized to serve some functionality with two loops. This post has briefly demonstrated the working and usage of “for loop” with multiple examples.