Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Recap
2. Recap
Using sets to compute unique elements
Set operations
Simulating machines
Summary

Instruction

Before we dive into the actual applications of sets, let's do a quick review.

set

As you may remember, sets are unordered collections of elements – there is no "first" or "last" element. Also, sets can only store unique elements; duplicates are not allowed.

Let's take a look at some basic set operations:

  1. Define a set:
  2. empty_set = set()
    letters = {'a', 'd', 'f', 'h'}
  3. Add an element:
  4. letters.add('t')
  5. Remove one element:
  6. letters.discard('d')
  7. Remove all elements:
  8. letters.clear()
  9. Iterate over a set:
  10. for letter in letters:
      ...
  11. Check if an element is in a set:
  12. if letter in letters:
      ...

How about a quick warmup exercise?

Exercise

Write a function named count_unique_letters(word) that accepts a string argument and returns the number of unique letters (characters) in the word.

Stuck? Here's a hint!

Within a loop, add each letter of the string to a set. Then, return the length of the set with:

len(unique_letters)