Introduction of Python
Getting Started with Python
Syntax in Python
Comment in Python
Variables in Python
Data Types in Python
Numbers in Python
Castings in Python
Strings in Python
Boolean in Python
Operators in Python
List in Python
Tuples in Python
Sets in Python
Dictionary in Python
if Else Statement in Python
While Loop in Python
For Loop in Python
Functions in Python
Lamba Function in Python
Arrays in Python
Classes and Objects in Python
Inheritance in Python
Iterators in Python
Scope in Python
Modules in Python
Dates in Python
Math in Python
JSON in Python
RegEx in Python
Try and Except in Python
User Input in Python
String Formatting in Python
Python me Set kya hai?
Python me set(s) inordered ways me data ko store karta hai. Dekha jaye to list aur tuple ki tarah sets me indexing ka concept nhi hota hai.
set_data = {2,34,56,78}
print(set_data[3])
O/P :
Error in output ; kyuki sets me indexing possible nhi hoti
Sets ka Syntax:
set_data = {2,34,56,78}
print(set_data)
Set me data ko single curly bracket “{…}” ke andar rakha jata hai, jaisa ki aap upar diye gaye example me dekh pa rahe hain.
Python me Sets ko Set Constructor ka use karke bhi declare kiya ja sakta hai.
Ex : set_data =set((2,34,56,78))
print(set_data)
*** set(( … )) – Double parenthesis ka use start our end me use karna compulsory hota hai.
Python Sets me values kaise change karte hain?
Python Sets me agar ek baar set banaya ja chuka hai to usme koi bhi changes nhi kar sakte. Lekin kuch function jaise add() aur update() function ka use karke hum uss set me items ko add kar sakte hain.
Add() function ka use karke single item ko add kiya jata hai aur wahi update() ka use karke kai sare items ko add kar sakte hain. Example se samajhte hain:
Add Function Method
set_one = set((“india”,”america”))
set_one.add(“russia”)
print(set_one)
Update Function Method
set_two = set((“india”,”america”))
set_two.update([“russia”, “china”, “japan”]) #use […]
print(set_two)
Set ki length calculate karen
setval={“usa”,”india”,”England”,”Canada”,”Singapore”}
print(len(setval))
Kisi bhi set ki length len() function ka use karke nikali ja sakti hai.
Sets ke items ko kaise access karen?
Kyuki Python sets me indexing directly use nhi kar sakte jiski wajah se set ke items ko access karna possible nhi hota. Issliye iske items ko loop ke through access kiya jata hai. Example se samjhe :
setdata= {“india”, “bangladesh”, “china”}
for a in setdata:
print(a)
Set me Items ko remove kaise karen?
Python sets me kisi bhi item ko remove karne ke liye remove() aur discard() function ko use kiya jata hai. Example :
setval={“usa”,”india”,”England”,”Canada”,”Singapore”}
setval.remove(“Canada”)
setval.discard(“England”)
Agar koi item set me present nhi hai aur hum ka use karte hain, to wah error generate karega lekin function koi bhi error generate nhi karega. Dekha jaye to yahi remove() aur discard() function ke bich ka main difference hai.
Iske alawa kai functions jaise pop(), del(), clear() ka use kiya jata hai. Jaha pop() function kisi bhi set ke end se data ko remove karta hai, clear() function set ko clear karta hai lekin set ke presence ko banaye rakhta hai aur wahi del() function poore set ko hi delete kar deta hai.
setval={“usa”,”india”,”England”,”Canada”,”Singapore”}
setval.pop()
setval.clear()
setval.del()
Sets ko apas me join kaise karen?
Python set me generally 2 tarah ke functions update() aur union() function use kiye jate hain.
union() function 2 ya 2 se jyada sets ko add karke sabhi items ko ek set me hi allocate kar deta hai.
setdata1 = {“america”, “italy” , “china”}
setdata2 = {“india”, “japan”}
setdata3 = setdata1.union(setdata2)
print(setdata3)
Wahi update() function kisi ek set ke items ko dusre set ke items se update karta hai
setdata1 = {“america”, “italy” , “china”}
setdata2 = {“india”, “japan”}
setdata1.update(setdata2)
print(setdata1)