The for Loop
This lesson highlights the main features of the 'for' loop.
We'll cover the following
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:
- The name of the iterator
- The sequence to be traversed
- 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.