🟢 Beginner · Lesson 14
Python Dictionaries
Dictionary क्या है?
Dictionary data को key : value pairs के रूप में curly braces { } में रखती है। आप key से value ढूंढते हैं — असली dictionary की तरह जहाँ शब्द key है और अर्थ value।
Python – dict.py
student = {"name": "Aman", "class": 12, "marks": 95}
print(student["name"])
print(student["marks"])Aman
95
Access, Add, Update
Python – modify.py
student = {"name": "Aman", "marks": 95}
student["class"] = 12 # नई key जोड़ें
student["marks"] = 98 # मौजूदा update करें
print(student){'name': 'Aman', 'marks': 98, 'class': 12}
- नई key को assign करने पर वह जुड़ जाती है।
- मौजूदा key को assign करने पर वह update होती है।
Dictionary Methods
| Method | क्या करता है |
|---|---|
| .keys() | सभी keys |
| .values() | सभी values |
| .items() | सभी key-value pairs |
| .get(k) | k की value (missing पर error नहीं) |
| .pop(k) | key k हटाएं |
Program 1: Student Record
Python – record.py
student = {
"name": "Riya",
"roll": 12,
"marks": [88, 92, 79]
}
print("Name :", student["name"])
print("Roll :", student["roll"])
print("Total:", sum(student["marks"]))Name : Riya
Roll : 12
Total: 259
Value खुद एक list हो सकती है — यहाँ marks में तीन numbers हैं जिन्हें sum() से जोड़ते हैं।
Program 2: Dictionary पर Loop
Python – loopdict.py
prices = {"pen": 10, "book": 50, "bag": 400}
for item, price in prices.items():
print(item, "costs", price)pen costs 10
book costs 50
bag costs 400
.items() हर key और value एक साथ देता है, इसलिए loop दोनों एक बार में पढ़ता है।
Program 3: Word Frequency गिनें
Python – freq.py
text = "apple banana apple cherry banana apple"
words = text.split()
freq = {}
for w in words:
freq[w] = freq.get(w, 0) + 1
print(freq){'apple': 3, 'banana': 2, 'cherry': 1}
freq.get(w, 0)मौजूदा count या नया word होने पर 0 लौटाता है।- हर बार word आने पर 1 जोड़ते हैं — classic counting pattern।
Program 4: छोटा Phone Book
Python – phonebook.py
book = {}
book["Aman"] = "98765"
book["Riya"] = "91234"
name = input("Whose number? ")
if name in book:
print(name, ":", book[name])
else:
print("Not found")Whose number? Riya
Riya : 91234
सामान्य गलतियाँ
- missing key को
dict[key]से access करने पर KeyError —.get()use करें। - Keys unique होनी चाहिए; दोहराई key पुरानी value overwrite करती है।
- Lists keys नहीं हो सकतीं (keys immutable होनी चाहिए)।
Practice Tasks
- 3 students और उनके marks की dictionary बनाकर topper print करें।
- किसी word में हर character कितनी बार आता है, गिनें।
- छोटी English-Hindi dictionary बनाकर एक word ढूंढें।
- Product prices store करके कुल bill print करें।
सारांश
- Dictionaries
{ }में key:value pairs रखती हैं। - Key से access; assign करने पर जुड़ता या update होता है।
- Loop के लिए
.items(), KeyError से बचने के लिए.get()। - Records, counting और lookups के लिए बढ़िया।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
Dictionary पढ़ते समय KeyError से कैसे बचें?
d.get(key) या d.get(key, default) इस्तेमाल करें, जो error की जगह None या default देता है।
क्या dictionary की keys lists हो सकती हैं?
नहीं। Keys immutable (hashable) होनी चाहिए जैसे str, int या tuple। Values किसी भी type की हो सकती हैं।
Keys और values पर एक साथ loop कैसे चलाएँ?
for key, value in d.items(): इस्तेमाल करें।
💻 लाइव कोड एडिटर
इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
