It's Autumn! Prices fall like leaves! - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Set basics
5. Adding elements and ensuring uniqueness
Relationships between sets
Summary

Instruction

Excellent! Adding an element to a set is equally easy. All we need to do is use the add() method:

favorite_movies = {'Halloween', 'A Star Is Born', 'Hocus Pocus'}
favorite_movies.add('Star Trek')

As we said before, sets can only contain unique values. If you try to add the same item again, Python will simply ignore the operation. Let's check that in the practice exercise.

Exercise

Add two people to new_branch_employees: Alex Dawson and Santos Barnes (who already exists).

Next, print the set. How many employees do you see?

Stuck? Here's a hint!

Add the following code:

new_branch_employees.add('Alex Dawson')
new_branch_employees.add('Santos Barnes')
print(new_branch_employees)