Python 中 = 和 == 運算子有什麼區別?


在 Python 中,= 符號定義為賦值運算子。它要求左側有一個變數,右側有一個表示式。右側表示式中的值被賦值給左側的變數。表示式和變數名稱不能互換。

>>> a=10
>>> b=20
>>> c=a+b
>>> a,b,c
(10, 20, 30)
>>> a+b=c
SyntaxError: can't assign to operator

== 符號是比較運算子,稱為等於運算子。如果兩邊的運算元相等,則返回 true,否則返回 false

>>> 10+2 == 10
False
>>> (10+2) == 12
True
>>> 'computer' == 'Computer'
False
>>> 'computer' == "computer"
True

更新於: 26-Feb-2020

1K+ 次瀏覽

開啟你的 職業生涯

完成課程即可獲得認證

開始吧
廣告
© . All rights reserved.