While programming, you will need to come up with names for many different things that you create. These names are sometimes called variables or identifiers.
For instance, you might want to give a name to the number 50:
length = 50
This box represents an integer object which has been given the name length.
This might seem like a silly idea, but imagine the number is used many times throughout your code, and suddenly you realize you need to change the 50 to 75.
You might do this:
forward(50) right(90) forward(50) right(90) forward(50) right(90) forward(50) right(90)
You could then use the find/replace function in your editor to change all of the 50s to 75s, but there is a good chance that you might miss one. Even worse, if this code were part of a much larger section, you might accidentally change a 50 that has nothing to do with this change.
你可以利用你编辑器的查找替换功能来替换所有的 50 到 75，但是这样你就失去了非常好的机会。更坏的情况，如果这些代码是一个大代码块的一部分，你可能替换掉了某个不希望被替换的50。
This is much better:
length = 50 forward(length) right(90) forward(length) right(90) forward(length) right(90) forward(length) right(90)
So, if you needed to change the size of your squares from 50 to 75, instead of having to go through and change all of the 50s to 75s, you can just change length = 50 to length = 75 and instantly make all of the needed changes.
所以，如果你需要把正方形的大小从50 改为 75，你可以改变 length = 50 到 length = 75来代替搜寻和替换所有的50 到 75并且马上可以实现你想要替换的。
This picture shows the name length being taken from the 50 object and placed on the 75 object.
这幅图片展示了变量 length 如何从 50转变到 75的。
它现在是多少？What Was That Again?
Once you have created variables, you will want to be able to see and to use the values they are holding on to. In the interactive session, you can just mention the name again, and the interpreter will tell you what it is.
>>> car = 'Ford Fairlane' >>> weight = 1700 >>> length = 23.4 >>> has_trunk = True >>> car 'Ford Fairlane' >>> weight 1700 >>> length 23.399999999999999 >>> has_trunk True
Notice here that there are a few different types of values that your variable names can be attached to.
'Ford Fairlane' is a string -- a series of characters enclosed in quotes. 1700 is an integer with nothing after the decimal point. 23.4 is called a floating point number -- it can have a fractional value, and notice that the value stored may not be exactly what you specified. True and False are available as boolean values.
'Ford Fairlane' 是一个 字符串 -- 一个包围在引号里面的有序的字符序列。1700是一个整数在它的小数点后面没有任何数字。23.4叫做浮点数 -- 它拥有一个小数的值，但是注意这里的值不一定是你指定的值。 True 和False是两个可用的布尔值。（真假值）
When we used the calls pete.forward() and pete.right() to make a square, the words forward and right are names for different methods that pete the Penguin object understands.
当你调用函数pete.forward() 和 pete.right()来画一个正方形，单词forward 和right 是企鹅pete所能理解的两个不同名字的方法。
Sometimes it is helpful to have more than one name for things. For instance, if you get tired of typing out pete.forward all the time, you could do:
有时有不止一种方法能够达到同样的效果。例如当你厌倦输入 pete.forward 时，你可以：
forward = pete.forward
and then even:
fd = forward
(Note that there are no parenthesis at the end of any of the names.)
Now, instead of using pete.forward(20), you could just say fd(20)
Of course, forward(20) and pete.forward(20) still work too. An object can have as many names or aliases as you would like to give it.
当然， forward(20) 和 pete.forward(20) 仍然可以运行。一件事物可以有许多你喜欢的名字或者别名。
Here the method object pete.forward has been given many different names: forward, fd and foo.
在这里，方法pete.forward 被赋予了许多不同的名字： forward，fd 和foo。
Making short names -- like using fd for pete.forward -- can save you a lot of time and typing when working in the interactive interpreter.
用一个简短的名字 -- 就像用 fd 代替 pete.forward -- 可以在使用交互环境的时候为你节省很多时间和输入内容。
However, when you start creating longer programs, you need to balance two things: You do not want to not type too much, but also you need to be able to remember what a particular name refers to.
Generally, unless a name will only be mentioned in one small area, you should not use names like:
a i q
since it is just too difficult to understand what they mean.
Instead, think about what the names refer to, and use names like:
number_of_apples appleindex quitAppleGame
Notice that there are a few different styles used to name things.
You should read through some other people's code and choose a style that you like, then try to use the same style consistently in all of your code.
一点数学知识A Little Math
Sometimes you do not know exactly what you want to store in a variable, but you could calculate it from other values that you do know. Just use mathematical symbols, and Python will do the calculations for you.
>>> price = 125 >>> tax_rate = 0.08 >>> tax = price * tax_rate >>> total = price + tax >>> total 135.0 >>> shares = 4 >>> total_per_share = total / shares >>> total_per_share 33.75
So, to multiply two numbers, use the *
Division uses the /