Python | Data Types | Dictionary & It's Conversion By. Shivansh Sir
PYTHON PROGRAMMING
PYTHON | DICTIONARY
Dictionary Data Type में keys और values की pairs होती है | Key value के pair को comma (,) से और key और value को colon (:) से separate किया जाता है | Dictionary के सभी keys और Values को curly braces ({}) में लिखा जाता है | ये एक Immutable data type है | PHP और Javascript में Associative Array का इस्तेमाल किया जाता है वैसे ही Python में Dictionary इस अलग नाम से इस्तेमाल किया जाता है | Dictionary; hashes की तरह होती है | Dictionary; mutable(changeable) होता है लेकिन Dictionary में values change हो सकती है और method(pop()) के जरिये keys भी change की जा सकती है लेकिन duplicate key नहीं दी जा सकती |
dict = {1:"H", 5:"e", 7:"l", 8:"l", 9:"o"}
print(dict[5])
print(dict[9])
Output
e
o
PYTHON | DICTIONARY | CREATING
Dictionary की keys और values को curly braces({}) के अन्दर लिखा जाता है |
Syntax
dict1 = {"name1":"Manish", "name2":"Astha", "name3":"Gaurav"}
dict2 = {}
PYTHON | DICTIONARY | ACCESSING VALUE
List और Tuple में value को access करने के लिए उनकी 'index' का इस्तेमाल किया जाता है | वैसे ही dictionary की values को access करना हो तो उनकी square brackets([]) में 'keys' का इस्तेमाल किया जाता है |
Source Code
dict = {"name1":"Manish", "name2":"Astha", "name3":"Gaurav"}
print(dict["name1"])
print(dict["name2"])
print(dict["name3"])
Output
Manish
Astha
Gaurav
Note :- Invalid key इस्तेमाल की जाती है तो, 'keyError' exception आ जाता है |
Source Code
dict = {"name1":"Manish", "name2":"Astha", "name3":"Gaurav"}
print(dict["name4"])
Output
print(dict["name4"])
KeyError: 'name4'
PYTHON | DICTIONARY | CHANGE DICTIONARY VALUES
Assignment Operator और square brackets([]) में keys की मदद से dictionary की values को change किया जा सकता है |
Source Code
dict = {"name1":"Manish", "name2":"Astha", "name3":"Gaurav"}
dict["name1"] = "Komal"
print(dict["name1"])
print(dict)
Output :
Nagesh
{'name1': 'Komal', 'name2': 'Astha', 'name3': 'Gaurav'}
PYTHON | DICTIONARY | CHANGE DICTIONARY KEYS
pop() method से dictionary की keys को change किया जा सकता है | pop() method से 'name1' key को remove करके दूसरी जगह पर 'name4' इस key को add किया गया है |
Source Code
dict = {"name1":"Manish", "name2":"Astha", "name3":"Gaurav"}
print("Before Changing Key :")
print(dict)
dict["name4"] = dict.pop("name1")
print("After Changing Key :")
print(dict)
Output
Before Changing Key :
{'name1': 'Manish', 'name2': 'Astha', 'name3': 'Gaurav'}
After Changing Key :
{'name2': 'Astha', 'name3': 'Gaurav', 'name4': 'Manish'}
PYTHON | DICTIONARY | ADD DICTIONARY
Dictionary में element add करने के लिए square bracket([]) में unique key name और उसकी value दी जाती है |
Source Code
dict = {"name1":"Manish", "name2":"Astha", "name3":"Gaurav"}
print("Before Adding Element :")
print(dict)
dict["name4"] = "Komal"
print("After Adding Element :")
print(dict)
Output
Before Adding Element :
{'name1': 'Manish', 'name2': 'Astha', 'name3': 'Gaurav'}
After Adding Element :
{'name1': 'Manish', 'name2': 'Astha', 'name3': 'Gaurav', 'name4': 'Komal'}
PYTHON | DICTIONARY | DELETE ELEMENT
Dictionary में element को delete करने के लिए 'del' Operator का इस्तेमाल किया जाता है |
Source Code
dict = {"name1":"Manish", "name2":"Astha", "name3":"Gaurav"}
print("Before Deleting Element :")
print(dict)
del dict["name2"]
print("After Deleting Element :")
print(dict)
Output
Before Deleting Element :
{'name1': 'Manish', 'name2': 'Astha', 'name3': 'Gaurav'}
After Deleting Element :
{'name1': 'Manish', 'name3': ' Gaurav'}
PYTHON | DICTIONARY | DICTIONARY FUNCTION
1. all() :- Sequence के सभी elements True होते है तो ये तो ये True return करता है |
2. any() :- Sequence का एक या सभी elements True होते है तो ये तो ये True return करता है |
3. dict() :- इसका इस्तेम्मल नए ढंग से dictionary को create करने के लिए किया जाता है |
4. len() :- Dictionary की length को return करता है |
5. sorted() :- Sequence को sort करके return करता है |
6. str() :- Dictionary को string में convert करके return करता है |
PYTHON | DICTIONARY | DICTIONARY METHOD
1. clear() :- Dictionary को clear करता है |
2. copy() :- Dictionary को copy करके return करता है |
3. fromkeys() :- Sequence के item को dictionary के key के रूप में return करता है |
4. get() :- Key की value को return करता है |
5. has_key() :- दी हुई key; dictionary पर है या नहीं ये boolean value में return करता है |
6. items() (key, value) :- इस नए प्रकार से dictionary को return करता है |
7. keys() :- Dictionary के सिर्फ keys को return करता है |
8. pop() :- Key को remove करके उसकी value return की जाती है | अगर दी हुई key नहीं मिल जाती तो set की हुई default value return की जाती है |
9. popitem() :- Dictionary के last item को remove करके (key, value) return करता है |
10. setdefault() :- दी हुई key exist होती है तो उसकी value return की जाती है | अगर key नहीं होती है तो set की हुई default value को return किया जाता है |
11. update() :- Old dictionary को update करके update हुई dictionary को return करता है |
12. values() :- Dictionary के सिर्फ keys की values को return करता है |
For Any Doubt Clear on Telegram Discussion Group
Comments
Post a Comment