The execution process of the python for statement: 1. Iterate through the sequence index, and the traversal method of executing the loop is through the index; 2. The statement in for is no different from the ordinary one, and the statement in else will be executed normally in the loop Executed when completed.
Related free learning recommendations: python video tutorial
Execution process of python for statement:
Python for loop can traverse any sequence of items, such as a list or a string.
Grammar:
The syntax format of for loop is as follows:
1 2 |
|
Flow chart:
Example:
1 2 3 4 5 6 7 8 9 10 11 |
|
Output result of the above example:
1 2 3 4 5 6 7 8 9 10 |
|
Iterate through sequence index
Another execution loop The traversal method is through index, as shown in the following example:
1 2 3 4 5 6 7 8 |
|
The output result of the above example:
1 2 3 4 |
|
In the above example, we used the built-in functions len() and range(), and the function len() returns a list The length, that is, the number of elements. range returns a sequence of numbers.
Loop using else statements
In python, for...else means this. The statements in for are no different from ordinary ones. The statements in else will be in It is executed when the loop is executed normally (that is, for is not interrupted by break), and the same is true for while...else.
Example
1 2 3 4 5 6 7 8 9 10 11 |
|
Output result of the above example:
1 2 3 4 5 6 7 8 9 10 |
|
The above is the detailed content of What is the execution process of python for statement. For more information, please follow other related articles on the PHP Chinese website!