Get startedGet started for free

Finding all the data and the overlapping data between sets

Sets have several methods to combine, compare, and study them all based on mathematical set theory. The .union() method returns a set of all the elements found in the set you used the method on plus any sets passed as arguments to the method. You can also look for overlapping data in sets by using the .intersection() method on a set and passing another set as an argument. It will return an empty set if nothing matches.

Your job in this exercise is to find the union and intersection in the species from male and female penguins. For this purpose, two sets have been pre-loaded into your workspace: female_penguin_species and male_penguin_species.

This exercise is part of the course

Data Types in Python

View Course

Exercise instructions

  • Combine all the species in female_penguin_species and male_penguin_species by computing their union. Store the result as all_species.
  • Print the number of species in all_species. You can use the len() function to compute the number of species in all_species.
  • Find all the species that occur in both female_penguin_species and male_penguin_species by computing their intersection. Store the result as overlapping_species.
  • Print the number of species in overlapping_species.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Find the union: all_species
all_species = ____

# Print the count of names in all_species
print(____)

# Find the intersection: overlapping_species
overlapping_species = ____

# Print the count of species in overlapping_species
print(____)
Edit and Run Code