Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🟢 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

  1. 3 students और उनके marks की dictionary बनाकर topper print करें।
  2. किसी word में हर character कितनी बार आता है, गिनें।
  3. छोटी English-Hindi dictionary बनाकर एक word ढूंढें।
  4. 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(): इस्तेमाल करें।
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
अगला topicPython में String Handling आपकी प्रगति: 14 / 66 (21%) · Lesson 15 / 66 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें