不只是真與假
Python 提供兩個布林值可供你使用:True 與 False。最常見的用途是表示某件事是否啟用或停用、是或否,或其他類似狀態。此外,許多 Python 型別在使用 bool() 評估時,會依其當下狀態回傳「truthy」或「falsey」的值。
本練習屬於課程
Python 的資料型別
練習說明
- 建立一個名為
my_list的空串列。 - 列印
my_list的真值性。 - 將字串
'cookies'加入 my_list。 - 列印
my_list的真值性。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create an empty list
my_list = ____
# Check the truthiness of my_list
____(____(____))
# Append the string 'cookies' to my_list
____.____('cookies')
# Check the truthiness of my_list
print(____(____))