CS8, 10F, H12, due Tue Lecture 11.16—Miller/Ranum, 5.2.4 (pp. 163-165), file reading methods, more string formatting (review of 5.2.3)—Total points: ?

Available online as http://www.cs.ucsb.edu/~pconrad/cs8/10F/homework/H12—printable PDF

Name: (4 pts)   Umail Address: (4 pts)   @umail.ucsb.edu
Lab Section (2 pts)—circle one:  9am   10am   11am   noon   unknown   crashing 
You may collaborate with at most ONE other person on this homework assignment. If you do, please enter his/her name here:  
  (He/she should also enter your name on her/his assignment.)

This assignment is due in Lecture on Tuesday, 11.16.
It may ONLY be submitted in Lecture, at 2pm on Tuesday.

You must come IN PERSON to turn it in during your assigned Lecture section.



This homework continues the reading in chapter 5, picking up where the previous homework assignment left off. Please read section 5.2.4 (pp. 163-165).

The flip-side of this homework has some more exercises related to section 5.2.3 (pp. 161-163) (string formatting).

  1. (5 pts) Section 5.2.4 describes alternative file-reading method including infile.readline() vs. infile.readlines() (where infile is an open input file object—i.e. the variable you used with infile = open('filename.txt','r').

    Read about readline() and readlines() and try to understand the difference between them.
    Then, to make sure you understand, do Exercise 5.3, using the file 'students.txt' that you used in lab07 and lab09.

    You should do a call to open, then use the infile variable to call readline twice, then call readlines.
    Describe the result—that is if you open the file, and then call readline twice before calling readlines, what does the list returned by readlines contain? (Describe it in an english sentence—I don't want you to list all the output!)




  2. (5 pts) Now close the file and reopen it—i.e. do infile.close() and then
    call infile = open('filename.txt','r') again.

    Then do as Exercise 5.4 suggests—call readlines() immediately. What does the list that is returned contain?
    (Again—describe it in an english sentence—I don't want you to list all the output!)




  3. Now, try the same experiment—close the file and reopen it, but this time call read() with no parameter, i.e.:

    infile = open('students.txt','r')
    result = infile.read()


    1. (5 pts) What does result contain?
      (Again—describe it in an english sentence—I don't want you to list all the output!)



    2. (5 pts) What could you do to convert this into something like the result returned by readlines()?
      Describe the python code that would do that.


Please turn over for more...

...continued from other side

  1. You can do this exercise in two ways:

    • You can just type in the stuff in the left column, like a robot, and copy down what appears at the Python prompt.
      You'll get full credit for this assignment, but will learn very little. I don't recommend this technique.

    • Better: read pp. 161-163 and based on what you learn, try to predict what will happen before you hit return. (Perhaps you might write it on a piece of scratch paper.) Then, see if your answer was right. If it wasn't try to figure out where your understanding was mistaken.

    Either way, write the correct answer in the right column below.

    (20 pts) This is more practice with the % operator as used for string formatting.

    Some of the problems below, though may also contain % used as modulus, so be careful! Remember that % is the string formatting operator when the left operand is a string, and it is modulus when the left operand is a number.

    For each of the expressions in the left hand column of the table below, determine what the result of that expression will be if you type it in at the Python prompt, and put that in the right hand column—one character per square, starting from the left. The first few are done for you as examples.

    Assume that the following assignment statements have been typed in before these expressions are typed in.

    w = 12
    x = 34
    y = 56
    z = "Gaucho"
    
    Expression Result
    "%5d" % w '       1 2 '                
    "%2d" % (17 % 3) '   2 '                      
    len("%5d" % w) 5                            
    "%02d%02d%02d" % (9,11,1)                              
    "%02d:%02d%2s" % (3,9,"pm")                              
    "w=%3d y=%2d" % (w,y)                              
    "%3d%%" % 24                              
    "x=%d y=%d" % (x,y)                              
    "(%d,%d)" % (x,y)                              
    "%-7s" % z                              
    "%7s" % z                              
    "%3d" % (23 % 12)                              
    "%-10s" % ("(%d,%d)" % ( x,y))                              

    (Grading: Ten problems, 2 points each, no partial credit—output must be exact.)


End of H12