add() function in python by R4R Team

- add() is a python function for the set data type.
- it add new item in the set.

Syntax-

set.add(item)

Example

set is {1,2,3}
set.add(4) will give {1,2,3,4}

set.add([1,2,3]) will give an error, because add() function can add only single element data type like int or string, not the list or set.


program-

s1=set([1,2,3])
print(s1)

s1.add(7)
print(s1)

s1.add(4)
print(s1)

s1.add("one")
print(s1)

s1.add("two")
print(s1)


output-

{1, 2, 3}
{1, 2, 3, 7}
{1, 2, 3, 4, 7}
{1, 2, 3, 4, 7, 'one'}
{1, 2, 3, 4, 7, 'one', 'two'}


-In this program, we have a set {1,2,3} and we add different items and print them one by one.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!