Python数据分析(中英对照)·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.
在Python中,您需要大写这些单词,True和False,以便Python将它们理解为布尔类型。
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.
如果我们键入True,Python会告诉我们这是一个布尔对象。
If we type True, Python tells us this is a boolean object.
或者如果我们问Python,False的类型是什么,F大写,Python告诉我们这是一个布尔对象。
Or if we ask Python, what is the type of False, with F capitalized,Python tells us this is a boolean object.
注意,如果我不大写false,Python就不知道这个对象是什么。
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.
让我们从“或”开始。如果x为真或y为真,或者两者都为真,那么x和y之间的“或”将为真。
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.
例如,如果我们说True或False,那么Python返回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”才为真。
"And" is only true if both objects are True.
所以如果我们输入True和True,答案将是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.
所以为了使“and”为真,这两个对象都必须为真。
So in order for "and" to be True, both of the objects need to be True.
最后,我们有“not”操作,它简单地否定对象的值。
Finally, we have the "not" operation, which simply negates the value of the object.
所以,如果我们说不正确,Python会给我们错误。
So if we say not True, Python gives us False.
如果我们说notfalse,Python就会返回True。
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.
我们可能会问Python,2比4小吗?
We might ask Python, is 2 less than 4?
Python对我们来说是真实的。
Python returns True to us.
我们也可以问,2是否小于或等于,比如说,2?
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.
在这种情况下,答案是错误的,因为2等于2。
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.
我们可以询问Python,包含数字2和3的列表是否与包含数字3和3的列表相同。
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.
如果我们修改第二个列表,现在两个列表中都有数字2和3。
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.
但是,如果我们想询问第一个列表是否与第二个列表是同一个对象,我们将使用“is”比较。
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.
如果我们想知道第一个列表是否与第二个列表不是同一个对象,我们可以使用“is not”操作。
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.
在本例中,Python返回True。
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.
这就是为什么这个比较返回False。
That’s why this comparison returns False.
我们如何测试浮点数和整数这两个数的相等性?
How would we test equality of two numbers that are a floating point number and an integer?
因此,我们可以看到测试2.0是否等于2.0时如何返回True。
So we can see how testing if 2.0 is equal to 2.0 returns True.
但是如果我们问Python,2.0是否等于2?
But what happens if we ask Python, is 2.0 equal to 2?
在本例中,2.0是一个浮点数,而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.
整数2的浮点表示形式为2.0。
The floating point representation of the integer 2 is 2.0.
因此,现在我们将隐式地比较2.0和2.0。
So now we are comparing implicitly 2.0 to 2.0.
因此,答案将是正确的。
Therefore the answer is going to be True.
金点网络-全网资源,一网打尽 » Python数据分析(中英对照)·Expressions and Booleans 表达式和布尔值
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
- 是否提供免费更新服务?
- 持续更新,永久免费
- 是否经过安全检测?
- 安全无毒,放心食用