You are here: Home / Topics / What is "Set" in python

What is "Set" in python

Filed under: Python on 2022-08-31 16:20:13

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.


About Author:
Y
Yogesh     View Profile
I am Yogesh Sharma. I found this website very useful for all the students. It is a very good website for learning thousands of multiple choice questions. Basically it is like a community website. Everyone can add questions so am I. I will add mcqs in this website to help you.