Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
python3
p
communitycreator

How does set add() work in Python?

Hassan Ahmed

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)

RELATED TAGS

python
python3
p
communitycreator
RELATED COURSES

View all Courses

Keep Exploring