Homework 6

Python practice

Run the python interpreter interactively to answer the following questions.

  1. What does the ** operator do? Try 2**4, 2**5, etc. What associativity does this operator have? (Try 2**3**2)
  2. Create a list (L) and call L.reverse(). What does it do? Does this function change the list or create a new one?
  3. For the list created above, what happens if you type L[-1]? What happens if you use an index that is out of bounds?
  4. Show how to get a slice that excludes the first and last elements of a list. What happens if you try to take a slice with the first index larger than the second? What happens if you assign a value to a slice? Try assigning to various types of objects as well as to single and multiple values.
  5. Can you assign a value to an element that is beyond the end of the list you created above? If this works, what is stored in the elements that weren't assigned?
  6. Show how you could use a slice to remove the trailing newline from a string read with readline().
  7. Show how you convinced yourself that all of the values listed below are false.
    0 0.0 "" '' [] () {} None
    Can you find any others?
  8. What happens if you try to add sequences of different types with the + operator? What happens if you append a sequence onto a list? What happens if you append a list onto itself?
  9. Create a 4 character string S and type S[0][0][0]. What do you get? What is happening here?
  10. What does the ord function return for a one-character string? Write a loop that prints ord(ch) for each character in a string.
  11. Write a function square that squares its argument. Now type
    map( square, (1,2,3,4))
    What is map doing?
  12. Type dir() and see what happens. Type dir( __builtins__) and see what happens.

This assignment should be submitted using

    submit tcole cs354 h6

Acceptable formats for electronically submitted assignments are text, OpenOffice or MicrosoftWord. I should be able to open your assignment with OpenOffice. Unless otherwise specified in the assignment, the entire assignment should be in a single document.