Python for Loop Statement
Python for loops can iterate over items of any sequence, such as a list or a string.
Syntax:
The syntax format of the for loop is as follows:
for iterating_var in sequence: statements(s)
Flowchart:

Example:
Examples
Try it yourself »
The output of the above example is:
当前字母: P 当前字母: y 当前字母: t 当前字母: h 当前字母: o 当前字母: n 当前水果: banana 当前水果: apple 当前水果: mango Good bye!
Iterating by Sequence Index
Another way to perform loop traversal is by index, as in the following example:
Example
The output of the above example is:
当前水果 : banana 当前水果 : apple 当前水果 : mango Good bye!
In the above example we used the built-in functions len() and range(). The len() function returns the length of the list, i.e., the number of elements. range() returns a sequence of numbers.
Using else Statement with Loop
In Python, for...else means the following: the statements in the for loop are no different from ordinary ones, and the statements in the else branch will be executed when the loop completes normally (i.e., the for loop is not interrupted by a break). The same applies to while...else.
Example
Try it yourself »
The output of the above example is:
10 等于 2 * 5 11 是一个质数 12 等于 2 * 6 13 是一个质数 14 等于 2 * 7 15 等于 3 * 5 16 等于 2 * 8 17 是一个质数 18 等于 2 * 9 19 是一个质数
More examples:Python printing diamond, triangle, rectangle
Other extensions