Numbers & operators
Python Basics Lesson 3 3:07 English narration · English + 中文 subtitles burned in
Chapters
Transcript
You do not have to teach Python arithmetic — it already knows.
你不需要教 Python 算术——它本来就会。
Write plus, minus, times, and it answers nine, five and fourteen without being told what those signs mean.
写加号、减号、乘号, 它就直接给出 9、5 和 14,不用你解释这些符号是什么意思。
Then divide seven by two, and look carefully at what comes back: not three, not three and a half written as a fraction, but three point five.
然后用 7 除以 2,仔细看它给回来的结果:不是 3,也不是写成分数的三又二分之一, 而是 3.5。
That dot is about to matter.
这个小数点,马上就会变得重要。
Python keeps two kinds of number apart.
Python 把两种数字分得很清楚。
An integer — int for short — is a whole number, like seven or minus three: no decimal point anywhere.
整数(int)是没有小数点的整数,比如 7 或 -3。
A float has a decimal point, like two point five.
浮点数带有小数点,比如 2.5。
Here is the part that surprises people: a single slash always gives a float.
让很多人意外的是:单斜杠的除法永远给出浮点数。
Four divided by two is not two, it is two point zero.
4 除以 2 不是 2,而是 2.0。
The division sign changes the kind of number, even when the answer is exact.
即使结果正好整除,除号也会改变数字的种类。
Three more operators earn their keep constantly.
还有三个运算符,用处非常大。
Double slash divides and keeps only the whole part, so seven double-slash two is three.
双斜杠做除法但只保留整数部分, 所以 7 // 2 是 3。
Percent gives what is left over instead — seven percent two is one.
百分号给出的是余下的部分——7 % 2 是 1。
And double star raises to a power: two double-star five is thirty-two.
双星号表示乘方:2 ** 5 是 32。
The first two are a pair, and they answer the same everyday question: seven sweets shared between two children is three each, with one left over.
前两个是一对,它们回答的是同一个日常问题: 7 颗糖分给 2 个小孩,每人 3 颗,还剩 1 颗。
When a line has several operators, Python does not simply work left to right.
当一行里有好几个运算符时,Python 并不是简单地从左往右算。
It uses the same order you use in maths: powers first, then times and divide, then plus and minus.
它用的是你在数学课上用的顺序:先乘方,再乘除,最后加减。
So two plus three times four is fourteen — the multiplication happens first, whatever the reading order suggests.
所以 2 + 3 * 4 等于 14——不管读起来是什么顺序,乘法都先做。
Put brackets around the addition and the answer becomes twenty.
给加法加上括号,答案就变成 20。
When a line matters, add the brackets even if you do not need them; the next reader will thank you.
遇到重要的一行,即使不需要也把括号加上; 下一个读代码的人会感谢你。
Now the lesson's second task.
现在做课程里的第二道题。
Seventeen sweets, five children, and two questions: how many each, and how many left.
17 颗糖,5 个小孩,两个问题:每人几颗,还剩几颗。
Store the two numbers first, so the sums read like the sentence.
先把两个数字存起来,这样算式读起来就像那句话。
Then floor division — double slash — answers how many each: three.
然后用整除——双斜杠——回答每人几颗:3。
And percent answers what is left: two.
用百分号回答剩下几颗:2。
Check it the slow way if you like: five children times three sweets is fifteen, and seventeen minus fifteen is two.
你也可以慢慢验算:5 个小孩每人 3 颗是 15,17 减 15 等于 2。
Four things to take with you.
带走四点。
One: a single slash always gives a float, even when the division is exact.
第一:单斜杠永远给出浮点数,即使正好整除。
Two: double slash keeps the whole part and percent gives the remainder — together they split a number cleanly.
第二:双斜杠保留整数部分,百分号给出余数——两者合起来能把一个数干净地拆开。
Three: double star is a power.
第三:双星号是乘方。
Four: brackets beat everything, so use them whenever you are not certain.
第四:括号优先级最高,不确定时就加上。
Now go and do the three tasks; the third one is about precedence, and it is worth getting wrong once.
现在去做那三道题;第三题讲的是运算顺序,值得先做错一次。