Blog

Example of a basic While Loop in Python

python

fruits = [“apple”, “banana”, “cherry”]

# Iterating over the elements in the list
for fruit in fruits:
print(fruit)




In this example, we have a list called fruits that contains three elements: “apple”, “banana”, and “cherry”. We use a for loop to iterate over each element in the list. On each iteration, the variable fruit takes the value of the current element, and we print it out. The output will be:


apple
banana
cherry


Each element in the list is printed on a separate line, demonstrating the iteration over the list using the for loop.