Python中的For和While循环

In python there are two types of loops: 

在python中,有两种类型的循环:

  • While loop

    While循环
  • For loop

    对于循环

()

()

While循环 (The While Loop)

Let us try to understand the logic of the while loop. Let's say a person hits a target using his bow till all the arrows are used up. In this case, as long as the container has an arrow in it, the person will take the arrow and hit the target using his bow.

让我们尝试理解while循环的逻辑。 假设一个人用弓箭击中目标,直到所有箭头都用完。 在这种情况下,只要容器中装有箭头,该人就会抓住箭头并用弓箭击中目标。

Let's assume the number of arrows in the container is 10. The target can be shot as long as the number of arrows is greater than zero. The final step would be to put the bow down when all the arrows have been shot.  So basically, as long as the arrows are present in the container, the loop of hitting the target using the bow will continue.  The logic of the while loop is: as long as the condition is true the loop continues. 

假设容器中的箭头数为10。只要箭头数大于零,就可以射击目标。 最后一步是当所有的箭都射完后放下弓箭。 因此,基本上,只要箭头存在于容器中,用弓箭击中目标的循环就会继续。 while循环的逻辑是:只要条件为真,循环就会继续。

Format of while loop in Python:

Python中while循环的格式:

while <<expression>>:
    statements 
    ....
    ....

The while loop starts with the keyword "while" as shown in the above snippet. In the expression, we can use any condition that we want to make it true till we want the loop to be executed—followed by a colon.  Next, there could be one or more statements, but a tab is required before any statement. There is one important thing we need to keep in mind: if in any case the expression never becomes false, then the while loop will never stop, making it an "infinite loop". In this case, the program will never end.

如上面的代码片段所示,while循环以关键字“ while”开始。 在表达式中,我们可以使用要使其变为真的任何条件,直到我们希望执行循环为止(以冒号开头)。 接下来,可能有一个或多个语句,但是在任何语句之前都需要一个制表符。 我们需要牢记一件事:如果在任何情况下表达式都不会变为假,则while循环将永远不会停止,从而使其成为“无限循环”。 在这种情况下,程序将永远不会结束。

Now let us convert the example into a proper Python program. 

现在,让我们将示例转换为适当的Python程序。

no_of_arrows = 10
while no_of_arrows > 0 :
	no_of_arrows = no_of_arrows -1
	print ("Number of arrows left in the container" + str(no_of_arrows))
print ("All arrows in the container is over and while loop completed")

Here is the output we get:

这是我们得到的输出:

>>> 
============ RESTART: C:/Users/swadhin.ray/Desktop/While_loop.py ============
Number of arrows left in the container 9
Number of arrows left in the container 8
Number of arrows left in the container 7
Number of arrows left in the container 6
Number of arrows left in the container 5
Number of arrows left in the container 4
Number of arrows left in the container 3
Number of arrows left in the container 2
Number of arrows left in the container 1
Number of arrows left in the container 0
All arrows in the container is over and while loop completed
>>> 

The Python program automatically determines the end of the "while" when the Python interpreter finds a statement that is not tabbed. We don't have to explicitly define it. 

当Python解释器找到未制表的语句时,Python程序会自动确定“ while”的结尾。 我们不必明确定义它。

For循环: (The For Loop:)

When any string or list is iterated in sequence, this requires a for loop. 

当按顺序迭代任何字符串或列表时,这需要一个for循环。

Here is a simple example to list the letters of a word using a for loop in python.

这是一个使用python中的for循环列出单词字母的简单示例。

for x in 'apple':
     print ('Lits of letters in apple :',x)
print ("Apple letter over!")

The result will be as shown below:

结果将如下所示:

============= RESTART: C:/Users/swadhin.ray/Desktop/for_loop.py =============
Lits of letters in apple : a
Lits of letters in apple : p
Lits of letters in apple : p
Lits of letters in apple : l
Lits of letters in apple : e
Apple letter over!
>>> 

Now say for example we have a list which has a few names and we want to loop it.

现在说,例如,我们有一个包含几个名称的列表,我们想循环它。

names = ['Ray', 'Mike',  'David']
for j in names:        
   print ( j , ' is a name')

The above code will print the below result or output:

上面的代码将打印以下结果或输出:

============= RESTART: C:/Users/swadhin.ray/Desktop/for_loop.py =============
Ray  is a name
Mike  is a name
David  is a name
>>> 

Thank you for reading my article. Please feel free to leave me some feedback or to suggest any future topics. Please give my article a thumbs up if you liked it.

感谢您阅读我的文章。 请随时给我一些反馈或建议任何将来的主题。 如果喜欢的话,请给我的文章点个赞。

Looking forward to hearing from you - Swadhin Ray (Sloba) - ( LinkedIn ) ( Twitter )

期待您的回音-Swadhin Ray(Sloba)-( LinkedIn )( Twitter

翻译自: https://www.experts-exchange.com/articles/28468/For-and-While-Loops-in-Python.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值