What is a Loop?
A loop is like telling the computer:
"Hey, repeat this thing again and again until I tell you to stop."
Types of Loops in Python
There are two main types:
1. for loop — when you know how many times you want to repeat something.
2. while loop — when you don't know how many times, and you want to keep going
while a condition is true.
Example 1: for loop (counting specific times)
Suppose you want to say "Hello" 5 times.
for i in range(5):
print("Hello")
What happens here?
• range(5) means numbers from 0 to 4 (5 times total)
• i takes each number in that range
• print("Hello") runs 5 times
while loop (repeat until condition is false)
i=1 # Start with 1
while i <= 5: # Keep looping while i is less than or equal to 5
print("Hello") # Print Hello
i=i+1 # Increase i by 1 each timeAdd 1 to i
Repeat until i is no longer less than or equal to 5
Loop with list
Print all the fruits in a basket.
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)