• Python Snacks
  • Posts
  • The Simplest Way to Understand The Difference Between For Loops and While Loops

The Simplest Way to Understand The Difference Between For Loops and While Loops

There's a major difference between for and while loops, and this explanation hits the key difference.

Imagine you’re a baker for a second, baking some muffins (chocolate chip, anyone?).

You’re tasked with making 12 muffins, so you’ll pour batter into 12 muffin cups in the tin. In this scenario, you have a set amount (this is important) of muffins to create.

A few days later, you’re told to make as many muffins as possible using the same batter. You re-create the same batter, but now you have to go until run out of batter - you don’t know how many (also important) muffins you’ll get out of it.

The first scenario where you’re making 12 muffins regardless if batter is leftover or not is representative of a for loop, where you know how many muffin you’re making.

The second scenario where you go until the batter is empty no matter how many muffins you make is representative of a while loop in programming looks like.

Example of a For Loop

In Python, if we know the number of times we’ll loop over something, we’ll use a for loop. This “number of times” can be a list, a range, a set, a dictionary - better generalized as an iterable. Using our example from above:

# We KNOW we're making 12 muffins
for cup_number in range(12):
    print(f"Filling muffin cup {cup_number + 1}")

Here, it doesn’t matter if there’s leftover batter, as the objective is to fill all 12 cups.

Example of a While Loop

On the contrary, if we want to go until a condition is met, we’ll use a while loop. In our example, we didn’t care about the number of muffins we made - we only cared if the batter was used entirely. In Python, this may look like this:

batter_left = 100  # Assume 100 is the total batter amount
muffin_count = 0

# Assume each muffin takes 8 units of batter
while batter_left >= 8:
    muffin_count += 1
    batter_left -= 8  # Reduce batter after each muffin
    print(f"Made muffin {muffin_count}, batter left: {batter_left}")

Hope this helps!

📧 Join the Python Snacks Newsletter! 🐍

Want even more Python-related content that’s useful? Here’s 3 reasons why you should subscribe the Python Snacks newsletter:

  1. Get Ahead in Python with bite-sized Python tips and tricks delivered straight to your inbox, like the one above.

  2. Exclusive Subscriber Perks: Receive a curated selection of up to 6 high-impact Python resources, tips, and exclusive insights with each email.

  3. Get Smarter with Python in under 5 minutes. Your next Python breakthrough could just an email away.

You can unsubscribe at any time.

Interested in starting a newsletter or a blog?

Do you have a wealth of knowledge and insights to share with the world? Starting your own newsletter or blog is an excellent way to establish yourself as an authority in your field, connect with a like-minded community, and open up new opportunities.

If TikTok, Twitter, Facebook, or other social media platforms were to get banned, you’d lose all your followers. This is why you should start a newsletter: you own your audience.

This article may contain affiliate links. Affiliate links come at no cost to you and support the costs of this blog. Should you purchase a product/service from an affiliate link, it will come at no additional cost to you.

Reply

or to participate.