Editor

Fix the Empty Set Bug

Intermediate · Fix the bug · from Python Sets: Unique Collections, Operations, and Use Cases

From the lesson: Python Sets: Unique Collections, Operations, and Use Cases

Imagine you're a teacher taking attendance. A student accidentally signs the attendance sheet twice. Does that mean they're in class twice? Of course not. You only care about who showed up, not how many times they wrote their name.

Read the lesson

This code tries to create an empty set, add items to it, and print the result. But there's a bug - the programmer used {} to create an empty set. Fix the bug so the code works correctly.

# Bug: this creates a dictionary, not a set!
my_set = {}
my_set.add('python')
my_set.add('java')
my_set.add('python') # duplicate
print(sorted(my_set))

More Sets exercises

All Python exercises · 50 practice problems · Data Structures quiz