How does set add() work in Python?

The built-in add() method in Python is used to add an element to a set.

Syntax

set.add(element)

Parameters and return value

This method requires one element to be passed as a parameter. If that element already exists in the set, it won’t be added.

It is important to note that this method only returns None. Instead of returning a value, it adds an element to the set that calls the method.

Example

x = {'Cats', 'Dogs', 'Elephants', 'Seals'}
x.add('Tiger')
print(x)
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources