Skip to content

Variables and expressions

Python for AP CS Principles Lesson 4 2:11 English narration · English + 中文 subtitles burned in

space play · ←/→ 5s · j/l 10s · f fullscreen · ,/. speed

Chapters

Transcript
A variable stores a value so you can use it later, and the equals sign does not mean equals — it means "store into". 变量把一个值存起来,好让你之后用得上, 而那个等号不是"等于"的意思——它的意思是"存进去"。
Which is why the second line here confuses everyone: score equals score plus five. 这也正是这里第二行让所有人犯迷糊的原因: score = score + 5。
It is not an equation. 它不是一个方程。
The computer works out the right side first, giving fifteen, and only then does it store it on the left. 计算机先把右边算出来,得到 15, 然后才把它存到左边去。
Two moments, not one. 是两个时刻,不是一个。
Plus, minus, times and divide behave as you expect. 加、减、乘、除都跟你想的一样。
Two more matter for the exam, and they come off the same picture. 还有两个对考试很要紧,而它们出自同一张图。
Take seventeen things and group them in fives. 拿 17 个东西,每 5 个一组。
Double slash gives you three — the whole fives that fit. 双斜杠给你 3——能装下的整组的 5。
Percent gives you two — what is left over. 百分号给你 2——剩下来的那些。
And a double star is a power: two to the power three is eight. 而双星号是幂:2 的 3 次方是 8。
Relational operators compare two values and hand back a Boolean — True or False. 关系运算符比较两个值,然后交还一个布尔值——True 或 False。
Five double-equals five is True. 5 == 5 是 True。
Five not-equals three is True. 5 != 3 是 True。
Seven less than two is False. 7 < 2 是 False。
Then logical operators combine them: and is True only when both sides are, or needs at least one, and not flips it. 然后逻辑运算符把它们组合起来: and 只有两边都真才真,or 至少一边真就行, 而 not 把它翻过来。
One thing that catches people out: the AP exam does not use Python. 有一件事常常把人绊倒:AP 考试不用 Python。
It uses its own pseudocode. 它用自己的一套伪代码。
The ideas are identical, only the writing changes. 道理完全一样,只是写法变了。
Assignment is a left arrow. 赋值是一个向左的箭头。
Remainder is written MOD. 取余数写作 MOD。
Printing is DISPLAY. 打印是 DISPLAY。
And watch the comparison — on the exam a single equals sign compares, because the arrow does the storing. 再看那个比较——在考卷上,单个等号是"比较", 因为存值那件事是箭头在做。
Four things to take with you. 带走四点。
One: assignment works out the right side, then stores it. 第一:赋值先算右边,然后把它存起来。
Two: double slash is whole division and percent is the remainder. 第二:双斜杠是整除,百分号是余数。
Three: a comparison produces True or False, a Boolean. 第三:比较产生 True 或 False,也就是布尔值。
Four: the exam writes assignment as a left arrow. 第四:考卷把赋值写成一个向左的箭头。
Now try the operators in the tasks below. 现在去下面的题里试试这些运算符。

Log in or create account

IGCSE, A-Level & AP