Python 简明教程
Python - Comparison Operators
Python Comparison Operators
Comparison operators 在 Python 的 Python’s conditional statements ( if, else and elif ) 和 looping statements ( while 和 for loops ) 中非常重要。 comparison operators 也称为 relational operators 。一些众所周知的运算符是表示小于的 "<" 和表示大于的 ">。"
Comparison operators in Python are very important in Python’s conditional statements (if, else and elif) and looping statements (while and for loops). The comparison operators also called relational operators. Some of the well known operators are "<" stands for less than, and ">" stands for greater than operator.
Python 使用另外两个运算符,将 "=" 符号与此二者组合。符号 "⇐" 表示小于或等于运算符,符号 ">=" 表示大于或等于运算符。
Python uses two more operators, combining "=" symbol with these two. The "⇐" symbol is for less than or equal to operator and the ">=" symbol is for greater than or equal to operator.
Different Comparison Operators in Python
Python 以 "==" 和 "!=" 的形式提供了另外两个比较运算符。它们适用于 is equal to 和 is not equal to 运算符。因此,Python 中有六个比较运算符,并在此表中列出:
Python has two more comparison operators in the form of "==" and "!=". They are for is equal to and is not equal to operators. Hence, there are six comparison operators in Python and they are listed below in this table:
< |
Less than |
a<b |
> |
Greater than |
a>b |
⇐ |
Less than or equal to |
a⇐b |
>= |
Greater than or equal to |
a>=b |
== |
Is equal to |
a==b |
!= |
Is not equal to |
a!=b |
比较运算符本质上是二进制的,需要两个操作数。包含比较运算符的表达式称为布尔表达式,并且始终返回 True 或 False。
Comparison operators are binary in nature, requiring two operands. An expression involving a comparison operator is called a Boolean expression, and always returns either True or False.
Example
a=5
b=7
print (a>b)
print (a<b)
它将生成以下 output −
It will produce the following output −
False
True
两个运算对象可能为 Python literals 、 variables 或表达式。由于 Python 支持混合运算,你可以拥有任意类型的运算对象。
Both the operands may be Python literals, variables or expressions. Since Python supports mixed arithmetic, you can have any number type operands.
Example
以下代码演示了 Python 的 comparison operators with integer numbers 的用法:
The following code demonstrates the use of Python’s comparison operators with integer numbers −
print ("Both operands are integer")
a=5
b=7
print ("a=",a, "b=",b, "a>b is", a>b)
print ("a=",a, "b=",b,"a<b is",a<b)
print ("a=",a, "b=",b,"a==b is",a==b)
print ("a=",a, "b=",b,"a!=b is",a!=b)
它将生成以下 output −
It will produce the following output −
Both operands are integer
a= 5 b= 7 a>b is False
a= 5 b= 7 a<b is True
a= 5 b= 7 a==b is False
a= 5 b= 7 a!=b is True
Comparison of Float Number
在以下示例中,比较了一个整数和一个浮点操作数。
In the following example, an integer and a float operand are compared.
Example
print ("comparison of int and float")
a=10
b=10.0
print ("a=",a, "b=",b, "a>b is", a>b)
print ("a=",a, "b=",b,"a<b is",a<b)
print ("a=",a, "b=",b,"a==b is",a==b)
print ("a=",a, "b=",b,"a!=b is",a!=b)
它将生成以下 output −
It will produce the following output −
comparison of int and float
a= 10 b= 10.0 a>b is False
a= 10 b= 10.0 a<b is False
a= 10 b= 10.0 a==b is True
a= 10 b= 10.0 a!=b is False
Comparison of Complex umbers
虽然复杂对象是一个数字 data type in Python ,但它的行为不同于其他数字。Python 不支持 < 和 > 运算符,但是它支持相等 (==) 和不等 (!=) 运算符。
Although complex object is a number data type in Python, its behavior is different from others. Python doesn’t support < and > operators, however it does support equality (==) and inequality (!=) operators.
Example
print ("comparison of complex numbers")
a=10+1j
b=10.-1j
print ("a=",a, "b=",b,"a==b is",a==b)
print ("a=",a, "b=",b,"a!=b is",a!=b)
它将生成以下 output −
It will produce the following output −
comparison of complex numbers
a= (10+1j) b= (10-1j) a==b is False
a= (10+1j) b= (10-1j) a!=b is True
使用小于或大于运算符会得到 TypeError。
You get a TypeError with less than or greater than operators.
Example
print ("comparison of complex numbers")
a=10+1j
b=10.-1j
print ("a=",a, "b=",b,"a<b is",a<b)
print ("a=",a, "b=",b,"a>b is",a>b)
它将生成以下 output −
It will produce the following output −
comparison of complex numbers
Traceback (most recent call last):
File "C:\Users\mlath\examples\example.py", line 5, in <module>
print ("a=",a, "b=",b,"a<b is",a<b)
^^^
TypeError: '<' not supported between instances of 'complex' and
'complex
Comparison of Booleans
Python 中的布尔对象实际上是整数:True 是 1,False 是 0。实际上,Python 把任何非零数字当作 True。在 Python 中,比较布尔对象是可行的。 “False < True”为 True!
Boolean objects in Python are really integers: True is 1 and False is 0. In fact, Python treats any non-zero number as True. In Python, comparison of Boolean objects is possible. "False < True" is True!
Example
print ("comparison of Booleans")
a=True
b=False
print ("a=",a, "b=",b,"a<b is",a<b)
print ("a=",a, "b=",b,"a>b is",a>b)
print ("a=",a, "b=",b,"a==b is",a==b)
print ("a=",a, "b=",b,"a!=b is",a!=b)
它将生成以下 output −
It will produce the following output −
comparison of Booleans
a= True b= False a<b is False
a= True b= False a>b is True
a= True b= False a==b is False
a= True b= False a!=b is True
Comparison of Sequence Types
In Python, comparison of only similar sequence objects can be performed. A string object is comparable with another string only. A list cannot be compared with a tuple, even if both have same items.
Example
print ("comparison of different sequence types")
a=(1,2,3)
b=[1,2,3]
print ("a=",a, "b=",b,"a<b is",a<b)
它将生成以下 output −
It will produce the following output −
comparison of different sequence types
Traceback (most recent call last):
File "C:\Users\mlath\examples\example.py", line 5, in <module>
print ("a=",a, "b=",b,"a<b is",a<b)
^^^
TypeError: '<' not supported between instances of 'tuple' and 'list'
序列对象通过词法排序机制进行比较。比较从第 0 索引处的项开始。如果它们相等,则比较移至下一个索引,直到某些索引处的项碰巧不相等,或者其中一个序列用尽。如果一个序列是另一个序列的初始子序列,则较短的序列较小(较少)。
Sequence objects are compared by lexicographical ordering mechanism. The comparison starts from item at 0th index. If they are equal, comparison moves to next index till the items at certain index happen to be not equal, or one of the sequences is exhausted. If one sequence is an initial sub-sequence of the other, the shorter sequence is the smaller (lesser) one.
哪个操作数较大取决于不相等索引处的项值的差。例如,“BAT”>“BAR”为 True,因为 T 在 Unicode 顺序中排在 R 之后。
Which of the operands is greater depends on the difference in values of items at the index where they are unequal. For example, 'BAT'>'BAR' is True, as T comes after R in Unicode order.
如果两个序列的所有项比较相等,则认为序列相等。
If all items of two sequences compare equal, the sequences are considered equal.
Example
print ("comparison of strings")
a='BAT'
b='BALL'
print ("a=",a, "b=",b,"a<b is",a<b)
print ("a=",a, "b=",b,"a>b is",a>b)
print ("a=",a, "b=",b,"a==b is",a==b)
print ("a=",a, "b=",b,"a!=b is",a!=b)
它将生成以下 output −
It will produce the following output −
comparison of strings
a= BAT b= BALL a<b is False
a= BAT b= BALL a>b is True
a= BAT b= BALL a==b is False
a= BAT b= BALL a!=b is True
在以下示例中,比较了两个元组对象−
In the following example, two tuple objects are compared −
Example
print ("comparison of tuples")
a=(1,2,4)
b=(1,2,3)
print ("a=",a, "b=",b,"a<b is",a<b)
print ("a=",a, "b=",b,"a>b is",a>b)
print ("a=",a, "b=",b,"a==b is",a==b)
print ("a=",a, "b=",b,"a!=b is",a!=b)
它将生成以下 output −
It will produce the following output −
a= (1, 2, 4) b= (1, 2, 3) a<b is False
a= (1, 2, 4) b= (1, 2, 3) a>b is True
a= (1, 2, 4) b= (1, 2, 3) a==b is False
a= (1, 2, 4) b= (1, 2, 3) a!=b is True
Comparison of Dictionary Objects
未定义在 Python 的字典中使用“<”和“>”运算符。在使用这些运算符的情况下,将报告 TypeError:'<' 不支持 'dict' 和 'dict' 实例之间的比较。
The use of "<" and ">" operators for Python’s dictionary is not defined. In case of these operands, TypeError: '<' not supported between instances of 'dict' and 'dict' is reported.
相等比较检查两个 dict 项的长度是否相同。字典的长度是其中键值对的数量。
Equality comparison checks if the length of both the dict items is same. Length of dictionary is the number of key-value pairs in it.
Python dictionaries 只靠长度进行比较。元素较少的字典被认为小于元素较多的字典。
Python dictionaries are simply compared by length. The dictionary with fewer elements is considered less than a dictionary with more elements.
Example
print ("comparison of dictionary objects")
a={1:1,2:2}
b={2:2, 1:1, 3:3}
print ("a=",a, "b=",b,"a==b is",a==b)
print ("a=",a, "b=",b,"a!=b is",a!=b)
它将生成以下 output −
It will produce the following output −
comparison of dictionary objects
a= {1: 1, 2: 2} b= {2: 2, 1: 1, 3: 3} a==b is False
a= {1: 1, 2: 2} b= {2: 2, 1: 1, 3: 3} a!=b is True