I believe everyone knows about python loop statements. There are many kinds of python loop statements, such as for loop, while loop, if, else, etc. Today I will tell you about for loop statements. The for loop statement is a loop control statement in python. The program is generally executed in order. Programming languages provide various control structures that allow more complex execution paths. The elements in any ordered sequence object can be traversed, such as strings, lists, tuples and other iterable objects. The following tutorial will introduce the syntax format of the for loop as well as the usage and process of the for loop to help everyone understand.
The syntax format of the for loop is as follows:
for iterating_var in sequence: statements(s)
The process of the for loopSee the picture below:
It may be difficult to understand if we just talk about the grammatical format and process. Next, I will give some simple examples to illustrate the for loop statement in Python.
The example cited is as follows:
#!/usr/bin/python # -*- coding: UTF-8 -*- for letter in 'Python': # 第一个实例 print '当前字母 :', letter fruits = ['banana', 'apple', 'mango'] for fruit in fruits: # 第二个实例 print '当前水果 :', fruit print "Good bye!"
This is a very simple for loop example, and the output result is also as shown below:
当前字母 : P 当前字母 : y 当前字母 : t 当前字母 : h 当前字母 : o 当前字母 : n 当前水果 : banana 当前水果 : apple 当前水果 : mango Good bye!
As shown in the above output The results are shown.
This article explains the basics and syntax format of Python for loop, and demonstrates some examples of its use. For loops and list sequences are powerful when used together, and you'll find yourself using them in many situations. I hope this article can bring some help to you who are learning python.
For more related knowledge, please visit the Python tutorial column on the php Chinese website.
The above is the detailed content of python for loop statement structure and usage examples (picture). For more information, please follow other related articles on the PHP Chinese website!