Get startedGet started for free

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, named penguins, as a list of dictionaries with the keys of species, flipper_length, body_mass and sex.

This exercise is part of the course

Data Types in Python

View Course

Exercise instructions

  • Use a for loop to iterate over the penguins list.
  • Check the penguin entry for a body_mass of more than 3300 grams.
  • Print the species and sex of 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"{____} - {____}")
Edit and Run Code