Set add() Syntax

Syntax: set.add( elem )

Parameters

  • elem: The element that needs to be added to a set.

Return

The add() method does not return anything 

Set add() Method in Python

The Python set add() method adds a given element to a set if the element is not present in the set in Python.

Example: Add Element to an Empty set

It is used to add a new element to the empty set.

Python3




GEEK = set()
GEEK.add('s')
print("Letters are:", GEEK)
 
# adding 'e' again
GEEK.add('e')
print("Letters are:", GEEK)
# adding 's' again
GEEK.add('s')
print("Letters are:", GEEK)


Output

Letters are: {'s'}
Letters are: {'e', 's'}
Letters are: {'e', 's'}

Similar Reads

Set add() Syntax

...

What is set add() Method

Syntax: set.add( elem )...

Python Set add() Method Examples

In Python, a set is an unordered collection of unique elements. The add() method is a built-in method in Python that is used to add a single element to a set. If the element is already present in the set, the set remains unchanged....

Contact Us