开始编程之旅 翻译自Lee Harr的Start Programming

本文是使用pygsear+pygame作为开发环境,以初级用户角度来分步分阶段学习PYTHON基本概念,并以小游戏开发项目为具体案例,介绍的十分详细。编写风格清新朴实,没有象一般教科书那样枯燥,极其适合初级用户来激发兴趣时使用。

StartProgramming-1-2 名字Names

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:

例如,你也许想让一个名字等于50:

length = 50

box_length.png

This box represents an integer object which has been given the name length.

这个箱子代表有一个被命名为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.

这可能是一个不形象地比喻,但是想象这个数值在你的代码中多次用到,并且突然你意识到需要将50更改为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并且马上可以实现你想要替换的。

box_length_move.png

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.

当你创建了一个变量,你也许想知道这个变量现在等于多少。在交互环境下,你可以输入变量的名字,解释器就会告诉你答案。

比如Like this:

>>> 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是两个可用的布尔值。(真假值)

昵称Nicknames

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)

现在,代替pete.forward(20), 你可以只输入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) 仍然可以运行。一件事物可以有许多你喜欢的名字或者别名。

box_forward.png

Here the method object pete.forward has been given many different names: forward, fd and foo.

在这里,方法pete.forward 被赋予了许多不同的名字: forward,fd 和foo。

好名字Good Names

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.

许多时候你不知道存储在变量中的精确值是多少,但是可以通过其他已知的值计算出来,现在就用数学符号,Python可以帮你计算它。

例如:Like this:

>>> 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 /

除法用/

StartProgramming-1-2 (last edited 2009-12-25 07:13:54 by localhost)