3. What is Set Data Type ?
If a programmer wants to keep his data secure, then Set is a very important and interesting data
type in
terms of keeping the data secure. Set is a mutable data type and despite being mutable, no
duplicate
data/value can be stored in a set. Python mein set banane ke liye hum curly brackets {...} ka
istemal karte hain aur items ke beech mein comma , lagate hain.
Note: Agar aapko ek khali (empty) set banana hai, toh aap {} nahi likh sakte, kyunki use Python
dictionary maan leta hai. Khali set hamesha set() function se banta hai.
1. Set Banane ka Tarika aur Uniqueness
Set apne aap duplicate values ko delete kar deta hai. Agar aap ek set mein ek hi number 3 baar bhi likhenge, toh wo use sirf ek hi baar store karega.
# Set banana (Isme duplicate numbers diye hain)
my_set = {1, 2, 2, 3, 4, 4, 4, 5}
print(my_set)
# Empty set banane ka sahi tarika
empty_set = set()
print(type(empty_set))
{1, 2, 3, 4, 5}
< class 'set' >
2. Set Unordered hota hai (No Indexing & Slicing)
Kyunki set ke andar items ka koi fixed order nahi hota, isliye aap isme Indexing ya Slicing ka use nahi kar sakte. Agar aap my_set[0] likhenge, toh error aayega kyunki set mein pehla ya aakhiri jaisa koi concept nahi hota.
names_set = {"Amit", "Rahul", "Karan"}
# Har baar run karne par sequence badal sakta hai
print(names_set)
{'Karan', 'Amit', 'Rahul'}
Set Methods
Set Methods Python mein pehle se bane-banaye (built-in) functions hote hain, jinka istemal set ke andar naye elements ko jodne, purane elements ko hatane, ya do sets ke beech math operations (jaise milaana ya tulna karna) ke liye kiya jata hai.
1. Union Method
Union method is used when one or more sets are combined to form a single set, i.e. if there are more than one common values ββin the sets, only one value is retained and the rest are removed. In the code editor given below, two sets have been created and only one common value from both the sets has been printed.
A = {1, 2, 3}
B = {3, 4, 5}
print(A.union(B))
{1, 2, 3, 4, 5}
A = {1, 2, 3} : A variable named A is created in which the set data is stored.
B = {3, 4, 5} : A variable named B is created in which the set data is stored.
print(A.union(B)) : This print() function displays the common data from both the
variables A and B.
2. Difference Method
Comparison can be done between one or more sets using the Difference Method, that is, the Difference Method is used to find out the values ββwhich are not common in all the sets. Two sets have been created in the code editor given below and the values ββwhich are not common in both have been printed.
A = {1, 2, 3}
B = {3, 4, 5}
print(A.difference(B))
{1, 2}
A = {1, 2, 3} : A variable named A is created in which the set data is stored.
B = {3, 4, 5} : A variable named B is created in which the set data is stored.
print(A.difference(B)) : This print() function prints those values ββwhich are not
present in both
the variables.
3. Subset Method
Subset is a method of Python language, which is used to find out whether all the values ββof a set are present in another set or not. The output obtained by using the Subset method is True or False. Two sets have been created in the code editor given below and it has been found out whether the values ββof set B are present in set A or not.
A = {1, 2}
B = {1, 2, 3, 4}
print(A.issubset(B))
True
A = {1, 2} : The set data is stored in the A variable.
B = {1, 2, 3, 4} : There is a variable named B in which some numbers are stored.
print(A.issubset(B)) : This Print() function shows True in the output when all the
numbers stored
in the A variable are found in the B variable .
Features of Set Data Type
1. No Duplicates Allowed : Set apne andar kabhi bhi ek jaisi do values ko store
nahi karta hai.Yeh feature data mein se duplicates hatane ke liye sabse best maana jata hai.
2. Mutable but Elements are Immutable : Aap poore set mein naye items add ya
remove kar sakte hain (Set mutable hai).Lekin set ke andar jo item rakha hai, use aap badal nahi
sakte hain.
3. High Performance Search : Set ke andar kisi item ko dhoodhna (using 'in'
operator) bohot tez hota hai.Yeh list ke muqable hazaron guna tezi se elements ko check kar leta
hai.