For loop is used to iterate over given sequence. Sequence can be a list, dictionary or similar enumerable object for python programming language. For loop is a bit different from other languages like C, C++, Java … For loop in python mainly uses object to iterate but in other languages counting and statements are used mainly.
For循环用于遍历给定序列。 Sequence可以是python编程语言的列表,字典或类似的可枚举对象。 For循环与C,C ++,Java等其他语言有点不同……python中的For循环主要使用对象进行迭代,但在其他语言中,计数和语句则主要使用。
句法 (Syntax)
Syntax of for loop is like below which consist of for
keyword an item, in
keyword and iterable object
which ends with double point :
for循环的语法如下所示,它由for
关键字一项, in
关键字和以double point :
结尾的iterable object
组成double point :
After the first line of for loop the loop body starts and which is expressed with indentation.
在for循环的第一行之后,循环主体开始,并以缩进形式表示。
for item in iterable_object:
print item
In the first step first value from iterable_object is assigned into item and the body of the for loop is executed. print item
is for loop body. For loop body can be more than one line. For each step this operation will be done iteratively by assigning next value from iterable object.
在第一步中,将来自iterable_object的第一个值分配给item并执行for循环的主体。 print item
用于循环主体。 对于循环体,可以超过一行。 对于每个步骤,将通过从可迭代对象分配下一个值来迭代完成此操作。
与For循环 (Loop with For)
Now we have simply looked syntax of for loop in previous part. But the best way to learn for loop is running examples. In this part we will run simple but useful example.
现在,我们仅在上一部分中介绍了for循环的语法。 但是学习循环的最好方法是运行示例。 在这一部分中,我们将运行简单但有用的示例。
In this example we will provide a list which consist of numbers from to
9
into a for loop and print this numbers to the screen.
在此示例中,我们将提供一个列表,其中包含来自到
9
进入for循环并将此数字打印到屏幕上。
mylist=[0,1,2,3,4,5,6,7,8,9]
for item in mylist:
print(item)
带范围循环(Loop With Range)
Previous part we have used a list which is already created explicitly to iterate. But the problem is how can we cope in a situation where we will iterate from to
100.000
. Creating a list manually is just a joke. In this situations we can use range
function which will create a list for given range. The most readable usage is providing start and end numbers to the range
function.
上一部分,我们使用了已显式创建的列表进行迭代。 但是问题是我们要如何应对这种情况到
100.000
。 手动创建列表只是一个玩笑。 在这种情况下,我们可以使用range
函数,它将为给定范围创建一个列表。 最易读的用法是为range
函数提供开始和结束编号。
In this example we will print numbers from to
100
with range
function in a for loop.
在此示例中,我们将从for循环中将
range
功能设置为100
。
for item in range(0,100):
print(item)
设定范围步骤(Set Range Steps)
In previous part we started loop from 0 and incremented in each step one by one upto 100. Increasing one by one is not ideal for some situations. We can specify the increase value into the range function.
在上一部分中,我们从0开始循环,并在每一步中逐一递增到100。在某些情况下,逐一递增并不理想。 我们可以在范围函数中指定增加值。
In this example we will increase the loop with 2
by providing third argument into range
function like below.
在此示例中,我们将通过向range
函数中提供第三个参数来增加2
的循环,如下所示。
for item in range(0,100,2):
print(item)
嵌套循环(Nested For Loop)
Up to now we have used single for loop for iteration. But in real world situations we may need multiple for loops nested together. Matrixes are one of the most used are of nested loops where x and y coordinates are iterated in a nested manner. Nested loop is not different from normal loop we will just provide new for loop in the body block of the another for loop.
到目前为止,我们已经使用single for循环进行迭代。 但是在现实世界中,我们可能需要嵌套在一起的多个for循环。 矩阵是嵌套循环中使用最多的矩阵之一,其中以嵌套方式迭代x和y坐标。 嵌套循环与普通循环没有什么不同,我们将在另一个for循环的主体块中提供新的for循环。
x=[1,2,3]
y=[1,2,3]
for a in x:
for b in y:
print(a,b)
打破循环(Break For Loop)
Starting a for loop will end after all elements are iterated. This is the most used scenario but there are some exceptions. In some situations we may want to break the loop if a specified condition is met. We can stop and exit from loop by using break
keyword.
在迭代所有元素之后,开始for循环将结束。 这是最常用的方案,但是有一些例外。 在某些情况下,如果满足指定条件,我们可能希望中断循环。 我们可以使用break
关键字停止和退出循环。
In this example we will look if the square root of the var
and if it is above 20 we will stop and exit from for loop.
在此示例中,我们将查看var
平方根,如果大于20,我们将停止并退出for循环。
mylist=[3,2,1,5,4,2]
for var in mylist:
if(var**2>20):
break
print(var)
跳过当前步/迭代(Skip Current Step/Iteration)
Another useful feature is skipping current into next step without running current step. We can use continue
keyword to iterate next step. This will prevent executing for loop body part after continue
keyword.
另一个有用的功能是在不运行当前步骤的情况下将电流跳过到下一步。 我们可以使用continue
关键字来迭代下一步。 这将防止在执行continue
关键字后执行for循环主体部分。
We will use previous example but only skip next iteration if the var
square root is bigger than 20 .
我们将使用前面的示例,但是如果var
square root大于20,则仅跳过下一个迭代。
mylist=[3,2,1,5,4,2]
for var in mylist:
if(var**2>20):
continue
print(var)
循环/迭代字典(Loop/Iterate Dictionary)
Another iterable type of python is dictionaries. We can iterate over a dictionary like a list and use both the key and value parts specifying as two items in a for loop.
python的另一种可迭代类型是字典。 我们可以遍历列表之类的字典,并使用指定为for循环中两项的键和值部分。
We will extract both key and value pairs from dictionary named mydict
by using items
function and set them variables named key
and value
我们将使用items
函数从名为mydict
的字典中提取键和值对,并将它们设置为名为key
和value
变量
mydict={'a':1,'b':2,'c':3}
for key,value in mydict.items():
print(key,value)
对于其他(For Else)
Python provides decision making mechanisms with if-else keywords. For loops also provides else which can be used to detect break
operation. As stated in break
section break will finish for loop. If we need to run some code after finishing loop without break we can add else
condition.
Python提供了具有if-else关键字的决策机制。 For循环还提供了其他可用于检测break
操作的循环。 如break
部分所述, break
将完成循环。 如果需要在结束循环后不中断地运行一些代码,则可以添加else
条件。
In this example we will print Loop finished
if break is not fired.
在此示例中,如果未触发break,我们将打印Loop finished
looped。
mylist=[3,2,1,5,4,2]
for var in mylist:
if(var**2>100):
break
print(var)
else:
print("Loop finished")
翻译自: https://www.poftut.com/python-for-loop-tutorial-with-examples-and-rangexrange-functions/