Learning by doing: Reading books and trying to understand the (code) examples

39 lines
1.3 KiB

  1. song_library = [("Phantom of the opera", "Sarah Brightman"),
  2. ("Knocking on heaven's door", "Guns N' Roses"),
  3. ("Captain Nemo", "Sarah Brightman"),
  4. ("Patterns in the ivy", "Opeth"),
  5. ("November rain", "Guns N' Roses"),
  6. ("Beautiful", "Sarah Brightman"),
  7. ("Mal's song", "Vixy and Tony")]
  8. artists = set()
  9. for song, artist in song_library:
  10. artists.add(artist)
  11. print(artists)
  12. for artist in artists:
  13. print("{} plays good music".format(artist))
  14. alpabetical = list(artists)
  15. alpabetical.sort()
  16. print(alpabetical)
  17. my_artists = artists
  18. auburns_artists = {"Nickelback", "Guns N' Roses", "Savage Garden"}
  19. print("All: {}".format(my_artists.union(auburns_artists)))
  20. print("Both: {}".format(my_artists.intersection(auburns_artists)))
  21. print("Either but not both: {}".format(my_artists.symmetric_difference(auburns_artists)))
  22. bands = {"Guns N' Roses", "Opeth"}
  23. print("my_artists is to bands:")
  24. print("issuperset: {}".format(my_artists.issuperset(bands)))
  25. print("issubset: {}".format(my_artists.issubset(bands)))
  26. print("difference: {}".format(my_artists.difference(bands)))
  27. print("*" * 20)
  28. print("issuperset: {}".format(bands.issuperset(my_artists)))
  29. print("issubset: {}".format(bands.issubset(my_artists)))
  30. print("difference: {}".format(bands.difference(my_artists)))