比較運算
布林值與其真假判定最常用在比較上,而我們平常使用比較時,往往不特別去想其底層的資料型別。要進行比較,可以使用比較運算子。Python 支援下列比較運算子:
==等於!=不等於>大於<小於>=大於或等於<=小於或等於 在本練習中,我們會使用 Palmer Archipelago(南極)企鵝資料集的一個子集,名為penguins,其為一個字典所組成的清單,鍵包含species、flipper_length、body_mass和sex。
本練習屬於課程
Python 的資料型別
練習說明
- 使用 for 迴圈走訪
penguins清單。 - 檢查該筆企鵝資料的
body_mass是否大於 3300 公克。 - 若為真,印出該企鵝的
species與sex。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Use a for loop to iterate over the penguins list
for penguin in ____:
# Check the penguin entry for a body mass of more than 3300 grams
if penguin["____"] ____ ____:
# Print the species and sex of the penguin if true
print(f"{____} - {____}")