10.14. ExercisesĀ¶

Below are the datafiles that you have been using so far, and will continue to use for the rest of the chapter.

The file below is travel_plans.txt.

This summer I will be travelling.
I will go to...
Italy: Rome
Greece: Athens
England: London, Manchester
France: Paris, Nice, Lyon
Spain: Madrid, Barcelona, Granada
Austria: Vienna
I will probably not even want to come back!
However, I wonder how I will get by with all the different languages.
I only know English!

The file below is school_prompt.txt.

Writing essays for school can be difficult but
many students find that by researching their topic that they
have more to say and are better informed. Here are the university
we require many undergraduate students to take a first year writing requirement
so that they can
have a solid foundation for their writing skills. This comes
in handy for many students.
Different schools have different requirements, but everyone uses
writing at some point in their academic career, be it essays, research papers,
technical write ups, or scripts.

The file below is emotion_words.txt.

Sad upset blue down melancholy somber bitter troubled
Angry mad enraged irate irritable wrathful outraged infuriated
Happy cheerful content elated joyous delighted lively glad
Confused disoriented puzzled perplexed dazed befuddled
Excited eager thrilled delighted
Scared afraid fearful panicked terrified petrified startled
Nervous anxious jittery jumpy tense uneasy apprehensive
  1. The following sample file called studentdata.txt contains one line for each student in an imaginary class. The students name is the first thing on each line, followed by some exam scores. The number of scores might be different for each student.

    joe 10 15 20 30 40
    bill 23 16 19 22
    sue 8 22 17 14 32 17 24 21 2 9 11 17
    grace 12 28 21 45 26 10
    john 14 32 25 16 89

    Using the text file studentdata.txt write a program that prints out the names of students that have more than six quiz scores.

  1. Create a list called destination using the data stored in travel_plans.txt. Each element of the list should contain a line from the file that lists a country and cities inside that country. Hint: each line that has this information also has a colon : in it.

  1. Create a list called j_emotions that contains every word in emotion_words.txt that begins with the letter ā€œjā€.

10.14.1. Contributed ExercisesĀ¶

Please copy-paste your code solution from https://codeclubprojects.org/en-GB/python/robo-trumps/ ~

Q-1: After completing the reading, what concepts are still unclear to you? If nothing is unclear, what did you find most interesting?

Write code that stores the contents of the months list in a file named months.txt. Store one month per line.

Write the contents of the x and y lists to a file called xy2.dat with one value from x and y on each line, separated by a comma.

Generate 101 evenly spaced values between -5 and 5 and compute \(x^3\) of each of these values. Write the result to a file called pow3.csv with one value of \(x\) and \(x^3\) per line, separated by a comma. Your first two lines should look like


The seasonal average monthly rainfall in inches recorded at the Van Nuys Airport as of 2019 are provide in the file van_nuys_seasonal_rainfall.dat. Read in the data from the file and calculate and print out the

  1. total,

  2. mean, and

  3. standard deviation,

each on a separate line.

The standard deviation is given by

\[\sigma = \sqrt{\frac{1}{N-1}\sum_{i=1}^N (x_i - \bar{x})^2 },\]

where \(\bar{x}\) is the mean. You may use the sum function.


xy.dat contains two floating point numbers per line, separated by a comma. Read the contents of xy.dat and store the first number of each line in a list called x and the second in a list called y. Values should be stored as floats.


How often does “red” and “scarlet” appear in Sir. Arthur Conan Doyle’s “The Study in Scarlet”. Use the scarlet.txt file to determine and return your values as red_count and scarlet_count.

Using altair, plot a histogram of lengths of the words in the words5000.csv file. Your lengths should be saved in a list called list_len and passed to altair.

Using words5000.csv and scarlet.csv . Determine the counts for each part of speech in the story. The counts should be stored in a variable part_count and the parts of speech should be stored in a variable part_o_speech, they should be in the order the appear in the word list. Plot the histogram using altair. If the word isn’t in the 5000 word list skip it in the count.

Using the code from above, create a csv with the part_o_speech and part_count variables. They data should be comma separated and saved to a file named parts.csv

You have attempted of activities on this page
Next Section - 10.15. Chapter Assessment