in section we will learn how to read from text file for nowhere' line of code that reads the entire contents of file containing the text of shakespeare' romeo and juliet and stores the contents in string called texttext open'romeoandjuliet txt 'read(to get at the individual wordswe will use the split method to turn the string into list of its individual words alsobecause some words may be capitalizedwe will convert the whole string to lowercase we also have to remove punctuation from string import punctuation text text lower(for in punctuationtext text replace( ''words text split(next comes the dictionary code that does the counting the dictionary keys will be the words from the text and the values will be counts of how many time each word appears we start with an empty dictionary then for every word in the list of wordsif we have seen the word beforewe add one to its countand otherwise we set the count for that word equal to here is the coded {for in wordsif in dd[wd[ elsed[ once we have created the dictionarywe can use the following code to print the items in alphabetical orderitems list( items()items sort(for in itemsprint(ithe way this works is little tricky remember that items(returns list of pairs (called tuples)which are lot like lists when we sort list of tuplesthe sorting is done by the first entrywhich in this case is the word so the sorting is done alphabetically if we instead want to order things by frequencywe can flip the order of the tuples and then sortitems list( items()items [( [ ] [ ]for in itemsitems sort(for in itemsprint(ihere is the code all togetherfrom string import punctuation read from fileremove caps and punctuationand split into words text open'romeoandjuliet txt 'read(