Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Data Types in Python

Cursus bekijken

Oefeninstructies

  • 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.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# 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"{____} - {____}")
Code bewerken en uitvoeren