Presumably everyone knows the Python loop, there are a number of Python loop statements, such as for loop, while loop, if, else and so on, today to tell you about the FOR Loop statement.
A For loop statement is a loop control statement in Python, the program is executed in order in the general case. Programming languages provide a variety of control structures that allow for more complex execution paths. Elements within any ordered sequence object can be traversed, such as strings, lists, tuples, and so on, which can iterate over the image. The next tutorial introduces the syntax format for the for loop and the usage and flow of the for loop to help you understand it.
The syntax format for the For loop is as follows :
For Iterating_var in sequence: statements (s)
for loop flow diagram See:
And if the word syntax format and process can be difficult to understand, then let me give some simple examples to illustrate the Python for loop statements.
The example is as follows:
#!/usr/bin/python#-*-Coding:utf-8-*-for letters in ' Python ': # The first instance of print ' Current letter: ', alphabetic fruits = [' banana ' , ' Apple ', ' mango ']for fruit in fruits: # The second instance of print ' current fruit: ', fruit print ' good bye! '
This is a very simple for loop instance, and the result of the output is as follows:
Current Letter: P Current Letter: y current letter: T current letter: H current Letter: o Current letter: n Current fruit: Banana current fruit: Apple current fruit: Mangogood bye!
As shown in the output above.
This article explains the basic and syntax formatting of the Python for Loop, and demonstrates some of its usage examples. The For loop and list sequence are powerful when used together, and you'll find yourself using them in many situations. Hopefully this article will give you a little help in learning about Python.
For more information, please visit the PHP Chinese Web Python tutorial section.