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

Peggy Russell prusselltechgroup at gmail.com
Tue Jul 5 02:41:28 UTC 2011


================== Homework ======================

1. How would you count the number of words in a single string?
   Assume words are separated by spaces
   -----------------------------------------------------------------------------
   python3
   >>> veggies = "peas,green beans,spinach,corn,squash"
   >>> print("Word Count:", len(veggies.split(",")))
   Word Count: 5
   -----------------------------------------------------------------------------

2. What does an index of [-1], or another negative number mean in a
   list or string?
   -----------------------------------------------------------------------------
   The right end of the list or string.

   python3
   >>> ### String
   >>> veggies = "peas,green beans,spinach,corn,squash"
   >>> print(veggies[-1])
   h
   >>> print(veggies[-5:-1])
   quas
   >>> print(veggies[-5:])
   quash

   python3
   >>> ### List
   veggies=['peas','green beans', 'spinach', 'corn', 'squash']
   >>> print(veggies[-1])
   squash
   >>> print(veggies[-5:-1])
   ['peas', 'green beans', 'spinach', 'corn']
   >>> print(veggies[-5:])
   ['peas', 'green beans', 'spinach', 'corn', 'squash']
   -----------------------------------------------------------------------------

3. Who is Guido van Rossum and why am I using him as an example?
   -----------------------------------------------------------------------------
   Author of python. His last name is two words which works well in the
   splice example.  Source: http://www.python.org/~guido/
   ** Plus it reinforces everything 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.
   -----------------------------------------------------------------------------
   words = ['', 'one', 'two', 'three', 'four', 'five']
   numWords = len(words)
   print("Number of words: ", numWords)
   print("Print 1 through 5" + "\n" + "-"*17)
   # Note: Index starts at 0
   for i in range(1, numWords):
     print(i, words[i])
   -----------------------------------------------------------------------------

5. (a) Plot a histogram graph from a list of numbers, with each number in the
       list on its own line.
   (b) vals = [ 0, 2, 4, 8, 16, 18, 17, 14, 9, 7, 4, 2, 1 ]
   (c) The first line has no stars, the second has two, then 4, etc.
   (d) Hint: you'll need two loops, one inside the other.
   -----------------------------------------------------------------------------
   #============================================================================
   # Description:
   #   Given a list of integers, print histogram using 2 loops.
   #
   # Revisions (v1.0):
   #   2011.07.05 - 1.0 Created.
   #============================================================================
   vals = [ 0, 2, 4, 8, 16, 18, 17, 14, 9, 7, 4, 2, 1 ]

   for i in range(len(vals)):
     hline=''
     for pts in range(vals[i]):
       hline += '*'
     print("{0:2d}  {1}".format(vals[i],  hline))
   -----------------------------------------------------------------------------


More information about the Courses mailing list