Homework 6
Python practice
Run the python interpreter interactively to answer the
following questions.
- What does the ** operator do? Try 2**4, 2**5, etc. What associativity
does this operator have? (Try 2**3**2)
- Create a list (L) and call L.reverse(). What does it do?
Does this function change the list or create a new one?
- For the list created above, what happens if you type L[-1]? What happens
if you use an index that is out of bounds?
- 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.
- 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?
- Show how you could use a slice to remove the trailing newline from a
string read with readline().
- Show how you convinced yourself that all of the values listed below are
false.
0 0.0 "" '' [] () {} None Can you find any others?
- 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?
- Create a 4 character string S and type S[0][0][0]. What do you get? What
is happening here?
- What does the ord function return for a one-character string? Write a
loop that prints ord(ch) for each character in a string.
- Write a function square that squares its argument. Now type
map( square, (1,2,3,4))
What is map doing?
- 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.