</> Code Editor

Python Loops (for, while) Exercises


1/20

Python Loops (for, while) Practice Questions

Correct
0%

Which of the following is the correct syntax of a for loop in Python?


  • In Python, a for loop uses the in keyword and usually works with an iterable (like a list, tuple, string, or range).
  • The correct syntax is:
for variable in iterable:
    # loop body
  • Option 2 is correct: for i in range(0,5) iterates from 0 to 4.
  • Option 1 looks like C/Java syntax (not valid in Python).
  • Option 3 uses incorrect to keyword (Python doesn’t have it).
  • Option 4 uses foreach, which exists in other languages like PHP, but not in Python.

This tests whether learners can identify the basic correct syntax of a Python for loop.

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.

Python while Loop at a Glance

count = 1
while count <= 3:
    print(count)
    count += 1

Key idea: Repeats until a condition becomes false.

Important: A while loop can become infinite if the condition never turns false.

Nested Loops in Python

A loop can be placed inside another loop. Example uses: printing patterns, 2D lists, repeated combinations.

Type Example
for inside for Best for 2D structured data
while inside for Repetition with conditions per sequence item
Mixed (for + while) Flexible for complex logic

Loop Control Statements in Python

Flow modifiers let you control the behavior of loops:

Statement Effect
break Exits the loop immediately
continue Skips to the next iteration
else with loop Runs only if the loop finishes without break
pass Does nothing — placeholder for future logic

Choosing the Right Loop in Python

Scenario Best Choice
You are working through items in a list, tuple, string, dict or range for loop
You don’t know how many repetitions until a condition happens while loop
Need multiple levels of iteration Nested loops
Want to exit early based on a condition Use break
Want to skip specific data points Use continue

Quick Notes

  • Loops reduce repetition and improve program structure.
  • Always ensure while loops have a stopping condition.
  • Use break and continue only when they improve readability.
  • Nested loops are powerful but can reduce performance if overused.



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.