👋 Work With Me

I help startups and teams build production-ready apps with Django, Flask, and FastAPI.

Let’s Talk →

I'm always excited to take on new projects and collaborate with innovative minds.

Address

No 7 Street E, Federal Low-cost Housing Estate, Kuje, Abuja 903101, Federal Capital Territory

Social Links

Tutorials

Python Tutorial Series – Lesson 10: Loops in Python

Python Tutorial for Beginners (Lesson 10): Learn loops in Python (for, while, break, continue, pass), nested loops, real-world examples, and exercises.

Python Tutorial Series – Lesson 10: Loops in Python

Welcome back to the Python Tutorial Series for Beginners! 🎉

In Lesson 9, we learned about conditional statements – how programs make decisions with if, elif, and else.

Now in Lesson 10, we’ll explore loops – the way Python repeats tasks efficiently.

By the end of this lesson, you’ll understand:

  • Why loops are important
  • The for loop
  • The while loop
  • Loop control statements (break, continue, pass)
  • Nested loops
  • Real-world use cases
  • Beginner exercises

🔹 What are Loops?

👉 Loops are used to repeat a block of code multiple times.

💡 Example in real life:

  • “For each student in the class, take attendance.”
  • “While the battery is not empty, keep playing music.”

🔹 1. The for Loop

The for loop is used when you want to iterate over a sequence (list, tuple, string, range, etc).

 
# Looping through a list
fruits = ["apple", "banana", "cherry"]

for fruit in fruits:
   print(fruit)

🔹 2. The range() Function with Loops

 
for i in range(5):
   print(i)

👉 Output:

 
0
1
2
3
4

You can also specify start and step:

 
for i in range(2, 10, 2):
   print(i)

👉 Output: 2, 4, 6, 8


🔹 3. The while Loop

The while loop is used when you don’t know in advance how many times the loop will run.

 
count = 1

while count <= 5:
   print("Count:", count)
   count += 1

👉 This keeps running until the condition is no longer true.


🔹 4. Loop Control Statements

break – exit the loop immediately

 
for num in range(1, 10):
   if num == 5:
       break
   print(num)

👉 Stops when num is 5.


continue – skip the current iteration

 
for num in range(1, 6):
   if num == 3:
       continue
   print(num)
 

👉 Skips printing 3.


pass – placeholder for future code

 
for num in range(5):
   pass  # do nothing for now

👉 Useful when writing code structure first.


🔹 5. Nested Loops

Loops can be placed inside other loops:

 
for i in range(3):
   for j in range(2):
       print(f"i={i}, j={j}")

👉 This prints all combinations of i and j.


🔹 6. Real-World Use Cases

Summing numbers in a list

 
numbers = [10, 20, 30, 40]
total = 0

for num in numbers:
   total += num
   
print("Total =", total)

Password retry system

 
password = ""
attempts = 0

while password != "python123" and attempts < 3:
   password = input("Enter password: ")
   attempts += 1
   
if password == "python123":
   print("Access granted")
else:
   print("Access denied")

🔹 Exercises for Beginners

Try these in a new file (lesson10.py):

  1. Print numbers from 1 to 20 using a for loop.
  2. Print only even numbers between 1 and 50 using a loop.
  3. Write a while loop that counts down from 10 to 1.
  4. Use a loop to calculate the factorial of a number (e.g., 5! = 120).
  5. Challenge: Create a multiplication table (1–10) using nested loops.

🎯 Recap

In this lesson, you learned:
✅ Loops allow you to repeat tasks efficiently
for loops iterate over sequences
while loops repeat until a condition is false
✅ Control statements (break, continue, pass) modify loop behavior
✅ Nested loops handle more complex iterations
✅ Real-world use cases include calculations, retries, and data processing

Next up: Lesson 11 – Functions in Python 🛠️ (where we group code into reusable blocks).

💼 Need a Developer?

I'm Kingsley Odume, a Django, Flask, and FastAPI developer with experience building SaaS platforms, APIs, and modern web apps. If you're a recruiter or business owner looking for a reliable software developer, let's connect!

🚀 Hire Me

3 min read
Aug 20, 2025
By Kingsley Odume
Share

Leave a comment

Your email address will not be published. Required fields are marked *

Related posts

Aug 29, 2025 • 3 min read
Step-by-Step: Building a SaaS App with Django + Stripe Payments

Learn how to build a real-world SaaS app with Django and Stripe paymen...

Aug 21, 2025 • 3 min read
Final Wrap-Up: Python Tutorial Series for Beginners

Congratulations on completing the Python Tutorial Series! 🚀 In this w...

Aug 20, 2025 • 4 min read
Final Lesson: Mini Project – Putting It All Together in Python

In this final lesson, we’ll bring all those concepts together in a Min...

Your experience on this site will be improved by allowing cookies. Cookie Policy