Looping in Python¶
Loops allow you to execute a block of code repeatedly. Python has two main types of loops: for and while.
The for Loop¶
The for loop iterates over a sequence (like a list, tuple, string, or range).
# Iterating over a list
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
# Using range()
for i in range(5):
print(i)
The while Loop¶
The while loop continues as long as a condition is true.
count = 0
while count < 5:
print(count)
count += 1
Loop Control Statements¶
break : Exits the loop prematurely
continue : Skips the current iteration and continues with the next
pass : Does nothing (placeholder)
# break example
for i in range(10):
if i == 5:
break
print(i)
# continue example
for i in range(10):
if i % 2 == 0:
continue
print(i)
# pass example
for i in range(5):
if i == 2:
pass # Do nothing
print(i)
Nested Loops¶
You can have loops inside loops.
for i in range(3):
for j in range(2):
print(f"i={i}, j={j}")
Infinite Loops¶
Be careful with while loops to avoid infinite loops.
# This will run forever (don't run this!)
# while True:
# print("Infinite loop")