開始使用免費開始

相等性

要檢查兩個 Python 值或變數是否相等,可以使用 ==。要檢查是否不相等,則使用 !=。以下是一些會得到 True 的範例,幫你快速複習。可以自己試著執行看看。

2 == (1 + 1)
"intermediate" != "python"
True != False
"Python" != "python"

當你把這些比較寫進指令碼時,記得要在外面包一個 print() 函式,才能看到輸出結果。

本練習屬於課程

Python 中級

檢視課程

練習說明

  • 撰寫程式碼檢查 True 是否等於 False
  • 撰寫 Python 程式碼檢查 -5 * 15 是否「不」等於 75
  • 問問 Python,字串 "pyscript""PyScript" 是否相等。
  • 比較布林值與整數會發生什麼事?撰寫程式碼檢查 True1 是否相等。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Comparison of booleans


# Comparison of integers


# Comparison of strings


# Compare a boolean with an integer
編輯並執行程式碼