Skip to content

Variables

Python Basics Lesson 2 3:37 English narration · English + 中文 subtitles burned in

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

Chapters

Transcript
Suppose your program mentions the same city three times. 假设你的程序里要用到同一个城市三次。
You could type it again, and again, and again — and then the day comes when the city changes, and you must find every copy. 你可以一遍一遍地把它打出来—— 可是等到有一天城市要换了,你就得把每一处都找出来。
Better: give it a name once, and use the name everywhere else. 更好的做法是:给它取一个名字,其他地方都用这个名字。
Now the city lives in one place, and changing it is a one-line job. 这样城市只存在于一个地方,要改动时改一行就够了。
That name is called a variable, and the picture to keep in your head is a box with a label on it. 这个名字叫做变量,你脑子里可以想象成一个贴了标签的盒子。
This first line makes a box labelled city and puts the word Paris into the box. 第一行做了一个标签为 city 的盒子,并把 Paris 这个词放进盒子里。
From then on, wherever you use the name, Python fetches whatever is inside. 从此以后,只要你用这个名字,Python 就会把盒子里的东西取出来。
So the print line does not say Paris anywhere — it says city, and Python looks in the box for you. 所以那行 print 里根本没有出现 Paris——它写的是 city,Python 替你去盒子里找。
Look at the shape of that line, because the order matters. 注意这行的结构,顺序很重要。
The name goes on the left. The value on the right. 名字写在左边,值写在右边。
And the equals sign in the middle is not the equals sign from your maths lesson: it does not claim the two sides are the same. 中间的等号也不是数学课上的等号:它并不是说两边相等。 它是一条指令——把它存起来。
It is an instruction — store this. Read it right to left: take the value, put it in the box called city. 要从右往左读:把这个值,放进叫 city 的盒子里。
You choose the name, but not freely. 名字由你来取,但不是随便取。
A name may use letters, digits and the underscore — and a clear one like total score, with an underscore, is worth far more than a clever one. 名字可以用字母、数字和下划线—— 像 total_score 这样清楚的名字,比一个聪明的名字有用得多。
A name cannot start with a digit, and it cannot contain a space; Python reads a space as the end of the name. 名字不能以数字开头,也不能包含空格;Python 会把空格当作名字的结束。
One more trap: capital S score and small s score are two different variables, because Python is case-sensitive. 还有一个陷阱:大写 S 的 Score 和小写 s 的 score 是两个不同的变量, 因为 Python 区分大小写。
A variable can change. 变量是可以改变的。
The first line stores one in the box called count. 第一行把 1 存进名为 count 的盒子。
Then the second line runs, and two goes in — but the box only holds one thing at a time, so the one is gone. 接着第二行执行,2 被放了进去——但盒子一次只能装一样东西,所以 1 没有了。
Not hidden underneath, gone. 不是被压在下面,是没有了。
When the print line finally asks, it gets only the latest value: two. 等到 print 那一行来问的时候,它只会拿到最新的值:2。
A name always points at exactly one value, the most recent one you stored. 一个名字永远只指向一个值,就是你最后存进去的那一个。
Some values should never change — a mathematical constant, a maximum score. 有些值本来就不该改变——比如一个数学常数,或者一个满分值。
Python has no keyword that locks a value, so programmers agreed on a signal instead: write the name in all capitals. Python 没有能锁住某个值的关键字,于是程序员约定了一个信号: 名字全部大写。
It is by convention only. 这只是一个约定。
Nothing stops you assigning to it again; the capitals are a message to the next person reading your code, saying this one is not meant to move. 没有什么能阻止你再次给它赋值; 大写字母是写给下一个读你代码的人看的,意思是:这个不该动。
Here is the second task from the lesson. 这是课程里的第二道题。
The variable colour starts as red, and you must make it blue. 变量 color 一开始是 red,你要把它变成 blue。
Not a new variable with a new name — the same one. 不是新建一个别的名字的变量——就是同一个。
So you simply write the name again on a fresh line, equals, and the new value in quotation marks. 所以你只要在新的一行再写一遍这个名字,加等号,再写引号里的新值。
Run it, and the box that held red now holds blue. 运行以后,原来装着 red 的盒子现在装着 blue。
Same name, new value: that is all reassignment is. 同样的名字,新的值: 重新赋值就是这么回事。
Four things to take with you. 带走四点。
One: name equals value stores a value under a name, right side into left. 第一:name = value 把一个值存到一个名字下面,右边进左边。
Two: storing again replaces the old value — a variable holds one thing. 第二:再存一次就会替换掉旧值——一个变量只装一样东西。
Three: names are case-sensitive, take no spaces and cannot begin with a digit. 第三:名字区分大小写,不能有空格,也不能以数字开头。
Four: a name in capitals is a constant by agreement. 第四:全大写的名字,按约定就是常量。
Now go and do the three tasks — they take a minute each. 现在去把那三道题做了——每题只要一分钟。

Log in or create account

IGCSE, A-Level & AP