Python Basics Part 4: Loops in Python Explained (for, while, break, continue)

Python Basics – Part 4: Loops in Python (for, while, break, continue)

In this part of my 5-week Python training series, we explore one of the most important programming structures — loops.
Loops allow you to repeat actions efficiently, handle sequences, and control flow dynamically.

We’ll cover:

  • The for loop
  • The while loop
  • The range() function
  • continue and break
  • Infinite loops and random examples

for Loop and range()

In many other languages, a counting loop might look like this:

for (int i = 0; i < 10; i++) { ... }

In Python, we use:

for i in range(start, end):

Remember: the start is inclusive, but the end is exclusive.

range1 = range(1, 8)
print(type(range1))
print(list(range1))

Output:

<class 'range'>
[1, 2, 3, 4, 5, 6, 7]

Basic for Loop Example

for i in range(1, 8):
    print(i, end=' ')

Output:

1 2 3 4 5 6 7

You can also use a more descriptive variable name:

for number in range(1, 8):
    print(number, end=' ')

If you don’t need the loop variable, use an underscore _:

for _ in range(1, 8):
    print("Hello", end=' ')

Default Start at 0

If you pass only one value to range(), Python starts counting from 0.

for i in range(5):
    print(i, end=' ')

Output:

0 1 2 3 4

Step Parameter

range(start, end, step) — The third value defines how much to increase (or decrease) each time.

for i in range(1, 11, 2):
    print(i, end=' ')

Output:

1 3 5 7 9

Negative step:

for i in range(9, 0, -1):
    print(i, end=' ')

Output:

9 8 7 6 5 4 3 2 1

Using range() with Lists

fruits = ["apple", "banana", "cherry"]
for i in range(len(fruits)):
    print(i, fruits[i])

Output:

0 apple
1 banana
2 cherry

Iterating Over Strings

Strings are sequences too:

for letter in "Hello World":
    print(letter, end=" * ")

Output:

H * e * l * l * o *   * W * o * r * l * d *

continue and break

These keywords control the flow of loops.

continue

Skips the current iteration and jumps to the next one.

for i in range(10):
    if i == 4:
        continue
    print(i, end=' ')

Output:

0 1 2 3 5 6 7 8 9

break

Stops the loop entirely.

for i in range(10):
    if i == 4:
        break
    print(i, end=' ')

Output:

0 1 2 3

You can also add an else clause to a loop — it runs only if the loop finishes normally (without break):

for i in range(5):
    print(i)
else:
    print("Loop finished without break.")

while Loop

A while loop repeats as long as the condition is True.

Basic Counting Example

i = 0
while i < 5:
    print(i, end=' ')
    i += 1

Output:

0 1 2 3 4

Equivalent for loop:

for i in range(5):
    print(i, end=' ')

When to Use while

Use while when the number of repetitions is unknown in advance.

i = 1
while i < 100:
    print(i, end=' ')
    i += i

Output:

1 2 4 8 16 32 64

Infinite Loops

Be careful! A missing update statement can create an endless loop.

i = 0
while i < 5:
    print(i, end=' ')
    i += 1  # without this, infinite loop

while True Example

Let’s simulate rolling a die until we get a six 🎲

from random import randint

count = 0
while True:
    count += 1
    roll = randint(1, 6)
    if roll == 6:
        print(f"It took {count} tries to roll a six.")
        break

Example Output:

It took 10 tries to roll a six.

Nested Loops (Bonus)

You can nest loops inside each other:

for i in range(3):
    for j in range(2):
        print(f"{i},{j}", end=' ')

Output:

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

for vs while

Use CaseLoop TypeExample
Known number of iterationsforCounting 1–10
Unknown repetitionswhileWaiting for user input or a random event
Skipping iterationscontinueSkip even numbers
Stopping earlybreakStop when condition met

Summary

In this lesson, we covered:

  • The for loop and the range() function
  • How to iterate over lists and strings
  • The continue and break statements
  • The while loop (including infinite loops)
  • A random simulation example using while True
  • Nested loops and when to use each type

Next up: Random numbers, input/output, and formatting in Python!

Leave a Reply

Your email address will not be published. Required fields are marked *