Basic tutorial for getting started with Python (learningPython) -- 4.2Python's count loop body for statement. Another type of loop body structure in Python is the counting loop body for. some statement blocks are executed for a certain number of times through the for loop. the syntax structure is as follows. Another type of loop body structure of Python is the count loop body for. some statement blocks are executed for a certain number of times through the for loop. the syntax structure is as follows.
The idea of the for loop application in Python is the same as that in other advanced languages such as C. When the for condition is met, the statement block in the for statement is executed, the difference is that the for condition writing method is somewhat different from other advanced languages.
[Python]View plaincopy
- For variable in [value1, value2,...]:
- (TAB) statement
- (TAB) statement
- (TAB) etc.
Here, the left square brackets '[' and right square brackets ']' appear for the first time. the data sequence enclosed by left and right square brackets is called the list, for more information about the list, see.
Note that [value1, value2,...] must be followed by a colon:; otherwise, a syntax error occurs.
The for loop principle is as follows: how many times does a loop take a value valuex from the list behind in and assign a value to the variable behind? A list contains several data loops several times. when all the data in the list is finished, the for operation ends. Therefore, the number of for executions depends on the number of data in the list, the following is an example.
[Python]View plaincopy
- Def main ():
- Print ('I will display the numbers 1 through 5 .')
- For num in [1, 2, 3, 4, 5]:
- Print (num)
- # Call the main function.
- Main ()
Line 2 of the code is a for loop. each time a value is obtained from the list [3rd, 5] and assigned to num, the list ([1, 2, 3, 4, 5]) There are 5 data records. we can see that for can be completed after 5 cycles.
The running result is as follows:
Then, let's analyze why the results are like this?
Each loop for will extract a data from [1, 2, 3, 4, 5] to num.
Explain implements block loop execution of some statements for a certain number of times. The syntax structure is as follows. For loop application of Py thon...