Lesson 19 of 34

Dictionaries: नाम से मान

10 min 30 XP

dictionary असली शब्दकोश की तरह है: हर key का एक value होता है। { } के अंदर key: value जोड़े लिखें। मान index से नहीं, key से ढूँढे जाते हैं।

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

जो key मौजूद नहीं, उसे माँगने पर error आता है। get() इसकी जगह None, या आपका चुना हुआ default लौटाता है।

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

keys और values को साथ-साथ loop करने के लिए items() का उपयोग करें।

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

गिनती के लिए dictionaries बेहतरीन हैं: count[ch] = count.get(ch, 0) + 1। यह इंटरव्यू में बार-बार आता है!

Challenge

+30 XP

input() से एक शब्द पढ़ें। dictionary से गिनें कि हर अक्षर कितनी बार आया, फिर dictionary print करें। उदाहरण: "hello" → {'h': 1, 'e': 1, 'l': 2, 'o': 1}

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.