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 Dictionary Overview
Python me Dictionary ka concept bhut bada hai, iske keys aur value pair ka concept isse kafi robust aur coding friendly banata hai. Dictionary me data unordered ways me store kiye jate hain, Jise indexing ke through access bhi kiya ja sakta hai aur List ki tarah isme bhi data ko change kiya ja sakta hai.
Dictionary Example
dict_one ={“name”:”Ashish”,”class”:”12″,”batch”:”2000″}
print(dict_one)
Python Dictionary ka Syntax
Python Dictionary me items ko single curly bracket “{…}” ke andar rakha jata hai, jaisa sets ke case me hota hai. Lekin Dictionary me items key/value ke pair me present hote hain.
Python me Dictionary ko dict Constructor ka use karke bhi declare kiya ja sakta hai.
Ex dict2 = dict(name=”Ashish”,std=”12″,batch=”2000″)
print(dict2)
*** dict( … ) – Single parenthesis ka use start our end me use karna compulsory hota hai. Dekha jaye to dict2 ka syntax dict_one(upar diye gaye example me) ke comparison me kafi alag hai.
Jaha dict_one me curly bracket ka use karke usme keys aur values ko double curser ke andar mention karke (:) se assign kiya ja raha hai, wahi dusri taraf dict2 me dict constructor ka use karte huye items ko single bracket ke andar rakha ja raha hai, aur usi ke sath values ko assignment operator (=) ke through keys ke sath assign kiya ja raha hai.
Python Dictionary me items ko kaise access karen?
Python Dictionary me items key aur value pair me store hote hain, issliye hum kisi bhi dictionary ke item ko uske key ke through access kar sakte hain. Iske alawa get() function ka bhi use karke dictionary ke items ko access kiya ja sakta hai. Example se samjhe:
Dictionary Item Access
dict_one ={“name”:”Ashish”,”class”:”12″,”batch”:”2000″}
print(dict_one)
print(dict_one[“name”])
print(dict_one[“class”])
print(dict_one[“batch”])
dict_one.get(“name”)
dict_one.get(“class”)
dict_one.get(“batch”)
Python Dictionary me values kaise change karen?
Python Dictionary me values ko indexing ke through change kiya jata hai. Example se samjhe:
Dictionary : Changing Values
dict_one ={“name”:”Ashish”,”class”:”12″,”batch”:”2000″}
print(dict_one)
dict_one [“name”]=”Shivam”
dict_one [“std”]=”11″
dict_one [“batch”]=”2010″
print(dict_one)
Dictionary Length calculate karen
Python Dictionary ki length len() function ka use karke kiya jata hai. Example se samjhe:
Dictionary : Length Function
dict_one ={“name”:”Ashish”,”class”:”12″,”batch”:”2000″}
print(len(dict_one))
Python Dictionary me Loop ka concept
Dictionary me loop by default key ki value generate karta hai. For loop ke concept se samajhiye
Dictionary loop
dict_one ={“name”:”Ashish”,”class”:”12″,”batch”:”2000″}
for x in dict_one:
print(x)
Agar aap dictionay ki value ko print karana chahte hain to code ko kuch aisa likhna hoga. Example dekhe:
Dictionary loop
dict_one ={“name”:”Ashish”,”class”:”12″,”batch”:”2000″}
for x in dict_one:
print(dict_one[x]) # dict_one[x]
dict_one ={“name”:”Ashish”,”class”:”12″,”batch”:”2000″}
for x in dict_one.values():
print(x) # values() method use karne par
Agar aap dictionary ki keys aur values dono me ek sath loop chalana chahte hain to code kuch aisa rakhe jaisa niche example me diya gaya hai.
Dictionary loop – using items() method
dict_one ={“name”:”Ashish”,”class”:”12″,”batch”:”2000″}
for x_key, y_value in dict_one.items():
print(x_key, y_value)
Python Dictionary me item kaise add karen?
Python Dictionary me items ki indexing keys ke through hoti hai means agar existing dictionary me koi item ko add karne ke liye hume particular key define karke uski value ko assign karna hoga. Example se samjhe:
Item addition through Indexing
dict_one ={“name”:”Ashish”,”class”:”12″,”batch”:”2000″}
dict_one[“subject”] = “Mathematics”
print(dict_one)
Python Dictionary me items ko remove kaise karen?
Python Dictionary me items ko remove karne ke liye kai fuctions jaise pop(), popitem(), clear() ka use hota hai aur iske alawa ‘del’ keyword ka use hota hai. Ab step by step sare method ko jante hain.
pop() function me uss item ki key ko dalna hota hai, jis item ko hum delete karna chahte hain. pop() function execute hone ke baad delete ki gai value ko bhi return karta hai. Iska matlab yah hai ki aap uss popped value ko kisi dusre variable me store karke uska use kar sakte hain.
# Pop() Method
dict_one ={“name”:”Ashish”,”class”:”12″,”batch”:”2000″}
popped_value = dict_one.pop(“class”)
print(popped_value)
print(dict_one)
popitem() function dictionary ke last item ko delete karta hai aur sath hi delete kiye gaye item ko bhi return karta hai. Example se samjhe:
# popitem() Method
dict_one ={“name”:”Ashish”,”class”:”12″,”batch”:”2000″}
popped_item = dict_one.popitem()
print(popped_item)
print(dict_one)
clear() function dictionary ko empty karta hai.
# clear() Method
dict_one ={“name”:”Ashish”,”class”:”12″,”batch”:”2000″}
dict_one.clear()
print(dict_one)
del keyword ek particular key ki value ko delete karta hai means del keyword ke sath key bhi define karna hoga.
# del keyword
dict_one ={“name”:”Ashish”,”class”:”12″,”batch”:”2000″}
del dict_one[“batch”]
print(dict_one)
Python Dictionary me copy kaise kam karta hai?
Python dictionary me agar hum 1 dictionary ke items ko dusre dictionary me copy karna chahte hain, to usse directly assign nhi kar sakte , jaise dict_one = dict_two kyuki yaha hum dict_one ka reference dict_two me assign kar rahe hain jiska matlab yah hai ki agar hum dict_two me kuch changes karenge to wah dict_one me bhi reflect hoga jo code me problem create kar sakta hai.
Issliye aisi problems ko dur karne ke liye copy() function ya fir dict() function ko use kiya jata hai, jo copy kiye gaye dictionary ko ek separate space allocate karega.
# copy() function
dict_one ={“name”:”Ashish”,”class”:”12″,”batch”:”2000″}
print(dict_one)
dict_copied = dict_one.copy()
print(dict_copied)
# dict() function
dict_one ={“name”:”Ashish”,”class”:”12″,”batch”:”2000″}
print(dict_one)
dict_copied = dict(dict_one)
print(dict_copied)
Python Dictionary me Nested Dictionary kaise create karte hain?
Nested Dictionary means ek dictionary ke andar kai aur dictionaries ka presence hona, niche diya gaya example ek Nested Dictionary ka best example hai jaha “school” ek main dictionary hai aur uske andar kai sare dictionaries labs hain ,jisme har ek lab ka ek naam aur ek assistant hai.
# Nested Dictionary
school = {
“lab1”:{“assistant”:”Rajesh”,”labname”:”chemistry”},
“lab2”:{“assistant”:”Mahesh”,”labname”:”physics”},
“lab3”:{“assistant”:”Priya”,”labname”:”biology”}
}
print(school)
Upar diye gaye example me hum har ek dictionary ko separately define karke nested loop ke concept par sabhi dictionaries ko ek final dictionary me add kar sakte hain. Example se samjhe:
# Nested Dictionary
lab1={“assistant”:”Rajesh”,”labname”:”chemistry”}
lab2={“assistant”:”Mahesh”,”labname”:”physics”}
lab3={“assistant”:”Priya”,”labname”:”biology”}
#School is a final dictionary
school = {“laboratroy1”:lab1,“laboratroy2”:lab2,“laboratroy3”:lab3}
print(school)