Python For Loops
A for loop is used for iterating over a sequence.
The for Loop
A for loop is used for iterating over a sequence (a list, tuple, string, dictionary, set, or range). This is less like the for keyword in other programming languages and works more like an iterator method found in other object-oriented programming languages.
Looping Through a String
Even strings are iterable objects — they contain a sequence of characters:
The range() Function
To loop a set number of times, use the range() function. It returns a sequence of numbers, starting from 0 by default:
range(6) gives values 0, 1, 2, 3, 4, 5 — it does not include the number 6 itself.
The range() function has two more parameters — start value and step:
Nested Loops
A nested loop is a loop inside a loop. The "inner loop" will be executed one time for each iteration of the "outer loop":