install add () in python

Python Methods and Functions

Syntax :

 set.add (elem) The add () method doesn't add an element to the set if it's already present in it otherwise it will get added to the set.  Parameters:  add () takes single parameter  (elem)  which needs to be added in the set.  Returns:  The add () method doesn't return any value. 

# set of letters

GEEK = { ' g' , 'e' , ' k' }

 
# adding & # 39; s & # 39;

GEEK.add ( 's' )

print ( 'Letters are:' , GEEK)

 
# add & # 39; s & # 39; again

GEEK.add ( 's' )

print ( 'Letters are : ' , GEEK)

Output:

 ('Letters are:', set (['k',' e', 's',' g'])) ('Letters are:', set (['k',' e', 's',' g']) 

Application :

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

# letter set

GEEK = { 6 , 0 , 4 }

 
# append 1

GEEK.add ( 1 )

print ( ' Letters are: ' , GEEK)

  
# adding 0

GEEK.add ( 0 )

print ( 'Letters are:' , GEEK)

Output:

 ('Letters are:', set ([0, 1, 4, 6])) ('Letters are:', set ([0, 1, 4, 6])) 


Adding a tuple to the set:

# Python code to demonstrate adding a tuple to a set p.

s = { 'g' , ' e' , ' e' , 'k' , 's' }

t = ( 'f' , 'o' )

 
# add tuple t to install s.
s.add (t)

 

print (s)

Exit :

 {'k',' s', 'e',' g', ('f',' o' )} 




Tutorials