Counting loops
Python Basics Lesson 7 3:03 English narration · English + 中文 subtitles burned in
Chapters
Transcript
To print the numbers one to five you could write it five times.
要打印 1 到 5,你可以写五遍。
It works.
这样也行。
Now print one to five hundred, and the idea collapses — nobody types five hundred lines, and nobody can check them.
但如果要打印 1 到 500,这个办法就垮了——没有人会敲 500 行,也没有人检查得过来。
A loop says the thing once and tells Python how many times to do it.
循环把这件事说一次,然后告诉 Python 做多少遍。
Two lines here handle five numbers, five hundred, or five million, with no more typing.
这里两行代码,处理 5 个数、500 个数,甚至 500 万个数,都不用多打一个字。
A for line has four parts and they are always in this order.
一行 for 有四个部分,顺序永远是这样。
The word for names the loop variable that follows — here it is i, and it holds a different value on every pass.
for 这个词后面跟的是循环变量的名字——这里是 i,它在每一轮里取到不同的值。
The word in comes next, then what to walk through.
接着是 in,然后是要遍历的东西。
Then a colon, and underneath it the indented body of the loop: the lines that run once per pass.
再是一个冒号,下面缩进的就是循环体:每一轮都会执行的那些行。
Same colon, same indentation you learned for if.
和你学 if 时一样的冒号,一样的缩进。
Most counting loops walk through a range.
大多数计数循环遍历的是一个 range。
A range of five gives you zero, one, two, three, four — five numbers, starting at zero, and five itself never appears.
range(5) 给你 0、1、2、3、4—— 五个数,从 0 开始,而 5 本身永远不会出现。
Give it a start and a stop instead and you get one, two, three, four, five.
改成给出起点和终点,你得到 1、2、3、4、5。
Add a third number and it becomes a step: zero, ten, two gives the even numbers up to eight.
再加第三个数字,它就是步长:range(0, 10, 2) 给出到 8 为止的偶数。
Hold on to one rule and the rest follows: the stop value is never produced.
记住一条规则,其余都能推出来:终点值永远不会被产生。
Here is the pattern you will use more than any other: the accumulator.
下面这个模式,你用到的次数会超过其他任何一个:累加器。
A variable that collects a result as the loop runs.
它是一个变量,在循环运行的过程中收集结果。
It has to exist before the loop starts, so set it to zero on the line above — inside the loop it would be reset every pass and never grow.
它必须在循环开始之前就存在,所以要在上面那一行把它设成 0—— 如果放在循环里面,它每一轮都会被清零,永远也长不起来。
Then each pass adds the current value to it.
然后每一轮把当前的值加到它上面。
Watch the total after each pass: one, three, six, ten, and finally fifteen.
看每一轮之后的 total:1、3、6、10,最后是 15。
That table is exactly what an exam asks you to write out.
这张表,正是考试要求你手写出来的东西。
Now the lesson's second task: add up every whole number from one to a hundred.
现在做课程里的第二道题:把 1 到 100 的所有整数加起来。
Same three lines.
还是那三行。
Start the total at zero, loop over the numbers, add each one.
total 从 0 开始,遍历这些数,把每一个加上去。
The only thing to be careful about is the stop value: to include a hundred you must write one hundred and one, because the stop is never produced.
唯一要小心的是终点值:要包含 100,你必须写 101,因为终点永远不会被产生。
Run it and the total is five thousand and fifty.
运行以后,总和是 5050。
Four things to take with you.
带走四点。
One: a for loop repeats a fixed number of times, and its body is indented under a colon.
第一:for 循环重复固定的次数,循环体缩进在冒号下面。
Two: range stops before its stop value — the single most common off-by-one in this course.
第二:range 在终点值之前停下——这是本课程里最常见的差一错误。
Three: the loop variable changes on every pass, so use it inside the body.
第三:循环变量每一轮都会改变,所以要在循环体里用它。
Four: an accumulator starts before the loop and grows inside it.
第四:累加器在循环之前初始化,在循环里面增长。
Now do the three tasks; the third builds a product, so think about what it must start at.
现在去做那三道题;第三题求的是乘积,想一想它应该从几开始。