Click for All Topics
Syntax –
set_name.add(element)
Example 1 – let’s see an example
my_set = set()
my_set.add(1)
my_set.add(2)
my_set.add(3)
print(my_set)
# Output -
# {1,2,3}
1
in my_set using the .add()
method.print()
function.
my_set2={10,20,30}
my_set2.add(40)
my_set2.add(20)
print(my_set2)
# Output-
# {40, 10, 20, 30}
my_set3={'this','is','edSlash'}
my_set3.add('Hello')
print(my_set3)
my_set3.add('anurag','dubey') #will give an error think about it.
# Output -
# {'Hello','this','is','edSlash'} # may be your output in a different order
# Traceback (most recent call last):
# File "/home/main.py", line 4, in
# my_set3.add('anurag','dubey')
# TypeError: set.add() takes exactly one argument (2 given)
my_set4={'a','b','c','d'}
my_set4.add(('e','f','g'))
print(my_set4)
# Output -
# {'a', ('e', 'f', 'g'), 'c', 'd', 'b'}
my_set5 = set()
for i in range(1, 6):
my_set5.add(i)
print(my_set5)
# Output -
# {1,2,3,4,5}
Office:- 660, Sector 14A, Vasundhara, Ghaziabad, Uttar Pradesh - 201012, India