In Python, there are two main types of loops: for
loops and while
loops. These loops allow you to execute a block of code multiple times based on a certain condition.
for
loops: This type of loop allows you to iterate over a sequence (such as a list, tuple, or string) and execute a block of code for each item in the sequence.
1 2 3 4 |
fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit) |
Output:
1 2 3 4 |
apple banana cherry |
while
loops: This type of loop allows you to execute a block of code repeatedly as long as a certain condition is true.
1 2 3 4 5 |
count = 0 while count < 5: print(count) count += 1 |
Output:
1 2 3 4 5 6 |
0 1 2 3 4 |