A for loop uses an iterator to traverse a sequence, such as a range of numbers, the elements of a list, or the characters in a string. In simple terms, the iterator is a variable that goes through the list. The iterator starts at the beginning of the sequence and ends when it 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 1400+ tech skills courses.