Python 比较运算符怎么用?

文章导读
Previous Quiz Next Python 比较运算符 Python 中的比较运算符在条件语句(if、else 和 elif)以及循环语句(while 和 for 循环)中非常重要。比较运算符也称为关系运算符。其中一些常见的运算符是 "<" 表示小于,"&gt
📋 目录
  1. Python 比较运算符
  2. Python 中的不同比较运算符
  3. 浮点数的比较
  4. 复数的比较
  5. 布尔值的比较
  6. 序列类型的比较
  7. 字典对象的比较
A A

Python - 比较运算符



Previous
Quiz
Next

Python 比较运算符

Python 中的比较运算符在条件语句(if、elseelif)以及循环语句(while 和 for 循环)中非常重要。比较运算符也称为关系运算符。其中一些常见的运算符是 "<" 表示小于,">" 表示大于运算符。

Python 还使用了另外两个运算符,将 "=" 符号与这两个运算符组合使用。"<=" 符号表示小于等于运算符,">=" 符号表示大于等于运算符。

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

复数的比较

虽然 complex 对象是 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 中的 Boolean 对象实际上是整数:True 是 1,False 是 0。事实上,Python 将任何非零数字视为 True。在 Python 中,可以对 Boolean 对象进行比较。"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'

序列对象通过字典序排序机制进行比较。比较从第 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

在以下示例中,比较了两个 tuple 对象 −

示例

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: '<' not supported between instances of 'dict' and '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