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

Leslie leslie.brothers at verizon.net
Sat Jul 2 17:42:24 UTC 2011


On Fri, 2011-07-01 at 21:57 -0700, Akkana Peck wrote:


> ================== Homework ======================
> 
> 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.
string = "It's a fabulous day."
words = string.split()
number = len(words)
number


> 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 guessed it would wrap around to the last item. Looks like it does.

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

I had to look him up. Now I know who invented Python!

> 4. Rewrite the exercise from lesson 2, the one where
>    you printed "one", "two", "three", "four", "five",
>    using a list instead of a series of if-elif.

string = "one two three four five"
words = string.split()
for c in words :
	print c

> 
> 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.


vals = [ 2,  4,  6,  8 ]
n = len(vals)

for y in range(0,  n) :
    x = vals [ y ]

    for i in range (0,  x) :
        print "*", 
    print "\n"

> _______________________________________________
> Courses mailing list
> Courses at linuxchix.org
> http://mailman.linuxchix.org/mailman/listinfo/courses




More information about the Courses mailing list