Simplify My Life
A loop in computer science usually refers to an action that is repeated until a certain condition is met.
Loops allow us to do repetitive tasks with little effort.
There are many different types of loops and each has has slightly different behavior:
The first use case for a loop is to go through all the elements in a list with little effort.
cars = ['bmw', 'audi', 'toyota', 'subaru']
# Print each car in its own line.
for car in cars:
print(car)
audi
bmw
subaru
toyota
When we use a loop to go through a list python reads the initiation of the loop.
cars = ['bmw', 'audi', 'toyota', 'chevy']
for car in cars
print(car)
bmw
audi
toyota
chevy