Learning Guides
Loops in Python: For and While Loops Explained
Quick answer: Learn Python for and while loops, including range, break, continue, else clauses on loops, and nested loops, with practical examples.
For Loops
A for loop iterates over a sequence, such as a list, string or range of numbers.
fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
print(fruit)
Using range()
for i in range(5):
print(i) # 0 1 2 3 4
for i in range(2, 10, 2):
print(i) # 2 4 6 8
While Loops
A while loop repeats as long as a condition remains true.
count = 0
while count < 5:
print(count)
count += 1
Forgetting to update the loop variable is the most common cause of an infinite loop.
Break and Continue
for num in range(10):
if num == 5:
break # exits the loop entirely
print(num)
for num in range(10):
if num % 2 == 0:
continue # skips to the next iteration
print(num) # prints only odd numbers
The else Clause on a Loop
A loop's else block runs only if the loop completes normally, without hitting a break.
for num in [2, 4, 6, 8]:
if num % 2 != 0:
print("Found an odd number")
break
else:
print("All numbers are even") # this runs, since no break occurred
Nested Loops
for i in range(3):
for j in range(2):
print(i, j)
Looping with enumerate() and zip()
fruits = ['apple', 'banana', 'cherry']
for index, fruit in enumerate(fruits):
print(index, fruit)
prices = [50, 30, 80]
for fruit, price in zip(fruits, prices):
print(fruit, price)
For vs While: Which to Use
Use a for loop when you know in advance what you are iterating over, such as a list or a fixed range. Use a while loop when the number of iterations depends on a condition that can only be checked at runtime.
Common Interview Questions
What is the difference between break and continue?
break exits the loop entirely. continue skips only the current iteration and moves to the next one.
When does a loop's else clause execute?
Only when the loop finishes all its iterations without encountering a break statement.
How do you avoid an infinite while loop?
Ensure the condition being checked is actually updated inside the loop body, so it will eventually become false.
FAQ
Frequently Asked Questions
What is the difference between a for loop and a while loop in Python?
A for loop iterates over a known sequence such as a list or range. A while loop repeats based on a condition and is used when the number of iterations is not known in advance.
What does break do in a Python loop?
It immediately exits the loop entirely, skipping any remaining iterations.
What does continue do in a Python loop?
It skips the rest of the current iteration and moves on to the next one, without exiting the loop.
What causes an infinite loop in Python?
A while loop whose condition never becomes false, usually because the loop variable is not updated correctly inside the loop body.
Keep Reading
Related Articles
Learning Guides
INNER JOIN in SQL with Examples: Complete Beginner's Guide
Learn INNER JOIN in SQL with practical examples, syntax, interview questions, and real-world use cases. A complete beginner-friendly SQL JOI
Learning Guides
OUTER JOIN in SQL: Complete Guide with Examples for Beginners
Master OUTER JOIN in SQL with practical examples and real-world scenarios. Learn LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, syntax, use cases,
Learning Guides
Multi-Row Functions in SQL: Complete Guide with Examples
Learn multi-row (aggregate) functions in SQL including SUM, AVG, COUNT, MIN, MAX, GROUP BY and HAVING, with practical examples, NULL handlin