Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Sets | Section 2
Python - Music

Pyyhkäise näyttääksesi valikon

book
Sets

In Python, a set is an unordered collection of unique elements. Think of it as your exclusive music collection where each track is distinct. Sets are perfect for situations where you need to eliminate duplicates and focus on unique items.

Here's how you can create a set in Python:

123
# Creating a set of music genres music_genres = {"Jazz", "Rock", "Classical", "Jazz"} print(music_genres)
copy

Notice how "Jazz" appears only once in the output? That's the power of sets—they automatically remove duplicates.

Just like updating your playlist with new tracks, you can add elements to a set using the add() method. If you decide a track no longer fits your vibe, you can remove it using the remove() method

123456789
music_genres = {"Jazz", "Rock", "Classical", "Jazz"} # Adding a new genre music_genres.add("Blues") print(music_genres) # Removing a genre music_genres.remove("Rock") print(music_genres)
copy

Sets are not just about uniqueness; they also allow you to perform interesting operations, much like mixing tracks to create a unique sound. Here are a few operations you can perform with sets:

  • Union: Combine two sets to see all unique elements from both.

  • Intersection: Find common elements between sets, like discovering shared influences between artists.

  • Difference: Identify elements present in one set but not the other, akin to finding unique tracks in your collection.

1234567891011121314
music_genres = {"Jazz", "Rock", "Classical", "Jazz"} more_genres = {"Hip Hop", "Electronic", "Jazz"} # Union of two sets all_genres = music_genres.union(more_genres) print(all_genres) # Intersection of two sets common_genres = music_genres.intersection(more_genres) print(common_genres) # Difference between two sets unique_genres = music_genres.difference(more_genres) print(unique_genres)
copy
Tehtävä

Swipe to start coding

Complete the get_unique_artists_count function that calculates the number of unique artists from a list of artist names. This function is useful for determining how many distinct artists are present in the list, especially when the list may contain multiple entries for the same artist.

Inputs:

  • artists: A list of strings where each string represents the name of an artist.

Steps:

  • Create a Set of Unique Artists: Convert the list of artist names into a set to eliminate any duplicate names, as sets automatically enforce uniqueness.

  • Return the Count: Use the len() function to return the number of unique artists present in the set.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 5
py

solution.py

py

main.py

Kysy tekoälyä

expand
ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

book
Sets

In Python, a set is an unordered collection of unique elements. Think of it as your exclusive music collection where each track is distinct. Sets are perfect for situations where you need to eliminate duplicates and focus on unique items.

Here's how you can create a set in Python:

123
# Creating a set of music genres music_genres = {"Jazz", "Rock", "Classical", "Jazz"} print(music_genres)
copy

Notice how "Jazz" appears only once in the output? That's the power of sets—they automatically remove duplicates.

Just like updating your playlist with new tracks, you can add elements to a set using the add() method. If you decide a track no longer fits your vibe, you can remove it using the remove() method

123456789
music_genres = {"Jazz", "Rock", "Classical", "Jazz"} # Adding a new genre music_genres.add("Blues") print(music_genres) # Removing a genre music_genres.remove("Rock") print(music_genres)
copy

Sets are not just about uniqueness; they also allow you to perform interesting operations, much like mixing tracks to create a unique sound. Here are a few operations you can perform with sets:

  • Union: Combine two sets to see all unique elements from both.

  • Intersection: Find common elements between sets, like discovering shared influences between artists.

  • Difference: Identify elements present in one set but not the other, akin to finding unique tracks in your collection.

1234567891011121314
music_genres = {"Jazz", "Rock", "Classical", "Jazz"} more_genres = {"Hip Hop", "Electronic", "Jazz"} # Union of two sets all_genres = music_genres.union(more_genres) print(all_genres) # Intersection of two sets common_genres = music_genres.intersection(more_genres) print(common_genres) # Difference between two sets unique_genres = music_genres.difference(more_genres) print(unique_genres)
copy
Tehtävä

Swipe to start coding

Complete the get_unique_artists_count function that calculates the number of unique artists from a list of artist names. This function is useful for determining how many distinct artists are present in the list, especially when the list may contain multiple entries for the same artist.

Inputs:

  • artists: A list of strings where each string represents the name of an artist.

Steps:

  • Create a Set of Unique Artists: Convert the list of artist names into a set to eliminate any duplicate names, as sets automatically enforce uniqueness.

  • Return the Count: Use the len() function to return the number of unique artists present in the set.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 5
Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Pahoittelemme, että jotain meni pieleen. Mitä tapahtui?
some-alt