Expressions and Booleans 表达式和布尔值

1.1.6: Expressions and Booleans 表达式和布尔值

Expression is a combination of objects and operators that computes a value.

Many expressions involve what is known as the boolean data type.

Objects of the boolean type have only two values.

These are called True and False.

In Python, you need to capitalize these words, True and False,for Python to understand them as boolean type.

Let’s just check very quickly.

If we type True, Python tells us this is a boolean object.

Or if we ask Python, what is the type of False, with F capitalized,Python tells us this is a boolean object.

Note that if I don’t capitalize false, Python doesn’t know what this object is.

It doesn’t understand that.

So you need to be sure to capitalize your boolean types.

Operations involving logic, so-called boolean operations,take in one or more boolean object and then

they return one boolean object back to you.

There are only three boolean operations, which are “or”, “and”, and “not”.

So let’s try these out.

Let’s start with “or”. “Or” between x and y is going to be True if either x is True or y is True, or both are True.

So for example, if we say True or False, then Python returns True.

True or True would also be True.

So the only time “or” would be False– if both the first and second object surrounding “or” are False.

“And” is only true if both objects are True.

So if we type True and True, the answer is going to be True.

However, if we turned the second True to False, “and” is going to be False.

So in order for “and” to be True, both of the objects need to be True.

Finally, we have the “not” operation, which simply negates the value of the object.

So if we say not True, Python gives us False.

And if we say not False, Python returns to us True.

We often need to compare objects in our programs.
Python中总共有八种不同的比较操作。
There are a total of eight different comparison operations in Python.

Although these are commonly used for numeric types,we can actually apply them to other types as well.

For example, if you’re comparing two sequences,the comparison is carried out element-wise.

So you’re comparing the first element of your first sequence to your first element in your second sequence, and so on.

The result of a comparison like this is always a boolean type, either True or False.

It’s perhaps easiest to understand these comparisons through a simple example that involves numbers.

So let’s try out a couple of them.

Let’s say you are comparing two numbers.

We might ask Python, is 2 less than 4?
Python对我们来说是真实的。
Python returns True to us.

We can also ask, is 2 less than or equal to, say, 2?

And in this case, the answer is again True.

We can test for equality by using two equal signs.
2等于2，因此Python向我们返回True。
2 is equal to 2, so Python returns True to us.

And finally we can ask, are two objects not equal to one another,by using the exclamation mark or exclamation point.

In this case, the answer is False, because two is equal to 2.

These two comparisons are used to test whether two objects are the one and the same.

Notice that this is different than asking if the contents of two objects are the same.

So let’s see what this implies using a little bit of code.

We could ask Python if the list that contains numbers 2 and 3 is the same as the list containing numbers 3 and 3.

The answer is, of course, False in this case.

If we modify the second list, now both lists have the numbers 2 and 3 in them.

The answer is going to be True.

The lists are identical in content.

However, if we wanted to ask if the first list is the same object as the second list, we would use the “is” comparison.
Python告诉我们这是错误的。
And Python tells us that this is False.

If we’d like to know if the first list is not the same object as the second list, we can use the “is not” operation.

And in this case, Python returns True.

So we actually have two lists here.

They happen to have the same contents, but we do have two objects.

That’s why this comparison returns False.

How would we test equality of two numbers that are a floating point number and an integer?

So we can see how testing if 2.0 is equal to 2.0 returns True.

But what happens if we ask Python, is 2.0 equal to 2?

In this case 2.0 is a floating point number, whereas 2 is an integer.

What happens in this situation is the following:
Python接受第二个数字，即数字2，一个整数——它将其转换为浮点数。
Python takes the second number, which is number 2, an integer– it turns that into a floating point number.

The floating point representation of the integer 2 is 2.0.

So now we are comparing implicitly 2.0 to 2.0.

Therefore the answer is going to be True.

上一篇 Numbers and Basic Calculations 数字和基本计算
1.1.4: Numbers and Basic Calculations 数字和基本计算数字是Python中的一种对象类型。Numbers are one type of object in Python.实际上，Python提供了三种不
2021-10-18 Lists 列表
1.2.2: Lists 列表列表是任何类型的对象的可变序列。Lists are mutable sequences of objects of any type.它们通常用于存储同质项目。And they’re typically use
2021-10-18
目录