It is a mutable data type.
Using Set data type is used to store multiple items inside a variable.
Curly brackets must be used when using Set data type.
Note:-
A value given inside a Set cannot be changed, but a new value can be added and a value can be removed.
No indexing
There is no index value in Set data type.
Unordered
For example, if the value {'B','C','C++'} is given in a set, it will not appear in the same order on the output screen when it is printed. That is, {'C','JS','C++'} can appear in the output screen even in an order like this. can also come. Thus, every time the program is run, the order of the value given in the set changes. Therefore, we say that the set is unordered.
No duplicates
Set data type does not take duplicate value.
How to create a Set?
demo={1,2,3,"good",7.6}
print(demo)
''''
output:
{1, 2, 3, 7.6, 'good'}
''''
type()
demo={3,4,1}
print(type(demo))
''''
output:
<class 'set'>
''''
Description:-
Since the data type of the variable demo given inside the type method is set, it prints set on the output screen.
Empty Set
Empty Set can be created using Set() method.
demo=set()
print(demo)
''''
Output:
set()
''''
len()
The len() method is used to count the values inside the Set.
demo={1,3,4,5,6}
print(len(demo))
''''
Output:
5
''''
min()
The min() method can find the smallest value among the values in the set.
Set1 = {5,2,7,10,11}
print(min(set1))
''''
O/P
2
''''
max()
The max() function is used to find the largest value among the values in the Set.
demo={1,3,4,5,6}
print(max(demo))
''''
Output:
6
''''
sum()
Adds all the values inside the set using the sum() method to return its value.
set1 = {5,10,15,20}
print(sum(set1))
''''
O/P
50
''''
add()
An element can be added inside a set using the add() method.
demo={1,3,4}
demo.add(10)
print(demo)
''''
Output:
{1, 10, 3, 4}
''''
Description:-
Element 10 is added to the set using the add() method.
demo={1,3,4}
demo.add(10,50)
print(demo)
''''
output:
demo.add(10,50)
TypeError: add() takes exactly one argument (2 given)
''''
Description:-
Only one element can be added to the set using the add() method.
update()
You can add more than one element to a set using the update() method.
demo={1,3,7}
demo.update({"java","php"})
print(demo)
''''
Output:
{'php', 1, 3, 7, 'java'}
''''
You can pass more than one set inside the update() method.
demo1={1,3,7}
demo2={"node","html"}
demo3={"java",35}
demo1.update(demo2,demo3)
print(demo1)
''''
Output:
{1, 3, 'node', 35, 7, 'java', 'html'}
''''
remove()
A specific element can be removed from the set using the remove() method.
demo={1,3,"hello",7}
demo.remove("hello")
print(demo)
''''
Output:
{1, 3, 7}
''''
discard()
The discard() method is used to remove an element from the Set.
demo={1,3,"hello",7,"js"}
demo.discard("js")
print(demo)
''''
Output:
{1, 3, 'hello', 7}
''''
pop()
Randomly removes the value from the set using the pop() method.
demo={1,3,"hello",7,"js"}
print(demo.pop())
''''
output:
1
''''
No Duplicate Values
demo={1,2,3,1,2}
print(demo)
''''
Output:
{1, 2, 3}
''''
Description:-
Value 1 and 2 occur twice in Set. But it has printed only once in the output.
What this means is that no matter how many times a value is repeated, it is taken once.