What is dictionary in Python Hindi ?

 What is dictionary in Python in hindi .


Dictionary :- 

       Dictionary एक collection of elements है मतलब इसमे बोहोत सारे elements होते है जिसमे कोई order नही होता है जिसको हम change कर सकते है और इसमें indexing भी होती है । python मैं dictionary को curly {} brackets के अंदर लिखा जाता है और इसमे keys ओर value होते है । मतलब dictionary दो चीजो को contains करती है पहली key ओर दूसरी value ।

Example :- 


dict1 = {'year':'2022', 'learn':'python'}
print(dict1)
अब dictionary को access कैसे करेंगे 

हमे किसी भी dictionary के elements को access करना है तो हम key value को use करते है कैसे करते है देखिए ।

Example :- 



dict1 = {'year':'2022', 'learn':'python'}
print(dict1)
x = dict1['learn']
print(x)
इसी same task को करने के लिए एक ओर method है जिसे हम get() कहते है । 

Example:- 



dict1 = {'year':'2022','learn':'python'}
y = dict1.get('year')
print(y)
अब सारे elements को one by one loop के through access करना हो यो कैसे access करेंगे ? 


Example:-



dict1 = {'year':'2022','learn':'python'}
for x in dict1:
	print(x)
ये values को print नही करेगा बल्कि index को print करेगा । मतलब जब आप इसको run करेंगे तो ये हमे year ओर learn print करके देगा । ना कि 2022 ओर python


अगर आपको values चाहिए यो आपको same dictionary बनाना होगा और print(x) की जगह print(dict1[x]) करना होगा 

And ये आपको values print करके दे देगा 

Example:-



dict1 = {'year':'2022','learn':'python'}
for x in dict1:
	print(dict1[x])
हम values() function का use करके भी values को print कर सकते है 

Example:-


dict1 = {'year':'2022','learn':'python'}
for x in dict1.values():
	print(x)
दोनों मैं फर्क क्या है तो जब आप

 for x in dictionary():

करते है तो वो सिर्फ index को print करता है और जब आप 

For x in dictionary.values(): 

कर देते है तो वो values को print करता है । 


हम items() function का use करके भी values को print करा सकते है । 

Example:-



dict1 = {'year':'2022','learn':'python'}
for x,y in dict1.items():
	print(x,y)
Output :- 

     Year       2022

     learn      Python 


एक ओर concepts 

अगर हमें values को change करना हो तो हम कैसे करेंगे । 

Example:- 









dict1 = {'year':'2022','learn':'python'}
dict1['year']= 2023
print(dict1)

Post a Comment

0 Comments