Loops are one of the most powerful concepts in Python. They allow a program to repeat a block of code multiple times, making your code shorter, cleaner, and more efficient.
Instead of writing the same code again and again, loops help automate repetition.
🧠 What is a Loop?
A loop is used when you want to execute a block of code repeatedly until a condition is met.
👉 In simple words:
- Loop = Repeat code automatically
🔁 Types of Loops in Python
Python provides two main types of loops:
- for loop
- while loop
🔢 1. for Loop
The for loop is used to iterate over a sequence like a list, tuple, string, or range.
⚙️ Syntax
for variable in sequence:
# code block🧪 Example: Using range()
for i in range(5):
print(i)Output:
0
1
2
3
4📦 Example: Loop Through a List
fruits = ["Apple", "Banana", "Mango"]
for fruit in fruits:
print(fruit)🔁 2. while Loop
The while loop runs as long as a condition is True.
⚙️ Syntax
while condition:
# code block🧪 Example
i = 1
while i <= 5:
print(i)
i += 1Output:
1
2
3
4
5⚖️ for Loop vs while Loop
| Feature | for Loop | while Loop |
|---|---|---|
| Use case | Fixed repetition | Conditional repetition |
| Control | Sequence-based | Condition-based |
| Best for | Lists, range | Unknown iterations |
🚀 Loop Control Statements
Python provides special statements to control loop behavior.
⛔ break Statement
Stops the loop completely.
for i in range(10):
if i == 5:
break
print(i)⏭️ continue Statement
Skips the current iteration.
for i in range(5):
if i == 2:
continue
print(i)🟡 pass Statement
Acts as a placeholder.
for i in range(5):
pass🔄 Nested Loops
A loop inside another loop.
for i in range(3):
for j in range(2):
print(i, j)🎯 Real-World Example: Multiplication Table
for i in range(1, 6):
print("2 x", i, "=", 2 * i)🛒 Real-World Example: Shopping Cart
items = ["Pen", "Book", "Laptop"]
for item in items:
print("Buying:", item)🔐 Real-World Example: Login Attempts
attempts = 0
while attempts < 3:
password = input("Enter password: ")
if password == "1234":
print("Access granted")
break
else:
print("Wrong password")
attempts += 1📊 Loop Summary
| Loop Type | Purpose |
| for | Repeat over a sequence |
| while | Repeat based on condition |
| break | Stop loop |
| continue | Skip iteration |
| pass | Do nothing placeholder |
🚀 Applications of Loops
Loops are used in:
- Games 🎮
- Data processing 📊
- Web scraping 🌐
- Automation tools 🤖
- File handling 📁
- AI and machine learning 🧠
💡 Best Practices
✔ Use for when working with collections
✔ Use while for condition-based repetition
✔ Avoid infinite loops
✔ Keep loop logic simple
✔ Always update loop variables
⚠️ Common Mistakes
1. Infinite Loop
while True:
print("Hello")2. Missing increment in while loop
i = 1
while i <= 5:
print(i)
# missing i += 13. Wrong indentation
for i in range(5):
print(i)🧾 Conclusion
Loops are essential in Python programming because they allow you to repeat tasks efficiently. By mastering for loops, while loops, and loop control statements, you can build powerful and automated programs.
🎯 Final Thought
Once you understand loops, you can automate almost any repetitive task in programming, making your Python code more powerful and scalable.


0 Comments