2 A Closer Look at Python: Texts as Lists of Words

and it produces a Traceback message that shows the context of the error, followed by the name of the error, IndexError, and a brief explanation.

Such lines have the form: variable = expression. Python will evaluate the expression, and save its result to the variable. This process is called assignment.

Variables can hold intermediate steps of a computation, especially when this makes the code easier to follow.

3 Computing with Language: Simple Statistics

3.1 Frequency Distributions

3.2 Fine-grained Selection of Words -finding the long words in a text

the distribution of word lengths in a text: FreqDist out of a long list of numbers, where each number is the length of the corresponding word in the text

4 Back to Python: Making Decisions and Taking Control

4.1. Conditionals

4.2 Operating on Every Element

4.3 Nested Code Blocks

4.4. Looping with conditions

Exercises

  1. Try using the Python interpreter as a calculator, and typing expressions like 12 / (4 + 1).

The Python multiplication operation can be applied to lists. What happens when you type ['Monty', 'Python'] 20, or 3 sent1?

Review 1 on computing with language. How many words are there in text2? How many distinct words are there?

Compare the lexical diversity scores for humor and romance fiction in 1.1. Which genre is more lexically diverse?

Find the collocations in text5.

Consider the following Python expression: len(set(text4)). State the purpose of this expression. Describe the two steps involved in performing this computation.

Review 2 on lists and strings.

Define a string and assign it to a variable, e.g., my_string = 'My String' (but put something more interesting in the string). Print the contents of this variable in two ways, first by simply typing the variable name and pressing enter, then by using the print statement. Try adding the string to itself using my_string + my_string, or multiplying it by a number, e.g., my_string * 3. Notice that the strings are joined together without any spaces. How could you fix this?

Define a variable my_sent to be a list of words, using the syntax my_sent = ["My", "sent"] (but with your own words, or a favorite saying).

Use ' '.join(my_sent) to convert this into a string. Use split() to split the string back into the list form you had to start with.

☼ Review the discussion of conditionals in 4. Find all words in the Chat Corpus (text5) starting with the letter b. Show them in alphabetical order.

☼ Type the expression list(range(10)) at the interpreter prompt. Now try list(range(10, 20)), list(range(10, 20, 2)), and list(range(20, 10, -2)). We will see a variety of uses for this built-in function in later chapters.

◑ Use text9.index() to find the index of the word sunset. You'll need to insert this word as an argument between the parentheses. By a process of trial and error, find the slice for the complete sentence that contains this word.

◑ Using list addition, and the set and sorted operations, compute the vocabulary of the sentences sent1 ... sent8.

◑ What is the difference between the following two lines? Which one will give a larger value? Will this be the case for other texts?

What is the difference between the following two tests: w.isupper() and not w.islower()?

◑ Write the slice expression that extracts the last two words of text2.