比较运算
布尔值及其真值性最常用于比较操作。我们经常在不考虑其底层数据类型的情况下使用比较。要进行比较,可以使用比较运算符。Python 支持以下比较运算符:
==等于!=不等于>大于<小于>=大于或等于<=小于或等于 在本练习中,我们将使用 Palmer 群岛(南极洲)的企鹅数据集的一个子集,变量名为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"{____} - {____}")