Looping Through Lists

Simplify My Life

Selection Sort

Loop

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:

  • For
  • While
  • Do
  • Do While

Looping Through a List

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
          

Looping In-Depth

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