If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
Python में dictionary उन 4 built in data types (list , tuple , set , dictionary) में से एक है जिसका use data collections को store करने किया जाता है या कह सकते हैं multiple values store करने के लिए किया जाता है।
Python में dictionary का use key : value type का data store करने के लिए किया जाता है। मतलब dictionary में से कोई particular value / item निकालने के लिए key का use करेंगे।
जबकि list , tuple , set में indexed data stored होता था। जिसे हम item index या for loop के through access करते थे।
Python में dictionary ordered , changeable है और unique key ही allow करता है।Ordered - ordered मतलब जिस order में हमने items को dictionary में insert किया था हमें उसी order में मिलेंगे।
unique key - means हम same key नहीं रख सकते हैं , value तो same हो सकती है लेकिन key नहीं।
Changeable - मतलब उन items को जरूरत पड़ने पर update कर सकते हैं या dictionary में नए items को add कर सकते हैं।
Python version 3.6 तक तो dictionary unordered थी , लेकिन version 3.7 में update किया गया और अब ये ordered ही हैं।
dictionary को curly brackets { } किया जाता है जिसमे key : value pair में data store करते हैं। और एक से ज्यादा items होने पर उन्हें comma , से separate करते हैं।
d = { 'key1' : 'value1', 'key2' : 'value2', . . . 'key_n' : 'value_n', }
d = {
'name' : 'Aryendra Sharma',
'age' : 25,
'city' : 'Agra',
'country' : 'India'
}
print(d)
C:\Users\Rahulkumar\Desktop\python>python dict_ex.py {'name': 'Aryendra Sharma', 'age': 25, 'city': 'Agra', 'country': 'India'}
Dictionary की length जानने के लिए len() function का use किया जाता है और , type जानने के लिए type() function का use किया जाता है।
See Example -
d = {
'name' : 'Aryendra Sharma',
'age' : 25,
'city' : 'Agra',
'country' : 'India'
}
print('Type :', type(d))
print('Length :', len(d))
C:\Users\Rahulkumar\Desktop\python>python dict_type_len.py Type : <class 'dict'=""> Length : 4
Dictionary की keys बनाते समय कुछ बातों का जरूर ध्यान रखें -
keys को आप String (जैसे : 'name', 'age') , Boolean (जैसे : True , False) , numeric (जैसे : 0 , 1 , 1.5) type का ले सकते है।
even empty String (जैसे : '', ' ', ' ', ' ') भी ले सकते हैं। यहां empty string with single space , double space अलग - अलग count होंगी।
लेकिन आप set (जैसे : {3}) , list (जैसे : [4]), tuple (जैसे : (5) ) को as a key नहीं ले सकते हैं , हाँ as a value तो ले सकते हैं उसमे कोई problem नहीं है।
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook