python 中的 == 和 is 運算子之間的差異。


is 和 equals(==) 運算子在很多情況下表現相同,但實際上它們並不相同。is 運算子用於判斷兩個變數是否指向同一物件,而 == 運算子用於檢查兩個變數的值是否相同。

示例程式碼

# Python program to  
# illustrate the  
# difference between 
# == and is operator 
# [] is an empty list 
list1 = [] 
list2 = [] 
list3=list1 
  
if (list1 == list2): 
   print("True") 
else: 
   print("False") 
  
if (list1 is list2): 
   print("True") 
else: 
   print("False") 
  
if (list1 is list3): 
   print("True") 
else:     
   print("False")

輸出

True
False
True

更新於:2019 年 7 月 30 日

815 次瀏覽

啟動你的 職業

完成課程獲得認證

開始