Skip to content

Your first program

Python Basics Lesson 1 3:46 English narration · English + 中文 subtitles burned in

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

Chapters

Transcript
A computer is not clever. 计算机并不聪明。
It cannot guess what you meant. It does exactly what you tell it, and nothing else. 它猜不出你的意思,只会照你说的做,一点也不多。
You write a line of instructions. Python reads that line. Then something appears on the screen. 你写下一行指令,Python 读这一行,然后屏幕上出现结果。
That is the whole loop, and every program you ever write is made of it — small instructions, carried out in order. 整个过程就是这样,你今后写的每一个程序,都是由这些按顺序执行的小指令组成的。
Here are two instructions in the editor, and the console beside them. 编辑器里有两条指令,旁边是控制台。
Python starts at the first line and does it: the words appear in the console. Python 从第一行开始执行:文字出现在控制台里。
Only then does it move to the second line and do that one. 做完之后,它才走到第二行,再执行那一行。
Top to bottom, one at a time. 从上到下,一次一条。
It never skips ahead, and it never runs two lines at once — so if your output comes out in a strange order, the order of your lines is the first thing to check. 它不会跳过,也不会同时执行两行——所以如果输出顺序奇怪,先检查你写的行的顺序。
Look closely at one instruction. It has three parts. 仔细看这一条指令,它有三个部分。
First the word print — that is the name of the job you want done: show something on the screen. 首先是 print 这个词——它是你要做的事情的名字: 在屏幕上显示某样东西。
Then the round brackets, which hold whatever you want shown. 然后是圆括号,里面放你想显示的内容。
And inside them, quotation marks around the words. 括号里面,文字两边还有引号。
Every part matters: leave one out and Python stops and tells you it does not understand. 每一部分都重要:少写一个,Python 就会停下来, 告诉你它看不懂。
Text inside quotation marks has a name: a string. 引号里的文字有一个名字:字符串。
You can wrap it in double quotes, and Python is happy. You can wrap the same words in single quotes, and Python is just as happy — the two are the same to it, so pick one and stay with it. 你可以用双引号把它包起来,Python 没问题; 同样的文字用单引号包起来,Python 也一样接受——对它来说两者相同,选一种坚持用就好。
But a number is different: write forty-two with no quotes at all and Python still prints it, because a number is already a value. 数字不一样:直接写 42,不加引号,Python 照样能打印,因为数字本身就是一个值。
The quotes are how you say this is text, not an instruction. 引号的作用,是告诉 Python:这是文字,不是指令。
Not every line is for the computer. 不是每一行都写给计算机看。
A line that starts with a hash is a comment: Python reads it, sees the hash, and skips the whole line. Nothing about it reaches the screen. 以井号开头的行是注释:Python 读到井号,就跳过整行, 它的内容不会出现在屏幕上。
Run this program and only the print line does anything. 运行这个程序,只有 print 那一行起作用。
Comments are notes for people — for the next person reading your code, and for you in two weeks, when you have forgotten why you wrote it. 注释是写给人看的——写给下一个读你代码的人,也写给两周后已经忘了当初为什么这么写的你。
Two mistakes catch almost everyone once. 有两个错误几乎每个人都会犯一次。
The first: you forget the quotes. 第一个:忘了加引号。
Without them Python thinks you are naming something, goes looking for a thing called Hi, and cannot find it. 没有引号,Python 以为你在 说某个名字,于是去找一个叫 Hi 的东西,结果找不到。
The second: you type a capital letter at the start. 第二个:开头打了大写字母。
Python is case-sensitive, so capital-P Print is simply a different word, and it does not know that one. Python 区分大小写,大写 P 的 Print 就是另一个词,它并不认识。
Neither is a disaster. 这都不是灾难。
Read the error message — it names the line, and usually the exact spot. 读一下错误信息——它会指出是哪一行,通常还指出具体位置。
Now the task waiting for you in the lesson. 现在看课程里等着你的那道题。
It asks for two lines of output: My name is Pat, then I am learning Python. 它要求输出两行:先是 My name is Pat, 然后是 I am learning Python。
So write the first print, with the first sentence inside quotation marks. Underneath it, write the second print with the second sentence. 那就写第一个 print,把第一句放在引号里; 在它下面写第二个 print,放第二句。
Run it, and you get two separate lines — because each print ends its line before the next one starts. 运行以后你会得到两行独立的输出—— 因为每个 print 都会在下一个开始之前结束自己那一行。
Two instructions, two lines. 两条指令,两行输出。
Four things to carry into the next lesson. 有四点带到下一课。
One: print shows text on the screen, and each one starts a new line. 第一:print 在屏幕上显示文字,每一个 print 都会另起一行。
Two: quotes make a string, single or double, as long as you are consistent. 第二:引号构成字符串,单引号双引号都行,只要保持一致。
Three: a line starting with a hash is a comment, and Python skips it. 第三:以井号开头的行是注释,Python 会跳过它。
Four: print is lower-case — Python is fussy about that, and it will say so. 第四:print 是小写的——Python 对这点很讲究,写错它会告诉你。
Now open the lesson and type the three tasks yourself. 现在打开课程,自己把三道题敲一遍。
Reading code is not the same as writing it. 读代码和写代码,是两回事。

Log in or create account

IGCSE, A-Level & AP