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

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

1. StartProgramming-1-3 比较Compare

You know how to give objects names, but you will also need to know how to tell if one thing is the same as another, or if it is equal, greater or less than another.

现在你知道如何给一个对象命名,但是你也需要知道如何表示一件事物和另外一件一样,或者相等,比较大或者较小。

To do this, we use comparison operators.

要做到这些,我们用比较运算符。

The most common comparison is equality:

用的最多的运算符是相等:

this == 5

Note the difference between these two lines:

注意下面两行的不同之处:

   1 this = 4
   2 this == 5

The first line is an assignment. It uses 1 equals sign (=) and it says "Set the variable this equal to 4." This is naming, like we talked about on the last page.

第一行是一个赋值。 这用了一个等于号 (=) 并且表示“设置变量 this的值等于 4。”这是个赋值操作,像我们上面说得那样。

The second line is a comparison. It uses 2 equals signs (==) and it asks a question: "Is the variable this equal to 5?"

第二行是一个比较操作。它用了2个等于号 (==) 并且提出了个问题:"这个变量 this 是否等于 5?"

Try these lines in the Python interpreter:

在Python交互环境下试试下面的语句。

this = 5

assignment赋值

that = 10

assignment赋值

this == that

Trueor或者False假?

this = that

assignment赋值

this == that

Trueor或者False假?

If this is equal to that then Python will return True (1) and if not, then Python returns False (0).

如果 this 等于 that 那么 Python 将返回 True真 (1) 否则, Python 返回False假 (0)。

Some other common comparisons:

一些常用的比较:

3 < 5

is less than小于

True

9 > 8

is greater than大于

True

1 != 0

is not equal to不等于

True

5 is not None

is not 不是(id不等于)

True

The last example introduces two new ideas:

最后的例子介绍了两个新概念:

First, None is a special object you can use to mean "no value" or "unknown" or "not set" or "null". It took me a long time to really understand when to use None and the best way is just to read other people's code and see how they use it.

首先,None 是一个你可以理解为“没有值”或“没有赋值”或者“空值”。它使我用了相当长的一段时间才真正明白什么时候用 None ,最好的办法是去阅读别人的代码,看看他们是怎么用的。

Second, the comparison is a bit different, as it does not compare the values of the two objects, but their actual identities.

另外,比较运算符也有一点不同,它不是用来比较两个对象的值,其实实际上也差不多。

In Python, every object has a unique id number which you can get by using the builtin id() function.

在Python里面,每一个对象拥有一个唯一的id号码,你可以通过内值函数 id() 来获得。

So, what the last line actually means is:

所以,刚才最后一行的实际意思是:

id(5) != id(None)

We use is or is not because it is quicker and easier to read.

我们用 is 或者 is not 是因为这样比较快,而且容易阅读。

This comparison of identity also implies that two objects can be equal even if they are not identical.

身分比较同样暗示我们两个对象即使不是同一个也可以相等。

例如For instance:

x1 = 3.2

3.2 is not an integer,it is a floating point number.3.2不是一个整数,它是一个浮点数。

x2 = 1.8

1.8 is also a float.1.8还是一个浮点数。

x_total = x1 + x2

Adding floats always returns a float.两个浮点数相加仍然返回一个浮点数。

x_total == 5

equals等于

True

x_total is 5

isid等于

False

  • (!) 译者注:id就好像出厂的序列号一样。比方说刚刚生产的小轿车,它们的颜色、体积、重量、发动机等等完全都是相同的,但是出厂的时候,汽车生产商要为小轿车贴上一个出厂序列号,标明是哪年哪月哪日生产的第几辆小轿车,因为次序的不同,所以这个id号也是不同的。