The add() function in Python is a method belonging to the Set data type. It is used to add a specific element to a set. If the element is already present in the set, it does not add it again as sets do not allow duplicates. The syntax to use the add() function is as follows: your_set.add(element).
Parameter Values
| Parameter | Description |
|---|---|
| elem | The element to be added to the set. |
Return Values
The add() method for a set doesn't return any value; it returns None.
How to Use add() in Python
Example 1:
The add() method adds an element to a set. If the element is already present, it does not add the element again. Sets do not allow duplicate elements.
set_var = {1, 2, 3}
set_var.add(4)
print(set_var)