Python 比较运算符
Python中的比较运算符在条件语句(if,else和elif)和循环语句(while和for循环)中非常重要。与算术运算符一样,比较运算符“-”(也称为关系运算符,<
表示小于,>
表示大于)也是众所周知的。
Python还使用两个运算符,将“=”符号与这两个运算符结合使用。”<=”符号表示小于或等于。”>=”符号表示大于或等于。
Python还有两个形式为==
和!=
的比较运算符。它们用于等于和不等于运算符。因此,Python中有六个比较运算符。
< |
小于 | a<b |
---|---|---|
> |
大于 | a>b |
<= |
小于等于 | a<=b |
>= |
大于等于 | a>=b |
== |
等于 | a==b |
!= |
不等于 | a!=b |
比较运算符是二进制的,需要两个操作数。涉及比较运算符的表达式称为布尔表达式,并且总是返回True或False。
a=5
b=7
print (a>b)
print (a<b)
它将产生以下输出: 输出 −
False
True
两个操作数可以是Python字面值、变量或表达式。由于Python支持混合算术,您可以使用任何数字类型操作数。
以下代码演示了使用Python的 比较运算符与整数 的用法。
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)
它将产生以下输出
输出 −
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
浮点数比较
在下面的示例中,将比较一个整数和一个浮点数操作数。
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)
它将产生以下输出
输出 −
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
复数的比较
虽然在Python中,复数是一种数字数据类型,但它的行为与其他类型不同。Python不支持<和>运算符,但支持相等(==
)和不等(!=
)运算符。
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)
它将产生以下 输出 −
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。
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)
它将产生以下的 输出 −
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
布尔值的比较
在Python中,布尔对象实际上是整数:True 是 1,False 是 0。事实上,Python将任何非零数视为 True。在Python中,布尔对象的比较是可能的。”False < True” 是 True!
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)
它将产生以下 输出 −
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
序列类型的比较
在Python中,只能对相同类型的序列对象进行比较。字符串对象只能与另一个字符串对象进行比较。即使列表和元组具有相同的元素,它们也无法进行比较。
print ("comparison of different sequence types")
a=(1,2,3)
b=[1,2,3]
print ("a=",a, "b=",b,"a<b is",a<b)
它将产生以下的 输出 −
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'
Sequence对象通过字典序比较机制进行比较。比较从第0个索引的项开始。如果它们相等,则比较移动到下一个索引,直到某个索引处的项不相等,或者其中一个序列耗尽。如果一个序列是另一个序列的初始子序列,则较短的序列较小(较小)。
操作数中哪个更大取决于它们在不相等的索引处的项值之差。例如,’BAT’ > ‘BAR’为True,因为在Unicode排序中T在R之后。
如果两个序列的所有项都相等,则认为这两个序列相等。
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)
它将生成以下 输出 −
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
在下面的例子中,比较了两个元组对象 –
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)
它将产生以下输出−
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
字典对象的比较
在Python中,对字典使用”<“和”>”操作符是未定义的。在使用这些操作时,会报告TypeError:“dict”和“dict”实例之间不支持'<‘。
相等比较会检查字典项的长度是否相同。字典的长度是其中键值对的数量。
Python的字典只通过长度进行比较。具有较少元素的字典被认为小于具有更多元素的字典。
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)
它将产生以下输出:
输出 -
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