for letter in sentencefrequencies[letter+ return frequencies this code looks like it couldn' possibly work the defaultdict accepts function in its constructor whenever key is accessed that is not already in the dictionaryit calls that functionwith no parametersto create default value in this casethe function it calls is intwhich is the constructor for an integer object normallyintegers are created simply by typing an integer number into our codeand if we do create one using the int constructorwe pass it the item we want to create (for exampleto convert string of digits into an integerbut if we call int without any argumentsit returnsconvenientlythe number zero in this codeif the letter doesn' exist in the defaultdictthe number zero is returned when we access it then we add one to this number to indicate we've found an instance of that letterand the next time we find onethat number will be returned and we can increment the value again the defaultdict is useful for creating dictionaries of containers if we want to create dictionary of stock prices for the past dayswe could use stock symbol as the key and store the prices in listthe first time we access the stock pricewe would want it to create an empty list simply pass list into the defaultdictand it will be called every time an empty key is accessed we can do similar things with sets or even empty dictionaries if we want to associate one with key of coursewe can also write our own functions and pass them into the defaultdict suppose we want to create defaultdict where each new element contains tuple of the number of items inserted into the dictionary at that time and an empty list to hold other things nobody knows why we would want to create such an objectbut let' have lookfrom collections import defaultdict num_items def tuple_counter()global num_items num_items + return (num_items[] defaultdict(tuple_counterwhen we run this codewe can access empty keys and insert into the list all in one statementd defaultdict(tuple_counterd[' '][ append("hello" [' '][ append('world'