The for Loop

This lesson highlights the main features of the 'for' loop.

A for loop uses an iterator to traverse a sequence, e.g. a range of numbers, the elements of a list, etc. In simple terms, the iterator is a variable that goes through the list.

The iterator starts from the beginning of the sequence. In each iteration, the iterator updates to the next value in the sequence.

The loop ends when the iterator reaches the end.

Structure

In a for loop, we need to define three main things:

  1. The name of the iterator
  2. The sequence to be traversed
  3. The set of operations to perform

The loop always begins with the for keyword. The body of the loop is indented to the right:

Get hands-on with 1200+ tech skills courses.