
I think for loop is the most commonly used statement in Python language. One of the fundamental concepts in Python is the for loop. In this article, we will explore some simple for-loop exercises in Python suitable for beginners.
Course for You: Learn Python in 100 days of coding
Basic for-Loop Syntax
A for-loop is a control structure that allows you to repeat a block of code for a specific number of times. In Python, for-loops are often used to iterate over lists, strings, data frames, etc. The basic syntax of a for loop looks like below:
# Basic syntax of for loop in python
for item in collection:
# Code to be repeated
In the above code, item
is a variable that takes on the values from collection
one at a time and the code block is executed for each value. Now let’s see some simple Python for loop example exercises with solution answers.
Exercise 1: Print Numbers from 1 to 10
Let’s start with a simple exercise. In this exercise, we will print numbers from 1 to 10 using a for-loop.
# python basic for loop exercises
for number in range(1, 11):
print(number)
1
2
3
4
5
6
7
8
9
10
In this example, range(1, 11)
generates a sequence of numbers from 1 to 10, and the for
loop prints each number in this range.
Exercise 2: Calculate the Sum of Numbers from 1 to 10
Now, let’s calculate the sum of numbers from 1 to 10 using a for-loop.
# Calculate sum of numbers using for loop in python
sum = 0
for number in range(1, 11):
sum += number
print("The sum of numbers from 1 to 10 is:", sum)
The sum of numbers from 1 to 10 is: 55
Exercise 3: Print Each Letter of a String
You can also use for-loops to iterate over characters in a string. Let’s print each letter of the word “Python”. The loop will go through each character in the string word
and print it.
# beginner loop exercises in python: Print Each Letter of a String
word = "Python"
for letter in word:
print(letter)
P
y
t
h
o
n
Exercise 4: Count the Number of Vowels in a String
In this exercise, we will count the number of vowels in a given string. We use an if
statement to check if a character is a vowel and increment the count if it is true.
# for loop exercises for python: Count the Number of Vowels in a String
text = "Hello World!"
vowel_count = 0
for char in text:
if char.lower() in "aeiou":
vowel_count += 1
print("The number of vowels in the text is:", vowel_count)
The number of vowels in the text is: 3
In our input string “Hello World!” the number of vowels is 3 (e, o, and o).
Exercise 5: Iterate Over a List using for loop
For-loops can also be used to iterate over lists. Let’s iterate over a list of fruits and print each fruit.
# exercises for loops python: List iteration
fruits = ["apple", "banana", "cherry", "date"]
for fruit in fruits:
print(fruit)
apple
banana
cherry
date
Exercise 6: Find the Largest Number in a List
For-loops can also help you find the largest number in a list. Let’s write a program to find the largest number in a list of integers.
# python exercises using loops: find largest number
numbers = [23, 45, 67, 12, 89, 54]
largest = numbers[0]
for num in numbers:
if num > largest:
largest = num
print("The largest number is:", largest)
The largest number is: 89
Exercise 7: Print Even Numbers using for loop
Now, in this exercise let’s print all the even numbers from 1 to 20 using a for-loop in Python. In this example, we use the modulo operator (%
) to check if a number is even (divisible by 2).
# loop exercises python: Print Even numbers
for number in range(1, 21):
if number % 2 == 0:
print(number)
2
4
6
8
10
12
14
16
18
20
Exercise 8: Print Multiplication Table
Let’s use a for-loop to print the multiplication table for a given number in Python. The below code will print the multiplication table for the number 7.
# simple loop exercises python: print multiplicatio table
num = 7
for i in range(1, 11):
result = num * i
print(f"{num} 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
Exercise 9: Search for an Element in a List
Now, let’s write a program to search for a specific element in a list. This exercise demonstrates how to use a for-loop to search for an element in a list.
# for loops exercises python: Search for a list element
numbers = [12, 45, 7, 32, 88, 19]
search_value = 32
if search_value in numbers:
print(f"{search_value} found in the list.")
else:
print(f"{search_value} not found in the list.")
32 found in the list.
Exercise 10: Sort a List of Dictionaries
You can sort a list of dictionaries based on a specific key within each dictionary using for loop.
# python loop exercises: Sort a lit of dictionaries
people = [
{"name": "Alice", "age": 30},
{"name": "Bob", "age": 25},
{"name": "Charlie", "age": 35}
]
# Sort by age
sorted_people = sorted(people, key=lambda x: x["age"])
for person in sorted_people:
print(f"{person['name']}, Age: {person['age']}")
Bob, Age: 25
Alice, Age: 30
Charlie, Age: 35
Exercise 11: Read and Process a Text File
This exercise involves file handling and demonstrates how to read and process a text file using a for-loop to display the first 10 lines. To run this code you need to have a .txt file inside your working directory.
# python for loop exercises: Read a txt file
file_path = "sample.txt"
with open(file_path, "r") as file:
lines = file.readlines()
line_count = len(lines)
print(f"Number of lines in '{file_path}': {line_count}")
print("First 10 lines:")
for i, line in enumerate(lines[:10], 1):
print(f"Line {i}: {line.strip()}")
Number of lines in 'sample.txt': 1
First 10 lines:
Line 1: Hello, this is a sample text file.
Exercise 12: Iterate Over Two Lists in Parallel
Let’s use the zip function to iterate over two lists in parallel and print corresponding elements.
# nested loop python exercises: Iterate Over Two Lists in Parallel
list1 = [1, 2, 3, 4, 5]
list2 = ['a', 'b', 'c', 'd', 'e']
for item1, item2 in zip(list1, list2):
print(f'Item from list1: {item1}, Item from list2: {item2}')
Item from list1: 1, Item from list2: a
Item from list1: 2, Item from list2: b
Item from list1: 3, Item from list2: c
Item from list1: 4, Item from list2: d
Item from list1: 5, Item from list2: e
Exercise 13: Nested Loop Exercises in Python
Nested loops are loops within loops. In this for loop exercises let’s use nested loops to print a square pattern of asterisks in Python.
# nested loop python exercises
size = 5
for i in range(size):
for j in range(size):
print("*", end=" ")
print()
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Exercise 14: Draw a Chessboard using for loop
This exercise demonstrates how to use nested loops to draw a chessboard pattern in Python.
# Advanced for loop exercises python: Draw a Chessboard
size = 8
for i in range(size):
for j in range(size):
if (i + j) % 2 == 0:
print("█", end=" ")
else:
print(" ", end=" ")
print()
█ █ █ █
█ █ █ █
█ █ █ █
█ █ █ █
█ █ █ █
█ █ █ █
█ █ █ █
█ █ █ █
Conclusion
In this article, I have added some simple Python loop example exercises with answers. These beginner loop exercises will help you to gain confidence in Python.
This is it for this article. If you have any comments or suggestions regarding this article, please please drop a comment below. If you want to learn Python quickly then this Udemy course is for you: Learn Python in 100 days of coding.
Similar Read:
- 15 Simple Python Programs for Practice with Solutions
- 12 Python Object Oriented Programming (OOP) Exercises
- 19 Python Programming Lists Practice Exercises
- 12 Python if-else Exercises for Beginners
- 11 Basic lambda Function Practice Exercises in Python
- 15 Python while Loop Exercises with Solutions for Beginners
- 12 Python Dictionaries Practice Exercises for Beginners

Hi there, I’m Anindya Naskar, Data Science Engineer. I created this website to show you what I believe is the best possible way to get your start in the field of Data Science.