Python

Loops

Learn how to repeat code execution using for loops and while loops in Python.

By TechCoder TeamLast updated: 2026-06-02
In a Nutshell

Learn how to repeat code execution using for loops and while loops in Python. This hands-on tutorial focuses on practical implementation of loops concepts.

Loops in Python

Loops allow you to repeat a block of code multiple times. This is essential for tasks like processing items in a list, counting, or keeping a program running until a condition changes.

Which Loop to Use?

The for Loop

The for loop is used to iterate over a sequence (like a list, tuple, string, or range).

Looping through a Range

The range() function generates a sequence of numbers.

PYTHON PLAYGROUND
⏳ Loading editor…

Looping through a String

You can loop through each character in a string.

PYTHON PLAYGROUND
⏳ Loading editor…

Looping through a List

PYTHON PLAYGROUND
⏳ Loading editor…

Looping through a Dictionary

You can iterate over keys, values, or both.

PYTHON PLAYGROUND
⏳ Loading editor…

The while Loop

The while loop repeats code as long as a condition is True.

PYTHON PLAYGROUND
⏳ Loading editor…

Interactive Example: Sum of Numbers

Let's calculate the sum of numbers from 1 to 10.

PYTHON PLAYGROUND
⏳ Loading editor…

AI Mentor

Confused about "Python for and while loops"? Ask our AI mentor for a simplified explanation.

Quiz

Quiz

Question 1 of 5

What does range(3) generate?

1, 2, 3
0, 1, 2
0, 1, 2, 3
1, 2

Key Takeaways

for loops iterate over sequences (lists, strings, ranges)
range(start, stop, step) generates numbers
while loops run as long as a condition is True
✅ Use .items() to loop through dictionary keys and values

What's Next?

In the next lesson, we'll master loop control:

  • break to exit loops early
  • continue to skip iterations
  • else clauses in loops
  • Nested loops

Keep coding! 🚀