When I started learning Python, I understood variables, data types, conditionals, and functions. But there was one concept I knew I couldn't avoid for long: loops.
Loops are one of those programming concepts that seem simple when someone explains them, but actually using them in a project is where things start to make sense.
After learning and practicing them, I decided to write about what I learned and how I used loops in my first Python projects.
What Is a Loop?
A loop allows us to repeat a block of code multiple times without writing the same code over and over again.
Imagine I wanted Python to print the numbers from 1 to 5.
Without a loop, I would have to write:
print(1)
print(2)
print(3)
print(4)
print(5)
That works, but imagine having to print 1 to 1,000.
That's where loops become useful.
Instead, I can use a loop to tell Python:
"Repeat this action for each number."
for number in range(1, 6):
print(number)
The result is:
1
2
3
4
5
Much cleaner.
The for Loop
The first loop I focused on was the for loop.
A for loop is useful when I want to go through a collection of items or repeat something a specific number of times.
For example:
fruits = ["Apple", "Banana", "Orange"]
for fruit in fruits:
print(fruit)
Python goes through the list one item at a time.
The output is:
Apple
Banana
Orange
The important thing I learned here is that the variable fruit represents the current item in the list.
So the loop is essentially doing:
First → fruit = Apple
Second → fruit = Banana
Third → fruit = Orange
This helped me understand that a loop isn't magic. Python is simply repeating the same instructions while changing the value of the loop variable.
Understanding range()
Another important thing I learned was range().
For example:
for number in range(5):
print(number)
This produces:
0
1
2
3
4
One thing that initially confused me was why it stops at 4 instead of 5.
The reason is that range(5) starts at 0 and stops before 5.
You can also specify a starting point:
for number in range(1, 6):
print(number)
Now we get:
1
2
3
4
5
Understanding this made working with loops much easier.
Using Loops With Conditions
Loops become much more powerful when combined with if statements.
For example:
for number in range(1, 6):
if number % 2 == 0:
print(number)
This goes through the numbers from 1 to 5 and only prints the even numbers.
Output:
2
4
This showed me that loops don't just have to repeat the exact same action. I can also make decisions during each repetition.
Using enumerate()
One of the most useful things I learned while building my quiz project was enumerate().
Suppose I have:
questions = [
"What is Python?",
"What is HTML?",
"What is CSS?"
]
I can write:
for index, question in enumerate(questions):
print(index, question)
The output will be:
0 What is Python?
1 What is HTML?
2 What is CSS?
enumerate() gives me both:
- the position of the item (
index) - the actual item (
question)
This became particularly useful in my quiz game.
Applying Loops: My Python Quiz Game
After learning loops, I wanted to actually use them in a project.
I built a simple Python Quiz Game.
The game contains five questions and asks the user to select an answer for each one.
I stored the questions in a list:
questions = [
"Question 1...",
"Question 2...",
"Question 3...",
"Question 4...",
"Question 5..."
]
And I stored the correct answers in another list:
answers = ['B', 'A', 'D', 'B', 'B']
Then I used enumerate() to connect each question to its correct answer:
for index, question in enumerate(questions):
print(question)
answer = input("What is your answer?: ").upper()
if answer == answers[index]:
score += 1
This was one of the moments where loops really started to click for me.
Instead of writing separate code for Question 1, Question 2, Question 3, and so on, I could write the logic once and let the loop handle all five questions.
Keeping Track of the Score
I also learned that I can use a variable outside the loop and modify it inside the loop.
I started with:
score = 0
Then whenever the user answered correctly:
score += 1
After all the questions were completed, I displayed the result:
print(f"Your score is: {score}/5")
This helped me understand how variables can change as a program runs.
What I Learned From Loops
Learning loops wasn't just about learning the for syntax.
I learned several important programming ideas along the way:
1. Don't repeat code unnecessarily
If I'm writing the same block of code multiple times, I should ask myself:
"Can a loop handle this?"
2. Lists and loops work really well together
A list can hold multiple pieces of information, while a loop can process them one by one.
for item in items:
print(item)
This combination is something I'll probably use constantly as I continue learning Python.
3. Indexes are important
Understanding indexes helped me understand how I could connect data from different lists.
answers[index]
4. Loops make programs more dynamic
Without loops, my quiz would have required separate code for every question.
With a loop, the same logic can handle any number of questions.
That's a much better way to think about programming.
What I'm Learning Next
Now that I've finished learning loops and practiced them by building a project, I'm moving on to dictionaries and sets.
I'm following the approach of learning a concept and then building something with it instead of trying to memorize everything at once.
My next Python project will be a Simple Shopping Cart, where I'll put these concepts together and start working with dictionaries.
Final Thoughts
Loops were one of the concepts I was looking forward to learning because I knew they were fundamental to programming.
After actually using them in a project, I can see why.
The biggest lesson for me wasn't simply learning:
for item in items:
It was learning how to look at a problem and recognize:
"I don't need to write this five times. I can make Python repeat it for me."
That's one of the small moments that makes learning programming feel a lot more rewarding.
I'm still learning Python, but I'm starting to understand that the best way to learn isn't just reading about a concept.
Learn it → practice it → build something with it → make mistakes → fix them → move on.
And that's exactly what I'm doing.
Top comments (0)