हेल्लो दोस्तों ! आज इस पोस्ट में python के बारे में बताया गया है की Python Dictionaries in hindi क्या होता है कैसे काम करता है और भी बहुत कुछ दिया गया है तो चलिए शुरू करते है
Contents
Introduction to Python Dictionaries
Python आपको एक और Lists जैसा collection provide करती है जिससे dictionary कहा जाता है। Lists की ही तरह एक dictionary को आसानी से change किया जा सकता है और आवश्यकतानुसार घटाया और बढ़ाया जा सकता है।
List और dictionary में िकय यह होता है की list में elements ordered होते है, लेकिन एक dictionary में elements ordered नहीीं होते है।
इसके अिावा dictionary collection indexing को support नहीीं करता है। इसका मतिब यह हुआ की dictionary में ककसी element को आप उसकी index या position के द्वारा access नहीीं कर सकते है। Dictionary में elements को keys के द्वारा access किया जाता है।
Dictionary में कोई भी element key और value के pair में store किया जाता है। हर key एक value को point करती है या किर उसे store और access करने के लिए use की जाती है।
एक dictionary के अींतगयत ककसी key की value python के ककसी भी type की हो सकती है। यानी की strings, lists, tuples और dictionary को आप key की value के रूप में use कर सकते है।
लेकिन dictionary में आप हर type की key नहीीं define कर सकते है।
Dictionary में key define करने के लिए आप लसिय immutable (ऐसे types जो unchangeable है या जजनकी values को change नहीीं किया जा सकता है) types को ही use कर सकते है। यानी tuples, strings, integers, floats आदद को key के रूप में use किया जा सकता है।
आप dictionary में value के रूप में ककसी list को store कर सकते है और एक list में भी value के रूप में dictionary को store किया जा सकता है।
Dictionary collection list, tuples आदद के साथ perform होने वािे operations को support नहीीं करता है। आप dictionary के साथ concatenation आदद operations perform नहीीं कर सकते है। Dictionary का मुख्य उद्देश्य mapping होता है। यानन की एक key को एक value से map करना।
Creating Python Dictionaries
एक Dictionary के items को curly brackets में define किया जाता है। Dictionary में items को key:value के pair में लिखा जाता है। Key और value को colon से separate किया जाता है। Colon के left side में key होती है और right side में value होती है। Key:value के हर pair को comma से separate किया जाता है।
उदाहरर् के लिए ननिे दिए गए code को देखखये।
myDict = {“c1″:”Red”,”c2″:”Green”,”c3″:”Blue”} print(myDictionary)
ऊपर दिए गए उदाहरर् में myDict के नाम से एक dictionary collection create किया गया है। इस dictionary में c1, c2 और c3 keys है और Red, Green और Blue values है। एक बात आपको ध्यान रखनी िादहए की string keys और string values को हमेशा double quotes में ही लिखा जाना िादहए। Tuples को आप keys के रुप में use कर सकते है। उदाहरर् के लिए ननिे दिए गए code को देखखये।
myDict = {(Apple, Chilli, Tomato):”Red”,(Calabash, Cabbage,Beans):”Green”}
ऊपर दिए गए उदाहरर् में tuples को keys के रूप में use किया गया है।
यदि आप एक existing dictionary में कोई नया item add करना चाहते है तो ऐसा आप एक नया नाम और नयी value define करके करते है। उदाहरर् के लिए ननिे दिए गए code को देखखये।
myDict = {“c1″:”Red”,”c2″:”Green”,”c3″:”Blue”} print(myDict)
myDict[“c4”] = “Lime” print(myDict)
ऊपर दिए गए उदाहरर् में myDict dictionary में c4 key और Lime value बाद में separately add की गयी है।
आप dict() constructor द्वारा भी dictionary create कर सकते है। जब आप dict() constructor के प्रयोग से dictionary create करते है तो keys को double quotes में नहीीं लिखते है और colon (:) की जगह assignment operator (=) का प्रयोग करते है। इसका उदाहरर् ननिे ददया जा रहा है।
myDict = dict(c1=”Red”,c2=”Green”,c3=”Blue”) print(myDict)
ऊपर दिए गए उदाहरर् में dict() constructor द्वारा dictionary collection create किया गया है।
Accessing Python Dictionaries
ककसी dictionary से items access करने के लिए उन्हें keys के द्वारा refer किया जाता है। उदाहरर् के लिए ननिे दिए गए code को देखखये।
myDict = {“c1″:”Red”,”c2″:”Green”,”c3″:”Blue”} print(myDict[“c1”])
ऊपर दिए गए उदाहरर् में c1 key द्वारा Red value को print किया गया है।
किसी dictionary में से value access करने के लिए आप get() method का भी प्रयोग कर सकते है। यह method argument के रूप में key को चाहता है। उदाहरर् के लिए ननिे दिए गए code को देखखये।
myDict = {“c1″:”Red”,”c2″:”Green”,”c3″:”Blue”} print(myDict.get(“c2”))
ऊपर दिए गए उदाहरर् में get() method द्वारा c2 key की value Green को print किया गया है।
आप ककसी dictionary के सभी elements को print करने के लिए उसे iterate कर सकते है। इसके लिए for loop और in operator का प्रयोग किया जाता है। उदाहरर् के लिए ननिे दिए गए code को देखखये।
myDict = {“c1″:”Red”,”c2″:”Green”,”c3″:”Blue”}
for key in myDict print(myDict[key])
ऊपर दिए गए उदाहरर् में for loop द्वारा myDict dictionary के सभी elements को print किया गया है। जब आप इस प्रकार ककसी dictionary को iterate करते है तो dictionary की keys return की जाती है।
इसके अिावा आप ककसी dictionary की keys को iterate करने के लिए iterkeys() method और values को iterate करने के लिए itervalues() method use कर सकते है। उदाहरर् के लिये ननिे दिए गए code को देखखये।
myDict = {“c1″:”Red”,”c2″:”Green”,”c3″:”Blue”}
for key in MyDict.iterkeys() print(myDict[key])
for value in myDict.itervalues() print(myDict[value])
ऊपर दिए गए उदाहरर् में myDict dictionary की keys और values को for loop के साथ iterkeys() और itervalues() methods को प्रयोग करके print किया गया है।
इन दोनों functions के अिावा आप items() function को भी use कर सकते है। यह function keys और values को एक साथ return करता है।
Updating Python Dictionaries
Dictionaries में यदि आप ककसी member की value को change करना चाहते है तो ऐसा आप उसकी key के द्वारा कर सकते है। आप key के द्वारा उस member को नयी value assign करवा सकते है।
myDict = {“c1″:”Red”,”c2″:”Green”,”c3″:”Blue”}
print(myDict) myDict[“c1”] = “Lime” print(myDict)
ऊपर दिए गए उदाहरर् में c1 key की value को Red से Lime में change किया गया है।
Deleting Python Dictionaries
Dictionaries में से items को delete करने के लिए आपको tuples की तरह उन्हें combine करने की आवश्यकता नहीीं होती है। Del keyword द्वारा आप ककसी specific key वािे item को remove कर सकते है। उदाहरर् के लिए ननिे ददया गया code देखखये।
myDict = {“c1″:”Red”,”c2″:”Green”,”c3″:”Blue”} print(myDict)
del myDict[“c2”] print(myDict)
ऊपर दिए गए उदाहरर् में del command द्वारा second item को remove किया गया है।
इसके अिावा आप pop() method द्वारा भी ककसी item को remove कर सकते है। इस method में key को argument के रूप में pass किया जाता है। इसका उदाहरर् ननिे ददया जा रहा है।
myDict = {“c1″:”Red”,”c2″:”Green”,”c3″:”Blue”} print(myDict)
myDict.pop(“c3”) print(myDict)
ऊपर दिए गए उदाहरर् में pop() method द्वारा myDict dictionary के तीसरे item को remove किया गया है।
इसके अिावा dictionary के last item को remove करने के लिए आप popitem() method को use कर सकते है।
Del keyword द्वारा आप एक सम्पूर्य dictionary को भी delete कर सकते है। इसके अिावा यदि आप एक dictionary को खािी करना चाहते है तो इसके लिए clear() method use कर सकते है। इस method से dictionary के सभी elements delete हो जाते है।
Methods for Python Dictionaries
Python में dictionaries के लिए ननिे दिए गए methods available है।
- copy()
- sort()
- len()
- cmp()
- update()
- str()
- items()
इन methods के अिावा आप dictionaries के साथ in और not in जैसे operators भी use कर सकते है।
निवेदन:-आप सभी छात्र –छात्रों से निवेदन है की अगर आपको ये Topic(Python Dictionaries in hindi) अच्छा लगा हो तो कृपया आप इस वेबसाइट(Python Dictionaries in hindi) के बारे में अपने दोस्तों को जरुर बताये अगर कोई topic(Python Dictionaries in hindi ) से संबधित प्रश्न हो तो कमेंट्स(comments) आपके लिए ही बना है और किसी Subject के लेकर भी कोई प्रश्न हो तो कमेंट करे