LM | Loops |
Loops
Loops let you execute code repeatedly. In Python, the main loop types are:
- for loops: Iterate over a sequence (like a list or string)
- while loops: Repeat as long as a condition is True
Loops are essential for automating repetitive tasks and processing data efficiently.
Examples
# for loop example
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
# while loop example
count = 0
while count < 3:
print("Counting:", count)
count += 1
Loops make your code more powerful and allow you to work with collections of data easily.