Iteration and For-Each Loops

Iteration is a repeating portion of an algorithm. Iteration repeats a specified number of times, or until a given condition is met. If we want to ask the…

Iteration
Iteration is a repeating portion of an algorithm. Iteration repeats a specified number of times, or until a given condition is met.
If we want to ask the user to enter 5 numbers and sum them, it is repetitive to do this:
For example, if we want to print a message 5 times:
for loops
Iteration loops are frequently referred to as for loops, because for is the keyword that is used to introduce them in nearly all programming languages, including Python.
Python's for loop iterates over items of a sequence (for example a list of numbers, or a string, which is a sequence of characters) and processes them with some code.
Lists
A list defines a sequence of ordered objects (integers, floats, strings, and so on). They can be defined with comma-separated values between square brackets. We will discuss lists further in the next lecture.
Looping over a list
The loop variable takes the value of each ITEM in turn. It is not a position number: on the first pass it IS the first item, on the second pass it IS the second item, and so on.
For-each over a string
Since a string is a sequence of characters, a for loop can be used to iterate over the characters.