[Courses] [python] Lesson 3: Fun with Strings and Lists

Kay Nettle pkn at cs.utexas.edu
Mon Jul 4 18:04:03 UTC 2011


 
 >1. How would you count the number of words in a single string?
 >   Assume words are separated by spaces ... don't worry about
 >   things like newlines, commas or hyphens.

foo="""This is just a test to
... see what happens when I have
... a long string"""
>>> len(foo.split())
15
>>> 

 >
 >2. What does an index of [-1], or another negative number mean in a
 >   list or string? Take a guess, then try it and see if you were right.

I found this out in lesson two from looking at list.  It will print the
last element in the list or string


 >3. Who is Guido van Rossum and why am I using him as an example?

He wrote python.

 >
 >5. This one's a little harder, but give it a try if you have time.
 >   Plot a histogram graph from a list of numbers, with each number in
 >   the list on its own line.
 >
 >   For instance, if you start with numbers like this:
 >vals = [ 0, 2, 4, 8, 16, 18, 17, 14, 9, 7, 4, 2, 1 ]
 >   you might plot something like this:
 >

>>> for val in vals:
...    star = ""
...    for num in range(val):
...        star += '*'
...    print star
... 

**
****
********
****************
******************
*****************
**************
*********
*******
****
**
*
>>> 


More information about the Courses mailing list