Comparisons
Booleans and their truthiness are most often used in comparisions, and we use comparisions without even thinking about their underlying data type. To perform a comparision, we can use a comparision operator. Python supports the following comparision operators:
==equal to!=not equal to>greater than<less than>=greater than or equal to<=less than or equal to For this exercise, we'll be using a subset of the Palmer Archipelago (Antarctica) penguin data set, namedpenguins, as a list of dictionaries with the keys ofspecies,flipper_length,body_massandsex.
This exercise is part of the course
Data Types in Python
Exercise instructions
- Use a for loop to iterate over the
penguinslist. - Check the penguin entry for a
body_massof more than 3300 grams. - Print the
speciesandsexof the penguin if true.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# 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"{____} - {____}")