Python学习笔记(十二)- while 和 for 循环(while and for Loops)

1.while 和 for 主要的功能区别是什么?
答:while 循环是通用的循环语句,而for被设计用来在序列(sequence)或者其它可迭代对象(iterable)迭代项。尽管 while 可以用计数器循环模仿 for,但是它会产生更多代码而且可能运行的更慢。

2.break 和 continue 的区别是什么?
答:break 语句立即退出循环(结束 while 或者 for 循环语句下的所有代码),而 continue 跳回循环的顶部(在 while 中的测试【test】之前或在 for 中的下一个项获取前收起位置【PS.尽力了 翻不懂~ 贴在下面 难道是 continue 后面语句跳过】)

【you wind up positioned just before the test in while or the next item fetch in for】

3.循环的 else 从句什么时候被执行?
答:在 while 或者 for 循环中的 else 从句,当循环退出后,被执行一次,如果循环正常退出(就是说没有使用 break 语句强制退出的情况下)。break 语句立即退出循环,如果遇到 else 语句,会跳过 else 的部分。

4.如何用 Python 编写基于计数器(counter-based)的循环?
答:计数器循环可以由 while 语句编写,手动地追踪索引(index),或者在 for 循环中,用 range 内建函数生成连续整型偏移量(integer index)。如果您需要简单地迭代序列中的所有项,则这两种方法都不是 Python 中的首选工作方式。取而代之,无论什么可能的时候,用简单的 for 循环代替,不用 range 函数或者计数器;它更加容易编写而且更快地运行。【for i in iterable】

5.在 for 循环中,range 可以被用来干什么?
答:range 内建函数可以被用在 for 中来实现固定次数的重复(repitition),按偏移量遍历,而不是以偏移项进行遍历,可以在执行过程中跳过连续项,并在遍历列表时更改列表。但是不是所有这些角色需要由 range 扮演,大多数情况有可供选择的替代品——遍历真实的项,三限切片(three-limit slice)和列表推导式是如今更好地解决方法(尽管前C语言程序员很自然地想要数一数)。

标注:转载《Learning Python 5th Edition》[奥莱理]
1. What are the main functional differences between a while and a for?
2. What’s the difference between break and continue?
3. When is a loop’s else clause executed?
4. How can you code a counter-based loop in Python?
5. What can a range be used for in a for loop?

1. The while loop is a general looping statement, but the for is designed to iterate across items in a sequence or other iterable. Although the while can imitate the for with counter loops, it takes more code and might run slower.
2. The break statement exits a loop immediately (you wind up below the entire while or for loop statement), and continue jumps back to the top of the loop (you wind up positioned just before the test in while or the next item fetch in for).
3. The else clause in a while or for loop will be run once as the loop is exiting, if the loop exits normally (without running into a break statement). A break exits the loop immediately, skipping the else part on the way out (if there is one).
4. Counter loops can be coded with a while statement that keeps track of the index manually, or with a for loop that uses the range built-in function to generate successive integer offsets. Neither is the preferred way to work in Python, if you need to simply step across all the items in a sequence. Instead, use a simple for loop instead, without range or counters, whenever possible; it will be easier to code and usually quicker to run.
5. The range built-in can be used in a for to implement a fixed number of repetitions, to scan by offsets instead of items at offsets, to skip successive items as you go, and to change a list while stepping across it. None of these roles requires range, and most have alternatives—scanning actual items, three-limit slices, and list comprehensions are often better solutions today (despite the natural inclinations of ex–C programmers to want to count things!).

猜你喜欢

转载自blog.csdn.net/Enderman_xiaohei/article/details/87516369