add() method for set
add(element) takes one arguments .
element
: element to be added to set
Returns : No return value.
my_set={'Alex','Ronald','John'}
my_set.add('Lewis')
print(my_set)
Output is here
{'Ronald', 'John', 'Alex', 'Lewis'}
Adding duplicate value
my_set={'a','b','c','x'}
my_set.add('c')
print(my_set)
Output
{'a', 'x', 'c', 'b'}
There is no error if we try to add element already available but it will not add the element again as set can't have duplicate elements.
Difference between add() and update()
Read more on update() method here.
« All set methods « Questions with solutions on set
← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
plus2net.com