Which code snippet will print "Done" only if the loop completes fully without hitting a break?
In Python, a for...else block runs the else part only if the loop is not terminated by break.
Option 2:
Loop runs from 0 → 4, printing all values.
No break → else executes, printing "Done".
Option 1: Has a break at i == 2, so "Done" never prints.
Option 3: Breaks at i == 4, so "Done" is skipped.
Option 4: Always prints "Done", but not using for...else.
Thus, the correct snippet is Option 2.
What will be the output of the following code?
i = 0
while i < 3:
i += 1
if i == 2:
break
else:
print("Completed")
print(i)
while i < 3: runs while i = 0 → 2.
i += 1 increments i each iteration.
When i == 2, break executes → loop exits immediately.
The else block does NOT execute because loop was terminated with break.
print(i) prints 2.
Step-by-step:
i = 0 → i = 1 → not 2 → continue
i = 1 → i = 2 → equals 2 → break → exit loop
else skipped → print(i) → 2
Quick Recap of Python Loops (for, while) Concepts
If you are not clear on the concepts of Loops (for, while), you can quickly review
them
here before
practicing the exercises. This recap highlights the essential points and logic to help you solve
problems
confidently.
Python Loops — for & while
Loops allow us to repeat a block of code multiple times instead of writing the same statements manually. They are essential for automation, data processing, and working with sequences.
Types of Loops in Python
Loop Type
When It Runs
Ideal Use Case
for loop
Runs once for every item in a sequence
When the number of repetitions is known or based on iterable items
while loop
Runs repeatedly as long as a condition remains true
When repetitions depend on a condition rather than a sequence
Python for Loop at a Glance
fruits = ["apple", "banana", "mango"]
for fruit in fruits:
print(fruit)
Key idea: Iterates over items in order — simple and predictable.
Practicing Python Loops (for, while)? Don’t forget to test yourself later in
our
Python Quiz.
About This Exercise: Python – Loops (for, while)
Loops are one of the most important building blocks in Python programming, and in this section on Solviyo, we focus entirely on them. Learning loops is essential because they allow us to repeat actions, handle collections of data, and reduce repetitive code. Without loops, many programming tasks would be inefficient or nearly impossible to manage. These exercises are designed to give you both the basics and the confidence to use loops in real situations.
We begin with for loops, which are perfect for going through sequences like lists, strings, tuples, or dictionaries. You’ll practice writing simple iterations, counting through ranges, and processing elements one by one. Then we move into while loops, which run as long as a condition remains true. These are especially useful when you don’t know in advance how many times an action needs to repeat. By practicing both, you’ll understand when to choose one over the other.
The exercises are structured in a way that starts simple and grows gradually. First, you’ll try tasks like printing sequences or summing numbers in a list. Then you’ll take on slightly more advanced problems, such as working with nested loops, applying conditions inside loops, and using break and continue to control the flow. As you progress, you’ll see how loops can solve pattern generation, data filtering, and algorithmic problems in a straightforward manner.
What makes these challenges valuable is that they’re inspired by real coding needs. For example, you might practice using loops to process user input, clean up data, or display structured results. These are the kinds of tasks you’ll face in coding interviews, coursework, and professional projects. By practicing them here, you’ll strengthen your problem-solving skills in a hands-on way rather than just memorizing theory.
At Solviyo, our goal is to make learning practical. That’s why each exercise comes with explanations that not only show the right answer but also highlight why it works. This ensures you can learn from mistakes, avoid common pitfalls, and steadily improve your coding style. We believe loops are not just about repetition—they are about building efficient and logical solutions.
Whether you’re just starting with Python or preparing for technical interviews, these loop exercises will sharpen your thinking and coding abilities. Consistent practice here will give you the confidence to handle both simple and complex tasks. Dive into the Python Loops exercises now and start building the kind of coding foundation that every programmer needs.
Need a Quick Refresher?
Jump back to the Python Cheat Sheet to review concepts before solving more challenges.