PYTHON LOOPS MADE EASY: HOW TO REPEAT ACTIONS WITHOUT WRITING THE SAME CODE TWICE
PYTHON LOOPS MADE EASY: HOW TO REPEAT ACTIONS WITHOUT WRITING THE SAME CODE TWICE
Imagine you need to print the numbers 1 to 100. Would you write 100 separate print statements? Of course not. That is exactly what Python loops are for. Loops allow your program to repeat a block of code multiple times without you having to write it over and over again.
In this guide we will cover the two main types of Python loops — the for loop and the while loop — with simple examples any beginner can follow.
WHAT IS A LOOP?
A loop is an instruction that tells Python to run the same block of code repeatedly. You decide how many times it runs or under what condition it should stop. Loops save time, reduce errors, and make your code clean and professional.
THE FOR LOOP
The for loop is used when you know exactly how many times you want to repeat something.
Example — printing numbers 1 to 5:
- Start with the word "for"
- Give a variable name to represent each item e.g. number
- Use "in range(1, 6)" to count from 1 to 5
- Write your repeated action indented below
for number in range(1, 6):
print(number)
Output: 1, 2, 3, 4, 5
You can also loop through a list of items:
books = ["Python Made Simple", "Django Made Simple", "ML Made Simple"]
for book in books:
print(book)
This prints each book title one by one automatically.
THE WHILE LOOP
The while loop keeps running as long as a condition is True. Use it when you do not know exactly how many repetitions you need.
count = 5
while count > 0:
print("Count:", count)
count = count - 1
print("Done!")
This loop keeps running while count is greater than 0. Each time it runs, count decreases by 1. When count hits 0 the loop stops.
COMMON LOOP MISTAKES BEGINNERS MAKE
- Forgetting to indent the code inside the loop — Python requires this
- Creating an infinite loop by forgetting to update your variable in a while loop
- Using range(5) and expecting it to include 5 — it actually goes from 0 to 4
- Overcomplicating loops when a simple range would do the job
BREAKING OUT OF A LOOP EARLY
Sometimes you want to stop a loop before it finishes. Use the break statement:
for number in range(1, 100):
if number == 10:
break
print(number)
This loop would normally run 99 times but stops the moment number equals 10.
SKIPPING ONE ROUND WITH CONTINUE
The continue statement skips the current round and moves to the next:
for number in range(1, 6):
if number == 3:
continue
print(number)
Output: 1, 2, 4, 5 — number 3 is skipped.
REAL WORLD EXAMPLE: BOOK SALES COUNTER
daily_sales = [3, 5, 2, 8, 4, 6, 1]
total = 0
for sale in daily_sales:
total = total + sale
print("Total books sold this week:", total)
This loop adds up all the daily sales automatically. No calculator needed.
LOOPS ARE EVERYWHERE IN PROGRAMMING
Once you understand loops, you will see them everywhere — processing lists of users, reading files line by line, training machine learning models, building Android apps that display lists of data. Loops are one of the most powerful tools in any programmer's toolkit.
READY TO GO DEEPER?
This is just one chapter of what you will learn in Python Made Simple by Benjamin Koikoi. The book covers everything from variables to functions to object-oriented programming — all explained simply with real examples. Get your copy today for just $1 on Gumroad and Amazon KDP. It is the cheapest investment you will make in your coding journey.

Comments
Post a Comment