Question: %%% Python Jupyter notebook %%% words.txt - file Writing programs or programming is a very creative and rewarding activity You can write programs for many

%%% Python Jupyter notebook %%%

words.txt - file

Writing programs or programming is a very creative and rewarding activity You can write programs for many reasons ranging from making your living to solving a difficult data analysis problem to having fun to helping someone else solve a problem This book assumes that {\em everyone} needs to know how to program and that once you know how to program, you will figure out what you want to do with your newfound skills

We are surrounded in our daily lives with computers ranging from laptops to cell phones We can think of these computers as our personal assistants who can take care of many things on our behalf The hardware in our current-day computers is essentially built to continuously ask us the question What would you like me to do next

Our computers are fast and have vasts amounts of memory and could be very helpful to us if we only knew the language to speak to explain to the computer what we would like it to do next If we knew this language we could tell the computer to do tasks on our behalf that were reptitive Interestingly, the kinds of things computers can do best are often the kinds of things that we humans find boring and mind-numbing

%%% Python Jupyter notebook %%% words.txt - file Writing programs or programming

# Code here

# open the file f = open("words.txt") # read the text and store in variable text = ________ # initialize a dictionary word_counts = ________ # populate the dictionary with words and their associated counts in the text # for each word in the text (split by spaces) # if we haven't seen it # initialize it in the dictionary to a value of 1 # otherwise # update the count by 1 # print out the word counts

Exercise 2 A common use case for dictionaries is to store word counts. Let's modify our program from Exercise 1 to store the count for each unique word. Let's rename the dictionary word counts This means you need to initialize the count value to 1 if you see it for the first time (i.e., it's not already in word counts) update the count (by adding 1 to it) if you see the In [ ]: | # Code here # open the file f -open("words.txt") # read the text and store in variable text - # initialize a dictionary word counts - # populate the dictionary with words and their associated c # for each word in the text (split by spaces) # if we haven't seen it # initialize it in the dictionary to a value of 1 # otherwise # update the count by 1 # print out the word counts

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!