Activity 2-1

Adapted from Brown University CSCI0931. Used with Permission.

Task 1: Review split

  1. On the Desktop, make a folder called ACT2-2. Download stringSplit.py into the ACT2-2 folder.
  2. Open IDLE, and then open stringSplit.py.
  3. In the ACT2-2 window, press F5 (or select Run > Run Module). What happens?
  4. What do you think that "\n" stands for? Add a print statement into your code with the answer.
  5. Write split() statements to try the following delimiters:
  6. Write a print statement to explain what split does, and what a delimiter is, in your own words.

Task 2: Read in a file, and do some simple statistics

  1. Download readFile.py and tiger.txt into your ACT2-2 folder.
  2. In readFile.py, we want to write a program that will read in William Blake's poem "The Tiger", the words of which are in tiger.txt.
    1. Download the tiger poem, and assign the file location as a string to a variable named fileName.
    2. Write an expression that opens the file at fileName for reading and assigns the return value to inFile. The value of inFile now a filehandle that has been opened and can be read.
    3. Assign to fileString the contents of the file, which are returned by calling the read function on the file. fileString is now a big string that contains the poem.
    4. Close the file object myFile; you don't need it any longer, and this will let Python clean up some of the memory space that you used.
    5. We want to count the number of words in the file. Use the split function to create a list of words from the string, separated by whitespace. Print that list to see what you get. Can you use this list to find the number of words in the file? Print out the number of words in the file. (Hint: Remember Activity 2-1, Task 1, Step 3f?)
    6. Now we want to count the number of sentences in the file. Use the split function again, but this time with the appropriate delimiter to get a list of sentences. Print the list to make sure that you are getting the correct thing. Print out the number of sentences in the file.

Task 3: Test your Loops!

  1. Go to File... New Window and start a new program called testLoops.py
  2. Using only one programming statement, print(i*2), in the loop body, write a loop that will print out the numbers 2, 6, 10, 14, 18, 20 (one number on each line).
  3. Using only one programming statement, print(i), in the loop body, write a loop that will print out the numbers 3, 8, -1, 35, 22, 0 (one number on each line).
  4. Using only one programming statement, print(i), in the loop body, write a loop that will print out the numbers 8, 8, 8, 8, 8 (one number on each line).
  5. Using only the following programming statements and the appropriate indentation, write a loop that will print the following output: The factorial of 5 is 120
    • print("The factorial of 5 is", factorial)
    • factorial = 0
    • factorial = 1
    • for i in [1, 3, 5, 7, 9]
    • for i in [1, 2, 3, 4, 5]
    • for i in [2, 4, 3, 8, -1]
    • factorial = factorial + i
    • factorial = factorial * i
    Note: (1) You will not have to use all the programming statements, and (2) the factorial of a number n is calculated as n * (n-1) * (n-2) * (n-3) * ... *2 * 1. Therefore, the factorial of 5 is calculated as 5*4*3*2*1 = 120

Task 4: Compute the Average Word Length in "Tiger Tiger"

We will go back to your readFile.py program for this task.
  1. We will use the list of words that you generated for Task 2, Step 2e.
  2. Create a variable with the value 0 that will sum up the lengths of all words.
  3. Use a for-loop to iterate over all words.
    1. For each word, get its length using the len() function (try doing a len("word") in the IDLE interpreter if you forget how to use it for strings.)
    2. Add the length of the current word to the variable that you created in Step 2.
  4. With the total sum of all word-lengths (i.e., the total number of non-whitespace characters), get the average by dividing this sum by the number of words. Print out the average length of the words in the poem.

Task 5: Find the number of words longer than 4 characters in "Tiger tiger"

We will write a new loop for your readFile.py program for this task.
  1. We will use the list of words that you generated for Task 2, Step 2e.
  2. Create a variable with the value 0 that will count the number of words that we are interested in (i.e. the number of words with length > 4).
  3. Use a for-loop to iterate over all words.
    1. For each word, get its length using the len() function (try doing a len("word") in the IDLE interpreter if you forget how to use it for strings.)
    2. If the length of the word is greater than 4, add one to the variable that you created in Step 2.
  4. Print out the number of words with length > 4 in the poem.