Explore programming tutorials, exercises, quizzes, and solutions!
Python Loops (for, while) Exercises
1/20
You’re learning how to repeat actions in Python using loops. Python provides both for and while loops to help perform repetitive tasks, such as iterating over a list or running a block of code until a condition is met.
Consider the following scenario:
numbers = [3, 6, 9]
for num in numbers:
print(num * 2)
What does this code demonstrate about how loops work in Python?
Python’s for loop is designed to iterate over any iterable object, including lists, strings, tuples, sets, and dictionaries.
In this example:
numbers = [3, 6, 9]
for num in numbers:
print(num * 2)
The for loop goes through each value in the list numbers and prints double its value.
This highlights the readability and flexibility of Python loops — you don’t need to manage indices manually unless necessary.
Unlike some other languages, Python’s for loop is not limited to ranges and is often used for direct iteration over collections.
How many times will this loop run?
for i in range(3):
print(i)
range(3) generates the numbers 0, 1, and 2. So the loop runs 3 times, printing those values.
The loop starts at 1 and runs until count is greater than 3. It prints 1, 2, and 3, then exits.
Which keyword is used to skip the current iteration of a loop?
The continue statement skips the current iteration and moves to the next cycle of the loop.
What will be printed by this code?
for char in "hi":
print(char)
The for loop iterates over each character in the string "hi", so it prints 'h' and 'i' on separate lines.
How many times will the following loop execute?
i = 10
while i > 0:
i -= 2
print(i)
The loop starts with i = 10 and subtracts 2 each time. It prints values 8, 6, 4, 2, and 0, then exits when i becomes 0.
What is the output of the following code?
for i in range(2, 10, 3):
print(i, end=" ")
range(2, 10, 3) starts at 2 and adds 3 each time: 2, 5, 8. It stops before reaching 10.
What is the output of this code?
for i in range(3):
for j in range(2):
print(i, j)
This is a nested loop. For every i (0 to 2), j loops from 0 to 1. This results in 6 pairs total being printed.
Which statement will stop a loop completely?
The break statement terminates the loop immediately. pass is a no-op, and continue skips to the next iteration.
What will be printed by this code?
x = 0
while x < 3:
print("Hello")
x += 1
The loop runs while x is less than 3. It prints "Hello" three times before x reaches 3 and the loop stops.
What is the output of the following nested loop?
for i in range(1, 4):
for j in range(i):
print('*', end='')
print()
The outer loop runs 3 times (i from 1 to 3). For each i, the inner loop prints '*' i times on the same line. So we get 1, 2, then 3 stars on separate lines.
Which of the following best describes the use of an else clause with a for loop in Python?
The else clause in a loop executes only if the loop finishes normally (without hitting a break statement). It’s useful for search-type problems.
What will this code output?
i = 0
while i < 5:
i += 1
if i == 3:
continue
print(i)
When i is 3, the continue statement skips the print. So 3 is not printed, but other values (1, 2, 4, 5) are.
Which loop will execute at least once even if the condition is false initially (in other languages)?
Though Python does not support do-while directly, in other languages, do-while guarantees at least one execution since the condition is checked after the loop body.
What is the value of total after this loop?
total = 0
for i in range(1, 5):
if i % 2 == 0:
continue
total += i
The loop skips even numbers (2 and 4), so only 1 and 3 are added to total. Result: 1 + 3 = 4.
What will the following code output?
for i in range(3):
print(i)
i += 1
The i += 1 inside the loop does not affect the loop counter of the for loop, as Python uses the iterator returned by range(). So the loop prints 0, 1, and 2.
What is the output of this code?
count = 0
while count < 5:
if count == 2:
break
print(count)
count += 1
The loop prints 0 and 1. When count becomes 2, the condition triggers break, exiting the loop before printing 2.
How many times will this loop run?
x = 0
while x != 10:
x += 0.1
Due to floating-point precision errors, x may never exactly equal 10. So the loop may never terminate, resulting in an infinite loop.
What will be the output of the following code?
for i in range(3):
print("Outer:", i)
for j in range(2):
break
else:
print("Inner loop completed")
The inner for loop breaks on its first iteration every time, so the else clause of the inner loop never executes. Only "Outer" lines are printed.
Which of the following is a correct way to create an infinite loop using a for loop?
iter(int, 1) creates an iterator that will never return 1 (int() returns 0), so the loop never stops — effectively an infinite loop.
Practicing Python Loops (for, while)? Don’t forget to test yourself later in
our
Python Quiz.
About This Exercise: Python – Loops (for, while)
Welcome to the Python Loops exercises — a targeted set of coding problems designed to help you master repetitive logic using Python's for and while loops. These exercises are perfect whether you're just starting out with loops or looking to deepen your understanding of iteration in real programming scenarios. This section is a key step in building solid programming logic and algorithmic thinking.
Loops are a fundamental part of Python and are essential for performing repetitive tasks, iterating over sequences, and managing flow control in automation scripts, data processing, and algorithmic problem-solving. In this section, you’ll explore both for loops — ideal for iterating over collections like lists, tuples, and strings — and while loops — which give you greater control when the number of iterations isn’t known in advance.
These exercises will help you understand when to use each loop type and how to avoid common mistakes such as infinite loops, off-by-one errors, or unnecessary complexity. You’ll practice using loop control statements like break, continue, and else clauses, and learn how to write efficient loops that are both readable and performant.
Every exercise is crafted to simulate real-world coding challenges, from basic iterations to nested loops, and even loop-based logic used in data analysis or web scraping tasks. By working through them, you'll become comfortable writing and debugging loops that form the backbone of many Python scripts and applications.
As you progress, be sure to explore related topics like conditionals, functions, and data structures, all of which interact closely with loop constructs. And don’t forget to take our interactive quizzes on Python loops to reinforce your learning and identify areas for improvement.
Dive into the Python Loops exercises now to strengthen your command over repetitive logic. With consistent practice, you’ll gain the confidence to tackle any iteration-based problem efficiently, whether you're working on automation, data science, or core software development.