sethserver.com Subscribe
Python For Loops Explained: Iteration, Scoping, and Generators

Python For Loops Explained: Iteration, Scoping, and Generators

By Seth Black • Updated: December 22, 2025

Python · 1 min read

Like this kind of writing? Get one email a week with notes on startups, AI, and the occasional strong opinion about Python: subscribe to the newsletter.

Python has some pretty amazing features, and one of its most powerful and versatile is the for loop.

pythons = ['Graham', 'John', 'Terry G', 'Eric', 'Terry J', 'Michael',]
for name in pythons:
print(f"It's {name}")

Simple enough: a list of six names and a for/in loop that iterates over the names in the list and outputs each name on a line. I feel like it's important right out of the gate to point out a caveat that trips up a lot of Python programmers: variable scoping. Let's try this again but print out our variable name outside of the loop.

pythons = ['Graham', 'John', 'Terry G', 'Eric', 'Terry J', 'Michael',]
for name in pythons:
print(f"It's {name}")
print(f"Still {name}")

If you run this code you'll notice something that is unexpected. The variable name is still in scope and holds the last value from the for loop. Keep this in mind because it can cause some strange things to happen if you reuse variable names in your code.

The next feature that makes Python's iterators so powerful is the generator. Generators allow lazy-loading of iterables, which means you are not required to front-load long running, IO-bound, CPU or memory intensive tasks. The following example will iterate over a million strings using a generator.

def large_memory_list():
for n in range(1000000):
yield('thisisasuperduperlongstringnotreallybutletusjustpretendforamomentthatitisinfactalongstringthankyou')
for long_string in large_memory_list():
print(f"{long_string}")

Instead of allocating a million strings at once, Python allows us to generate the strings one at a time using the yield statement. This can be useful for processing large quantities of data one line at a time, or when iterating over a large list of items when you don't necessarily need to traverse the entire set. As you can see the for loop in Python is quite powerful when used in conjunction with native lists, sets, and generators.

Good luck and happy looping!

-Sethers

Share this post
Newsletter

One email, once a week.

Notes on databases, systems, and the occasional strong opinion about Python. No spam, unsubscribe anytime.

Seth Black
Written by

Seth Black

Engineer and founder based in Texas. Writes about databases, AI, and running things in production. Embeds in small teams as lead engineer.

More from Python

View all →
Python

OpenAI Bought Astral - and my fav tool uv

Mar 23, 2026
Python

Python Pydantic Validation: Stop Writing Manual Checks

Mar 04, 2026
Python

Python asyncio Recipes

Mar 04, 2026