def count_chars(s):
'''Return a dict that contains each character in str s as a key. The
value associated with each key is the number of times that character
occurs in s.'''
d = {}
for ch in s:
if ch in d:
d[ch] += 1
else:
d[ch] = 1
return d
我没有得到代码中的第三行“if ch in d”。如果字典中还没有任何条目,为什么该字符会出现在字典中?
另外,我不明白 d[ch] += 1 应该是什么意思,以及为什么会有 else 语句。有人可以帮帮我吗?