Set in python by R4R Team

Set in python are also similar as the list but in set items can never be repeated but in list items may repeated. Set is a immutable data type because in set , items value are not changes or index operation is not allowed.
Even in set any kind of index operation is not allowed.

Example of set:-
S=set([1,2,3,2])
When we try to print this set then it display only three item 1,2 and 3 because repetition are not allowed in set.
S[1]=4
It is also invalid because set data type are immutable so we can't change the index item in this manner

Program -

s=set([1,2,3,1])
print(s)
print(len(s))
s.discard(3)
print(s)
s.add(4)
print(s)
s.clear()
print(s)


output -

{1, 2, 3}
3
{1, 2}
{1, 2, 4}
set()


In this program , we simply create the set data type and display its value and length then discard() function are used to remove the specified item in set and then add() functions are used to add new item in the set and clear() function are used to clear or remove all items in the set.

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!