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 Exercises – For and While Loops
Welcome to Solviyo’s Python Loops exercises, where you will learn how to use for and while loops effectively in Python programming. Loops are essential for repeating tasks, processing collections of data, and writing efficient code. These exercises are designed to help you master loops from basic iterations to more advanced real-world applications.
What You Will Learn
In this set of Python Loops exercises, you will explore:
Using for loops to iterate over sequences like lists, strings, tuples, and dictionaries.
Working with while loops to execute actions as long as a condition is true.
Applying conditions inside loops to control the flow of execution.
Using break and continue to manage loop behavior effectively.
Working with nested loops to solve more complex problems.
Applying loops to real-world scenarios such as data processing, pattern generation, and user input handling.
Writing clean, efficient, and maintainable code using loops.
Why Learning Python Loops Matters
Loops are one of the most fundamental concepts in Python programming. Mastering loops allows you to automate repetitive tasks, handle large data efficiently, and implement logical solutions to complex problems. Understanding when and how to use for and while loops will also prepare you for advanced Python topics and technical interviews.
Start Practicing Python Loops Today
By working through these exercises, you will gain confidence in using Python loops for a variety of tasks. Each exercise includes detailed explanations so you can learn from mistakes and improve your coding style. Start practicing Python Loops exercises now and build a solid foundation that will make tackling more complex Python programming challenges much easier.
Need a Quick Refresher?
Jump back to the Python Cheat Sheet to review concepts before solving more challenges.