Lesson 19 of 34

Dictionaries: பெயர் → மதிப்புDictionaries: Names to Values

10 min 30 XP

dictionary என்பது ஒரு அகராதி போல: ஒவ்வொரு key-க்கும் ஒரு value. { } அடைப்புக்குறியில் key: value என்று எழுதுங்கள். Index-க்கு பதிலாக key-ஐப் பயன்படுத்தி மதிப்பைப் பெறலாம்.

A dictionary works like a real dictionary: every key has a value. Write key: value pairs inside { }. You look values up by key instead of by index.

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() பயன்படுத்தினால் error இல்லாமல் None அல்லது நாம் சொல்லும் மதிப்பு கிடைக்கும்.

Asking for a missing key gives an error. get() returns None, or a default you choose, instead.

Example: edit me and press Run

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

items() மூலம் key மற்றும் value இரண்டையும் loop-ல் பெறலாம்.

Use items() to loop over keys and values together.

Example: edit me and press Run

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

எழுத்துகளை எண்ண dictionary மிகச் சிறந்தது: count[ch] = count.get(ch, 0) + 1. இது interview-களில் அடிக்கடி வரும்!

Dictionaries are perfect for counting: count[ch] = count.get(ch, 0) + 1. This comes up in interviews all the time!

Challenge

+30 XP

ஒரு வார்த்தையை input() மூலம் பெறுங்கள். ஒவ்வொரு எழுத்தும் எத்தனை முறை வருகிறது என்று dictionary-ல் எண்ணி, அந்த dictionary-ஐ print செய்யுங்கள். உதாரணம்: "hello" → {'h': 1, 'e': 1, 'l': 2, 'o': 1}

Read a word with input(). Count how many times each letter appears using a dictionary, then print the dictionary. Example: "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.