Back to Blog
Python

Python For Loops Explained: A Beginner's Guide with Clear Examples

9/14/2025
5 min read
Python For Loops Explained: A Beginner's Guide with Clear Examples

Struggling with Python for loops? Our easy-to-follow guide breaks them down with practical examples

Python For Loops Explained: A Beginner's Guide with Clear Examples

Python For Loops Explained: A Beginner's Guide with Clear Examples

Python For Loops Explained: A Beginner's Guide with Clear Examples

Ever found yourself doing the same task over and over again? It gets boring, right? Computers are fantastic at handling repetitive tasks without complaining, and in the world of Python programming, the for loop is one of the most elegant tools we have to make that happen.

If you're just starting your coding journey, the concept of loops might seem a bit abstract. But trust me, by the end of this article, you'll not only understand what a for loop is but you'll also see how it’s like giving your computer a simple, clear to-do list for a bunch of items. Let's break it down, together.

What is a For Loop, Really? Think of a Shopping List.

Imagine you have a shopping basket and a list of items to buy: ["apples", "bread", "milk", "cheese"]. Your mental process is a perfect loop:

  1. You start at the top of the list.

  2. You pick the first item ("apples") and put it in your basket.

  3. You move to the next item ("bread") and put it in your basket.

  4. You repeat this process until...

  5. ...you reach the end of the list. Then you're done!

A Python for loop does exactly this, but with data. It takes a "sequence" of items (like our shopping list) and goes through each item, one by one, performing the same action on each one.

The Syntax: Writing Your First Loop

The structure of a for loop in Python is beautifully simple and reads almost like English.

python

shopping_list = ["apples", "bread", "milk", "cheese"]

for item in shopping_list:
    print("I need to buy:", item)

Let's translate this:

  • for: This is the keyword that starts the loop.

  • item: This is a variable name we choose. It acts as a temporary placeholder. On each turn of the loop, item becomes the next value in the list. You could call it thing, product, or even x—though clear names are always better!

  • in: Another keyword that tells Python what collection we're looping over.

  • shopping_list: This is our sequence of items (the shopping list).

  • :: The colon is crucial! It tells Python that the indented code below is the body of the loop.

  • print("I need to buy:", item): This is the body of the loop. This is the action we want to perform for every single item. Notice the indentation (usually 4 spaces). This indentation is how Python knows what code belongs inside the loop.

Output:

text

I need to buy: apples
I need to buy: bread
I need to buy: milk
I need to buy: cheese

See? The print statement ran repeatedly, but each time, the item variable had a new value from the list.

Beyond Lists: Looping Through Different Things

Your "shopping list" doesn't have to be a list. You can loop over any "iterable" object. Fancy word, simple meaning: anything that can give you its items one at a time.

1. Looping through a string: A string is a sequence of characters.

python

name = "Python"
for letter in name:
    print(letter, end='-')  # The 'end' argument prints a dash instead of a new line

# Output: P-y-t-h-o-n-

2. Looping with range(): This is incredibly common. The range() function generates a sequence of numbers.

python

# Print numbers 0 to 4
for number in range(5):
    print(number)

# Output: 0, 1, 2, 3, 4

# Print a message 3 times
for i in range(3):
    print("Hello again!")

# Output: Hello again! (three times)

Why Are For Loops So Powerful? The Magic is in the Body.

The real power isn't in the looping itself, but in what you do inside the loop. You can perform calculations, make decisions, and manipulate data.

Example: Calculating the sum of a list of numbers.

python

expenses = [125, 50, 75, 200]
total = 0

for cost in expenses:
    total = total + cost  # Add each cost to the running total

print("Your total expenses are: ₹", total)
# Output: Your total expenses are: ₹ 450

Example: Making decisions inside a loop.

Let's find out which of our expenses were above ₹100.

python

expenses = [125, 50, 75, 200]

for cost in expenses:
    if cost > 100:
        print(f"₹{cost} is a major expense.")
    else:
        print(f"₹{cost} is a manageable expense.")

# Output:
# ₹125 is a major expense.
# ₹50 is a manageable expense.
# ₹75 is a manageable expense.
# ₹200 is a major expense.

A Common Beginner Hurdle (And How to Jump Over It)

Many beginners try to change the loop variable (item) and expect the original list to change. It doesn't work that way. item is just a copy. To modify the list, you need to use the index.

A more advanced, but very useful, technique is to use the enumerate() function. It gives you both the index and the value during the loop.

python

shopping_list = ["apples", "bread", "milk", "cheese"]

# Let's make the list all uppercase by modifying the original list
for index, item in enumerate(shopping_list):
    shopping_list[index] = item.upper()

print(shopping_list)
# Output: ['APPLES', 'BREAD', 'MILK', 'CHEESE']

Ready to move beyond the basics and start building? Visit us at codercrafter.in today to explore our courses and enroll. Let's craft your future in code, together.

Related Articles