What is for loop in python with example [10 Examples]

A for loop is used to iterate over elements in a sequence (like a list, tuple, or string) or other iterable objects. It allows you to execute a block of code repeatedly for each item in the sequence.

Here’s an example of a for loop in Python:

fruits = ['apple', 'banana', 'cherry', 'grape']

for fruit in fruits:

In this example, we have a list called fruits that contains four elements: ‘apple’, ‘banana’, ‘cherry’, and ‘grape’. The for loop iterates over each item in the fruits list, and the variable fruit takes the value of each element one by one in each iteration. The print(fruit) statement inside the loop will output each fruit name on a separate line:



The loop continues until it has iterated over all the elements in the fruits list. For loops are commonly used for tasks like iterating through collections, processing elements, or performing repetitive actions on data.

Simple example of for loop in python 

Here are four more examples of for loops in Python:

  1. Summing elements in a list:
numbers = [1, 2, 3, 4, 5]
sum_result = 0

for num in numbers:
    sum_result += num

print("Sum of numbers:", sum_result)


Sum of numbers: 15
  1. Printing numbers in a range with a step:
for i in range(0, 10, 2):


  1. Finding the maximum element in a list:
numbers = [12, 45, 6, 27, 8, 33]
max_num = numbers[0]

for num in numbers:
    if num > max_num:
        max_num = num

print("Maximum number:", max_num)


Maximum number: 45
  1. Iterating over characters in a string:
message = "Hello, Python!"

for char in message:



In these examples, you can see how for loops are used to perform various tasks, such as calculating sums, iterating over ranges, finding maximum values, and processing characters in a string. For loops are versatile and can be applied to many different situations in Python programming.

Here are five more examples of for loops in Python:

  1. Counting even and odd numbers in a list:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_count = 0
odd_count = 0

for num in numbers:
    if num % 2 == 0:
        even_count += 1
        odd_count += 1

print("Even numbers:", even_count)
print("Odd numbers:", odd_count)


Even numbers: 5
Odd numbers: 5
  1. Printing the multiplication table:
number = 7

for i in range(1, 11):
    result = number * i
    print(f"{number} x {i} = {result}")


7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
  1. Printing characters with their ASCII values:
message = "Hello, Python!"

for char in message:
    ascii_value = ord(char)
    print(f"{char}: {ascii_value}")


H: 72
e: 101
l: 108
l: 108
o: 111
,: 44
 : 32
P: 80
y: 121
t: 116
h: 104
o: 111
n: 110
!: 33
  1. Iterating over a dictionary and printing key-value pairs:
student_scores = {"Alice": 85, "Bob": 92, "Charlie": 78, "David": 95}

for name, score in student_scores.items():
    print(f"{name} scored {score} marks.")


Alice scored 85 marks.
Bob scored 92 marks.
Charlie scored 78 marks.
David scored 95 marks.
  1. Reversing a list using a for loop:
numbers = [1, 2, 3, 4, 5]
reversed_numbers = []

for i in range(len(numbers) - 1, -1, -1):

print("Original list:", numbers)
print("Reversed list:", reversed_numbers)


Original list: [1, 2, 3, 4, 5]
Reversed list: [5, 4, 3, 2, 1]

In these additional examples, you can see how for loops are used for counting elements, generating multiplication tables, working with ASCII values, iterating through dictionaries, and reversing lists. For loops are an essential part of Python and are often used in various programming tasks.

What is the for in Python?

In Python, “for” is a keyword used to create a loop that iterates over a sequence of elements. It is used to execute a block of code repeatedly for each item in the sequence.

What is for () an example of in coding?

The phrase “for ()” is not commonly used in Python coding or programming in general. The correct syntax of a for loop in Python does not involve parentheses after “for.” If this is referring to a specific context or pattern, more context would be needed to provide an accurate answer.

Read More;

  • Yaryna Ostapchuk

    I am an enthusiastic learner and aspiring Python developer with expertise in Django and Flask. I pursued my education at Ivan Franko Lviv University, specializing in the Faculty of Physics. My skills encompass Python programming, backend development, and working with databases. I am well-versed in various computer software, including Ubuntu, Linux, MaximDL, LabView, C/C++, and Python, among others.

Leave a Comment