sales = [1000, 2000, 1500, 3000]
total = 0
for amount in sales:
total += amount
print(total)
Output:
7500🔹 11. Common Mistakes
❌ Forgetting to update the loop variable
count = 1
while count <= 5:
print(count)
This creates an infinite loop because
count never changes.Correct:
count = 1
while count <= 5:
print(count)
count += 1
🎯 Practice Questions
1. Print numbers from 1 to 10 using a "for" loop
2. Print even numbers from 2 to 20
3. Find the sum of numbers from 1 to 100
4. Print all elements of a list using a loop
5. Create a multiplication table of any number using a "for" loop
🎯 Key Takeaways
✅ Use a "for" loop when the number of iterations is known
✅ Use a "while" loop when the number of iterations depends on a condition
✅ range() generates sequences of numbers
✅ break exits the loop immediately
✅ continue skips the current iteration
✅ pass acts as a placeholder
Loops are one of the most important concepts in Python. You'll use them extensively for data processing, feature engineering, machine learning, automation, and solving coding interview questions.
Double Tap ❤️ For Part-6