Python Day #6: How to Write and Use For Loops in Your Code
Learn Python loops fast with this beginner-friendly guide to for loops, while loops, and range(). Write cleaner code, automate tasks, and think like a developer.
Loops, the way we repeat a block of code. Not only can we automate redundant tasks but we can also speed things up. As a programmer you want to be lazy.
Yes, you read that right. Why write endless lines of code, when the whole process could be a single loop?
Welcome back to the Python Roadmap to Success, where I am giving all my readers a fair chance at learning Python the right way, following a proven roadmap that I’ve structured and taught for over the last four years.
In our last article we covered the fifth phase in the Roadmap to Success, it covered How to Get Started with While Loops.
In this lesson, we are going to hit the last step of managing flow with code. You are going to be introduced to the For Loop. This is the loop we use when we either want to go through something or repeat for an exact number of times.
Every week you’ll be introduced to a new topic in Python, think of this as a mini starter course to get you going and allow you to have a structured roadmap that actually builds to create you a solid foundation in Python. Join us today!
Over the last 8+ years I’ve been teaching full-time around the world which has given me the unique opportunity to work with over 1,500+ students. The last four years I’ve been teaching Python full-time. Breaking down complex topics, step by step.
I’ve spent this time curating a structured learning path that ensures my students have the greatest chance of success and don’t just learn but actually master Python as well as their logical thinking and problem solving.
In this article, I’m going to assume you’ve been making good progress. You’ve been reading these articles and putting them into action to actually further your learning.
Today will continue to build on our last lessons, where you’ve been learning to manage the flow of your code. Last week we broke down the while loop, the loop that repeats until a certain condition is met.
This week is all about the for loop, iteration.
One point I should note is, spend time digesting these when I write them, after six weeks most of my posts are automatically put behind a paywall.
If you haven’t subscribed to my premium content yet, you should definitely check it out. You unlock exclusive access to all of these articles and all the code that comes with them, so you can follow along!
Plus, you’ll get access to so much more, like monthly Python projects, in-depth weekly articles, the '3 Randoms' series, and my complete archive!
I spend a lot of my week on these articles, so if you find it valuable, consider joining premium. It really helps me keep going and lets me know you’re getting something out of my work!
Thank you for allowing me to do work that I find meaningful. This is my full-time job so I hope you will support my work by joining as a premium reader today.
👉Only this next month for Mothers & Fathers Day, I am offering my readers a 30% off on all annual subscriptions! Show your parents you care about your career, thanks mom and dad…
If you’re already a premium reader, thank you from the bottom of my heart! You can leave feedback and recommend topics and projects at the bottom of all my articles.
👉 I genuinely hope you get value from these articles, if you do, please help me out, leave it a ❤️, and share it with others who would enjoy this. Thank you so much!
The Iteration of Code
The for
loop is the second type of loop I take my students through, as this builds on the newly created idea of loops but does not use a conditional statement as the while loop did.
There are two ways to use this loop so I will break down each one of them for you. First I’ll do it here through my words, then we can jump into look at some code.
The code may all look foreign and new to you, that is okay. We will cover some here today and any other concepts will be covered as we approach a later section of the Python Roadmap to Success.
First, a for
loop is used to “go through” something. For example, I want to go through a sentence, I want to go through a word. I can take a word, then go to each letter in that word and do something with it. This loop will repeat until it gets to the end of the iterable. Iterable, What is that? I’ll touch on it soon.
Second, we can also design a for
loop to no go through something, but repeat for an exact number of times. For example, you rub a lamp, then a genie gives you three wishes. You get exactly three wishes, no more, no less. This version of the for
loop is just like that!
Okay, it’s time we go through these and look at some real code. As we do, take a moment to pause, think about what is happening.
👉 I genuinely hope you get value from these articles, if you do, please help me out, leave it a ❤️, and share it with others who would enjoy this. Thank you so much!
Looping through an Iterable
As I said in the above we can structure out a For Loop when we want go through something, specifically what we call an iterable. Now, the question may be, What is an Iterable?
Great question, an iterable is a type of data we can go through in Python. Now, you should know the eight types of data. Of those eight types, just over half are iterables. We have strings, lists, tuples, dictionaries, and sets.
These are the five different types of data we can go through, now some of these you may not know, that it okay as I haven’t introduced them yet. I’ll break them down for you guys when the time is right.
For the breakdown today, I will use a string as you should be comfortable with that. Let’s image you have a string variable with the value “subscribe”.
I want to go through the string and check if there are any vowels in the word.
We can do this by combining a few aspects of past lessons as well as using the for loop, let’s take a look at that in action.
I start with three variables here. First I have word = “subscribe”
, which you should subscribe as this is my full-time job so this really supports my work. Then we have vowels = “aeiou”
and finally a counter variable which you learned last lesson.
Then, I begin with my for
loop. Now I want you to read that line out loud. For letter in word, for every letter in my word. In this, letter
is known as an element. You can think of this like a local variable to be used into side the for loop that represents a single element in the word, subscribe.
You can name this anything you’d like but I prefer to name is something of which is actually represents, in this case it represents a single letter in a word. Then we have, word
. If letter
is the element, then word
is the sequence. The sequence is the iterable that you want to go through.
This loop will run nine times, because there are nine letters in, subscribe. Each time this loop runs, letter
represents a single letter in word
. We check in a conditional statement if the current letter is also found in the variable, vowels. If it is we can increase our counter variable to count the number of vowels in the word
👉 If you get value from this article, please help me out, leave it a ❤️, and share it with others who would enjoy this. Thank you so much!
It’s Time You Learn to Think like a Developer
Most people waste months bouncing between tutorials and still feel lost. That is not going to happen to you, at least not on my watch!
👉 I’m giving you my exact system that’s been proven and tested by over 1,500 students over the last 4+ years with a limited time offer!
My Python Masterclass gives you a clear roadmap, hands-on practice, and expert support—so you can gain Python fluency faster and with confidence.
Here’s What You Get:
✅ 135+ step-by-step lessons that make learning easy
✅ Live Q&A & One-on-one coaching (limited spots!)
✅ A private community so you’re never stuck
✅ Interactive tests & study guides to keep you on track
No more wasted time. No more confusion. Just real progress.
Take your career to new heights—secure your spot today!
P.S - Save 35% with the Code: pythonsummer35
🎁 - Get a free one-on-one coaching session with any Masterclass!
Still hesitant?
Go through the entire Masterclass, complete all material, and attend the Q&A's, if you still feel like your struggling I'll personally work with you one-on-one until you're confident!
Repeating an Exact Number
In the above, I broke down the first way to use a for loop. These are quite common and you’ll use them often. Next up, we want to cover the second way to use this style of loop, this way is when we want to repeat a set number of times.
For instance, if I have a block of code I want to repeat exactly five times, we can also use a for loop. But not only are we using the for keyword, we also need to use a new function to achieve this, the range
() function.
Now in the above code here I wrote a for
loop, but this time instead of going through a sequence like before I replace the sequence with the range
() function. This function will repeat a set number of times. In this case the loop with run 100 times.
As we give the range
function a start and a stop point. The first time the loop runs, i = 1
. Then the loop carries on, it goes up until, but does not include 101, therefore it stops at 100.
Each time we repeat the loop, i
is changing. I created a condition to check if i
is equal to an even number, if it is, I add i
to our variable total every time. This justs adds all the even numbers together.
Now i
is just a popular naming convention in for
loops, i
generally means index, or position. Although we see this often, there may be times where you do not actually need the value of i
for anything, therefore we don’t actually need it.
In my above code, since I do not need i
for anything in the loop, we can simply replace i
with and underscore. This is a common way to handle this and improve readability.
Another thing you may notice is range
() only has one argument this time. When we do this we do not give range
() a start position, so it automatically begins at 0 and runs 9 times since it stops just before 10.
This code covers some advanced concepts we haven’t yet covered on our series. But we will look at these soon, I just wanted to get the gears turning. To create the dice roller simulator, I used a module, random. I also squeezed what’s known as a list in here.
Don’t worry, you don’t need these right now. I just want you to see them so when we cover these in our series they won’t look as foreign to you guys.
👉 If you get value from this article, please help me out, leave it a ❤️, and share it with others who would enjoy this. Thank you so much!
Breaking Out of a Loop
Last lesson we looked at while loops, I introduced you to a way to stop the loop in its tracks — it’s called the break
statement. You use break
when you want to stop a loop right then and there.
This works for both types of loops, so it’s great. It’s like telling Python, “Hey, that’s enough — we’re done here.”
This is useful when you’re searching for something, checking user input, building menu systems, or running a loop that could go on forever until a certain condition is met. It also helps keep your code cleaner and stops it from doing more work than it needs to.
By now, you’ve seen how for
loops just keep going until they’ve gone through everything in the list, string, or whatever you’re looping through. But what if you want it to stop early?
Let’s say you’re looking for something specific — once you find it, there’s no point in continuing. That’s where break
comes in handy.
What’s happening here is simple: I’m going through each letter in the word “subscribe”
— which, by the way, you should totally do if you enjoy this stuff, it really helps me keep writing as this is my full-time job!
Anyway, as soon as the loop hits the letter “i”, it prints a message and the break
stops the loop right there. It doesn’t check the rest of the letters after that.
So yeah — break
is super helpful when you only need to go as far as finding the thing you care about, and then you’re good. No need to keep looping once your job is done.
👉 If you get value from this article, please help me out, leave it a ❤️, and share it with others who would enjoy this. Thank you so much!
My Best Starter Resources
Here are the best resources I have to offer to get you started with Python no matter your background! Check these out as they’re bound to maximize your growth in the field.
Code with Josh: This is my YouTube channel where I post videos every week designed to help break things down and help you grow.
Zero to Knowing: My Python Masterclass platform designed from 8 years teaching experience and that has helped over 1,500+ students succeed with Python. (All my readers unlock a limited time, 35% discount, use code: pythonsummer35)
My Books: Maybe you’re looking to get a bit more advanced in Python. I’ve written 3 books to help with that, from Data Analytics, to SQL all the way to Machine Learning.
The Nerd Nook: This is where you are right now! I write here multiple times per week with the sole goal of breaking down this larger than life topic to help you excel and learn Python faster than you thought was possible.
This article will be a piece to a larger roadmap but will go behind a paywall after 6 weeks! To help support my work in teaching others consider joining as a premium reader to unlock all these articles, monthly Python projects and in-depth dives.
Wrapping Up Lesson Six
By now, loops in Python should feel a whole lot easier—both while
loops and for
loops. You’ve seen how they let you run the same code over and over until something changes, how you can loop through words, lists, and other things, and how you can do something a certain number of times using range()
.
But here’s the thing: loops aren’t just about repeating stuff—they’re about working smarter. They help you keep your code clean, save time, and handle boring, repetitive work without doing it all by hand.
That’s the big shift—thinking like a developer means finding the easiest, smartest way to solve a problem.
If this clicked for you, go back and try out the examples again. The more you practice, the more natural it all becomes—just like how loops work: repeat, learn, grow.
This is just one stop on your Python journey. Stick around and you won’t just get better at Python—you’ll get better at solving real problems.
Let’s keep moving forward with Day #7 next week.
Hope you all have an amazing week nerds ~ Josh (Chief Nerd Officer 🤓)
👉 If you’ve been enjoying these lessons, consider subscribing to the premium version. You’ll get full access to all my past and future articles, all the code examples, extra Python projects, and more.