Python - 比較運算子



Python 比較運算子

Python 中的比較運算子Python 的條件語句if, elseelif)和迴圈語句whilefor 迴圈)中非常重要。比較運算子也稱為關係運算符。一些眾所周知的運算子是“<”表示小於,而“>”表示大於運算子。

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

複數比較

儘管複數物件是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'

序列物件透過字典序排序機制進行比較。比較從索引為 0 的元素開始。如果它們相等,則比較移動到下一個索引,直到某個索引處的元素不相等,或者其中一個序列被耗盡。如果一個序列是另一個序列的初始子序列,則較短的序列是較小的(更小的)序列。

哪個運算元更大取決於它們不相等的索引處元素值的差異。例如,'BAT'>'BAR' 為 True,因為 T 在 Unicode 順序中位於 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: '<' not supported between instances of 'dict' and '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
廣告