Python for loop, with examples

A for loop is a control flow statement that allows us to execute a code block multiple times. It is used to iterate over a sequence (list, tuple, string, dictionary, etc.) and perform specific actions on each sequence element. This tutorial will discuss Python for loop and its various applications.

Syntax

The syntax of the for loop in Python is as follows:

for variable in sequence:
    # code to be executed

Here, the variable represents the current element of the sequence, and the sequence is the object we are iterating. The code inside the loop is executed once for each sequence element.

Examples

Iterating over a List

One of the most common applications of the for loop is to iterate over a list. We can use the for loop to perform certain operations on each list element. For example, let’s say we have a list of numbers, and we want to print each number:

numbers = [1, 2, 3, 4, 5]
for num in numbers:
    print(num)

This will output:

1
2
3
4
5

Iterating over a dictionary

We can also use the for loop to iterate over a dictionary. When we iterate over a dictionary, the variable represents the dictionary’s keys, and we can use it to access the corresponding values. For example, let’s say we have a dictionary of students and their grades, and we want to print each student’s name and grade:

grades = {"Alice": 90, "Bob": 80, "Charlie": 70}
for name in grades:
    print(name, grades[name])

This will output:

Alice 90
Bob 80
Charlie 70

Nested Loops

We can also use a for loop inside another for loop, which is called a nested loop. Nested loops are helpful when we need to iterate over multiple sequences simultaneously. For example, let’s say we have two lists, numbers and letters, and we want to print all possible combinations of the two:

numbers = [1, 2, 3]
letters = ['a', 'b', 'c']
for num in numbers:
    for letter in letters:
        print(num, letter)

This will output:

1 a
1 b
1 c
2 a
2 b
2 c
3 a
3 b
3 c

10 more examples using basic python for loop.

  • Using a for loop to iterate through a list of items and perform a specific action on each item
  • Reading data from a file using a for loop to iterate through each line of the file
  • Using a for loop to iterate through a range of numbers and perform a specific action on each number
  • Printing out a multiplication table using nested for loops
  • Creating a program that prints out the Fibonacci sequence using a for loop
  • Using a for loop to iterate through a dictionary and perform a specific action on each key-value pair
  • Creating a program that finds the factorial of a number using a for loop
  • Using a for loop to iterate through a string and perform a specific action on each character
  • Creating a program that checks if a number is prime using a for loop
  • Using a for loop to iterate through a list of items and remove items that meet a specific condition

Conclusion

The for loop is a powerful tool in Python that allows us to iterate over a sequence and perform certain operations on each element. It is widely used in various applications, such as iterating over lists, dictionaries, and nested loops. By understanding the syntax and applications of the for loop, we can write more efficient and effective Python code.