mailwal

Python From Scratch #26 - for Loops

Shefali Jangid

Python From Scratch #26 - for Loops

Loop through values and repeat code without writing the same thing again and again.

Hey 👋

Welcome back to the Python from Scratch series.

So far, we’ve learned how to make decisions using if, elif, and else.

Now let’s learn how to repeat code using loops.

What Are Loops?

Loops let us run a block of code multiple times.

For example, if you want to print numbers from 1 to 5, you could write:

print(1)
print(2)
print(3)
print(4)
print(5)

But what if you wanted to print numbers from 1 to 1000?

Writing every print() manually would take a lot of time.

That’s where loops are useful.

The for Loop

A for loop lets you go through each item in a sequence one at a time.

For example, we can loop through a string:

name = "Python"

for letter in name:
    print(letter)

# Output:

# P
# y
# t
# h
# o
# n

Python takes one character at a time from the string and stores it in the variable letter.

You can think of it like this:

First loop  → P
Second loop → y
Third loop  → t
Fourth loop → h
Fifth loop  → o
Sixth loop  → n

The loop stops when there are no more characters.

The Variable Name Can Be Anything

In this example:

for letter in name:
    print(letter)

letter is just a variable name.

You could call it character:

for character in name:
    print(character)

It works the same way.

The general structure is:

for variable in sequence:
    # Code to repeat

Using range()

Now, what if you don’t want to loop through a string?

And, what if you simply want to repeat something a specific number of times?

That’s where the range() function is useful.

For example:

for number in range(5):
    print(number)

# Output:

# 0
# 1
# 2
# 3
# 4

Notice that it starts at 0 and stops before 5.

So range(5) gives us 0, 1, 2, 3, 4.

It does not include 5.

Starting From a Different Number

You can also tell range() where to start.

for number in range(5, 10):
    print(number)

# Output:

# 5
# 6
# 7
# 8
# 9

Here range(5, 10) means:

  • Start at 5

  • Stop before 10

Using a Step

You can also tell range() how much to increase by each time.

for number in range(5, 10, 2):
    print(number)

# Output:

# 5
# 7
# 9

Here range(5, 10, 2) means:

  • Start at 5

  • Stop before 10

  • Increase by 2 each time

So the values are:

5 → 7 → 9

Counting Backward

The step can also be negative.

A negative step makes range() count downward instead of upward:

for number in range(10, 0, -1):
    print(number)

# Output:

# 10
# 9
# 8
# 7
# 6
# 5
# 4
# 3
# 2
# 1

Here range(10, 0, -1) means:

  • Start at 10

  • Stop before 0

  • Decrease by 1 each time

A negative step is useful when you need to count backward.

range() Syntax

The general syntax is:

range(start, stop, step)

All three values don’t have to be provided.

range(stop)

range(5)

Starts from 0 and stops before 5.

range(start, stop)

range(5, 10)

Starts from 5 and stops before 10.

range(start, stop, step)

range(5, 10, 2)

Starts from 5, stops before 10, and increases by 2.

A Simple Example

Let’s use a for loop to print a message five times:

for number in range(5):
    print("Hello")

# Output:

# Hello
# Hello
# Hello
# Hello
# Hello

The variable number isn’t being used here, but the loop still runs five times.

Using range() With len()

You can combine range() with len() when you need both the index and the character at that position.

For example:

name = "Python"

for i in range(len(name)):
    print(i, name[i])

# Output:

# 0 P
# 1 y
# 2 t
# 3 h
# 4 o
# 5 n

Here, len(name) gives us the number of characters in the string, which is 6.

Then range(len(name)) gives us the indexes: 0, 1, 2, 3, 4, 5

We can use each index to access the corresponding character: name[i]

This gives you both the index and the character at that position.

This pattern is useful when you need to know where something is, not just what the value is.

For simply going through each character, you can still use:

for character in name:    
    print(character)

You don’t need indexes unless you actually need them.

Try It Yourself

Now, a small exercise for you.

Try these:

  1. Use a for loop to print the numbers from 1 to 10.

  2. Use range() to print the numbers from 5 to 15.

  3. Use range() with a step of 2 to print the even numbers from 2 to 10.

  4. Loop through your name and print each character on a separate line.

That’s all for today!

Tomorrow, we’ll learn about another type of loop: the while loop.


If you enjoyed this newsletter, consider sharing it with another developer. And if you have any feedback, I’d love to hear it. It would genuinely mean a lot to me. ❤️


Want to sponsor my work? Let’s talk!

See you tomorrow ✨

- Shefali

Thanks for reading DevBits by Shefali! Subscribe for free to receive new posts and support my work.

If you enjoyed this edition of DevBits, you can support my work here ☕

Buy Me a Coffee

Your support helps me keep creating more developer tips, UI snippets, and helpful content for the community.

© 2026 Shefali Jangid
548 Market Street PMB 72296, San Francisco, CA 94104