python-tutorial icon indicating copy to clipboard operation
python-tutorial copied to clipboard

dict.get() example needs clarification

Open Akuli opened this issue 4 years ago • 1 comments

<Arrinao> sentence = input("Enter a sentence: ")
<Arrinao> counts = {}
<Arrinao> for word in sentence.split():
<Arrinao>     counts[word] = counts.get(word, 0) + 1
<Akuli> two cases: word is not in counts, word is in counts
<Akuli> word not in counts:   counts[word] = 0+1
<Akuli> s o when we see it for the first time, set count to 1
<Akuli> and when word is in counts, it does:   counts[word] = counts[word] + 1
<Akuli> so when we see a word we already saw before, we increment the count

Akuli avatar Jun 25 '21 17:06 Akuli

@Akuli I get it this is some kind of histogram. I'll take this issue.

Anas-debug avatar Jan 03 '22 01:01 Anas-debug