Python की मूल बातें

Python Dictionaries: Keys, Values and Loops

Look up a value by its name instead of its position. Create, update and loop over key-value pairs.

  • शुरुआती
  • 2 मिनट में पढ़ें
  • 1 चलाने योग्य उदाहरण
आप क्या सीखेंगे
  • Creating dictionaries and reading values
  • Adding, updating and deleting keys
  • Looping with .items() and using .get()
student
"name""Ada"
"score"92
"course""Python"
student["score"] finds 92 directly, without searching.

Keys point to values

Dictionary unique key से तेज lookup देता है और अलग-अलग प्रकार के values रख सकता है।

मुख्य बातें

  1. student = {"name": "Ada"} maps the key "name" to the value "Ada".
  2. student["name"] reads a value; a missing key raises KeyError.
  3. student.get("age", 0) returns a default instead of failing.
  4. Keys must be unique and immutable, such as strings or numbers.

इसका उपयोग कब करें?

  • चलने योग्य code से Python syntax सीखें।
  • बड़ी programming समस्याओं से पहले मजबूत आधार बनाएँ।
  • स्थानीय setup के बिना किसी विचार को तुरंत जाँचें।

देखें यह कैसे काम करता है

एक key लिखें और उसे सहेजें या खोजें। Python key को हैश करके उसकी जगह सीधे ढूँढ लेता है, हर एंट्री देखने की ज़रूरत नहीं पड़ती।

उदाहरण कोड

कोड चलाएँ →
main.py
student = {"name": "Ada", "score": 92}
student["course"] = "Python"

for key, value in student.items():
    print(f"{key}: {value}")
इस उदाहरण के लिए Run बटन बनाएँ

अपेक्षित आउटपुट

name: Ada
score: 92
course: Python

यह कैसे काम करता है

Square brackets value जोड़ते या बदलते हैं; items iteration के लिए key और value देता है।

मान बदलें और Python इंस्टॉल किए बिना CodeUtility ऑनलाइन Python कंपाइलर में प्रोग्राम चलाएँ।

अभ्यास के कार्य

बड़े dataset पर जाने से पहले input बदलें और edge cases की जाँच करें।

  1. Code चलाने से पहले output का अनुमान लगाएँ।
  2. खाली और गलत input संभालें।
  3. Logic को function में रखें और tests जोड़ें।
Python IDE में चलाएँ →

चैलेंज से अभ्यास करें

इस पाठ में सीखी बात से एक असली समस्या हल करें। हर चैलेंज ब्राउज़र में चलता है, तुरंत टेस्ट और AI रिव्यू के साथ।

सभी चैलेंज