Imagine you have to write "Hello, Andhra Pradesh!" 10 times. You could write the same line of code 10 times, or you could use a loop! A loop is a way to tell the computer to repeat an action, saving you a lot of work.
Story: Loops are for efficiency. Instead of writing the same code again and again, you write it once and tell the computer how many times to repeat it.
Analogy: A farmer telling a worker to water 50 plants, instead of saying "water this plant," "water this plant," 50 times.
Story: A `for` loop is perfect when you know exactly how many times you want to do something.
The Code: for i in range(5): print("This will print 5 times")
Story: A `while` loop is used when you want to repeat an action as long as a certain condition is true. It stops when the condition becomes false.
Analogy: A worker who is told to keep packing mangoes while there are still mangoes in the basket.
# A for loop to print numbers 0 to 4
print("For loop example:")
for i in range(5):
print(i)
# A while loop to count up to 3
print("
While loop example:")
count = 0
while count < 3:
print("Count is:", count)
count = count + 1
If you want to print the names of all 50 students in a class, which loop is better to use?