Please send questions to st10@humboldt.edu .
Python 2.5 (r25:51918, Sep 19 2006, 08:49:13) 
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "copyright", "credits" or "license()" for more information.

IDLE 1.2      

>>> import sys
>>> sys.path.append('/Users/smtuttle/Humboldt/to-back-up/f06cis180py/180py_homeworks/180py_hw03')

>>> import hw3
>>> pig_latin

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    pig_latin
NameError: name 'pig_latin' is not defined

>>> hw3.pig_latin
<function pig_latin at 0x1108230>

>>> hw3.pig_latin("Howdy")
'owdy-Hay'

>>> # an alternative to import (a BIT different under-the-hood)
    #    (when you use this, you do not then need to precede the
    #    imported function names with the file/module name)
    #
    # note that you leave off the .py when typing this, too

>>> from hw3 import *

>>> pig_latin
<function pig_latin at 0x1108230>

>>> pig_latin("Spam")
'pam-Say'

>>> # a list is a collection of objects

>>> # to write a list: surround it with [ ].
>>> #    and separate the elements with commas
>>> # empty list: []
>>> # list of three integers: [1, 2, 3]

>>> []
[]

>>> type([])
<type 'list'>

>>> [1, 2, 3, 4]
[1, 2, 3, 4]

>>> type([1, 2, 3, 4])
<type 'list'>

>>> list1 = []
>>> list1
[]
>>> if type(list1) == list:
	print "list1 is a list!"

	
list1 is a list!

>>> # len is a built-in function that tells how many
>>> #    elements are in a sequence - whether a string
>>> #    or a list!

>>> len("Spam")
4

>>> len(list1)
0

>>> list1
[]

>>> list2 = ["a", 13, [], 3.4]
>>> len(list2)
4

>>> # I can grab an individual element of a list
>>> #    just like I grabbed an individual character
>>> #    from a string --- list_name[index]
>>> #    (where 1st element is index 0)
>>> #
>>> #   (that is, list_name[0] is the list's FIRST element,
>>> #    and list_name[ len(list_name)-1 ] is the LAST element

# ...but that index had better BE in the list, else you get an
#    error (no out-of-bounds indices allowed!)

>>> list1[0]

Traceback (most recent call last):
  File "<pyshell#44>", line 1, in <module>
    list1[0]
IndexError: list index out of range

>>> list2[0]
'a'

>>> list2
['a', 13, [], 3.3999999999999999]

>>> list2[1]
13

>>> list2[2]
[]

>>> list2[3]
3.3999999999999999

>>> list2[4]

Traceback (most recent call last):
  File "<pyshell#50>", line 1, in <module>
    list2[4]
IndexError: list index out of range

>>> # you can have slices of lists, too!
>>> #    list_name[a:b] will grab from the element
>>> #    with index a up to but not including the
>>> #    element with index b
>>> # (just like strings!)

>>> name = "Palin"
>>> name[1:3]
'al'

>>> list2[1:3]
[13, []]

>>> # I can do repetition, concatenation for lists
>>> #    like I can for strings, too!

>>> "hi " + "there"
'hi there'

>>> [1, 2, 3] + [4, 5]
[1, 2, 3, 4, 5]

>>> "Ni!" * 48
'Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!Ni!'

>>> ["Ni!", "Nay!"] * 48
['Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!', 'Ni!', 'Nay!']

>>> # what do you think happens if I call the list built-in
>>> #    function with a string?

>>> list("spam")
['s', 'p', 'a', 'm']

>>> list("Hello, how are you?")
['H', 'e', 'l', 'l', 'o', ',', ' ', 'h', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u', '?']

>>> # and we can use the in operator (membership operator)
>>> #    to see if something is in a list

>>> 'a' in 'spam'
True

>>> 'b' in 'spam'
False

>>> 'a' in list2
True

>>> list2
['a', 13, [], 3.3999999999999999]

>>> 'b' in list2
False

>>> # and, I can use a list in a for..in loop,
>>> #    to "walk through" the elements in a list

>>> for item in list2:
	print "<" + str(item) + ">"

	
<a>
<13>
<[]>
<3.4>

>>> # a list can contain any Python object - including
>>> #    another list

>>> matrix = [ [1, 2, 3],
	   [4, 5, 6],
	   [7, 8, 9] ]

>>> matrix
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

>>> matrix[0]
[1, 2, 3]

>>> matrix[0][0]
1

>>> # (note: there's a Python extension called NumPy
>>> #    that provides other ways of handling matrices, too)

>>> # lists are MUTABLE (can be changed in-place);
>>> # strings are NOT (you can create new strings easily,
>>> #    but you CAN'T change a character within a string

>>> name
'Palin'

>>> initial = name[0]

>>> name[0] = 'Q'

Traceback (most recent call last):
  File "<pyshell#104>", line 1, in <module>
    name[0] = 'Q'
TypeError: 'str' object does not support item assignment

>>> list2
['a', 13, [], 3.3999999999999999]

>>> list2[3] = 3

>>> list2
['a', 13, [], 3]

>>> L = ['spam', 'Spam', 'SPAM!']
>>> L
['spam', 'Spam', 'SPAM!']

>>> L[1] = 'eggs'
>>> L
['spam', 'eggs', 'SPAM!']

>>> # yes, I can even put a list slice on the LHS of an
>>> #    of an assignment statement!

>>> L[0:2] = ['eat', 'more']
>>> L
['eat', 'more', 'SPAM!']

>>> L[0: len(L)]
['eat', 'more', 'SPAM!']

>>> # handy way to think about this (a list slice on LHS
>>> #    of an assignment statement): think of it as:
>>> #    1. the slice you specify on the LHS is "deleted"
>>> #    2. the new items on the RHS are inserted where
>>> #       you deleted the elements

>>> L = [0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> L[3:5]
[3, 4]

>>> L[2:4] = ['a', 'b', 'c', 'd']
>>> L
[0, 1, 'a', 'b', 'c', 'd', 4, 5, 6, 7, 8]

>>> len(L)
11

>>> L[1:5] = ['x', 'y']
>>> L
[0, 'x', 'y', 'd', 4, 5, 6, 7, 8]

>>> L
[0, 'x', 'y', 'd', 4, 5, 6, 7, 8]

>>> L[2:5] = L[3:6]
>>> L
[0, 'x', 'd', 4, 5, 5, 6, 7, 8]

>>> # one of SEVERAL ways to delete from a list:
>>> # insert an empty list into a slice

>>> L
[0, 'x', 'd', 4, 5, 5, 6, 7, 8]

>>> L[1:3] = []
>>> L
[0, 4, 5, 5, 6, 7, 8]

>>> # there are LOTS of string methods!
>>> # a_list.count(element) returns how many times
>>> #    element is in a_list

>>> L
[0, 4, 5, 5, 6, 7, 8]

>>> L.count(5)
2
>>> L.find(5)

Traceback (most recent call last):
  File "<pyshell#144>", line 1, in <module>
    L.find(5)
AttributeError: 'list' object has no attribute 'find'

>>> L.replace(5, 55)

Traceback (most recent call last):
  File "<pyshell#145>", line 1, in <module>
    L.replace(5, 55)
AttributeError: 'list' object has no attribute 'replace'

>>> # a_list.append(element) adds element to the end of
>>> #    a_list

>>> L.append("dog")
>>> L
[0, 4, 5, 5, 6, 7, 8, 'dog']

>>> L.append([1, 2, 3])
>>> L
[0, 4, 5, 5, 6, 7, 8, 'dog', [1, 2, 3]]

>>> L[-1]
[1, 2, 3]

>>> L + [1, 2, 3]
[0, 4, 5, 5, 6, 7, 8, 'dog', [1, 2, 3], 1, 2, 3]

>>> # a_list.sort() changes a_list so its elements
>>> #    are in "alphameric" order
>>> # (if you don't like that ordering, you can pass
>>> # an ordering function...)

>>> L.sort()
>>> L
[0, 4, 5, 5, 6, 7, 8, [1, 2, 3], 'dog']

>>> names = ['Monty', 'Python', 'Michael', 'Palin']

>>> names.sort()
>>> names
['Michael', 'Monty', 'Palin', 'Python']

>>> # append and sort do NOT return ANYTHING -
>>> #    they change the list IN PLACE.
>>> #
>>> # point: DO NOT USE THESE IN AN ASSIGNMENT STATEMENT!!!!
>>> #
>>> # why? because append, sort don't return anything;
>>> #    so --- if you DO this, you, ah, nuke the LHS

>>> L
[0, 4, 5, 5, 6, 7, 8, [1, 2, 3], 'dog']

>>> L = L.sort()   # NOOOOOOOOO!!!!
>>> L
>>> type(L)
<type 'NoneType'>

>>> list2
['a', 13, [], 3]

>>> list2 = list2.append('hi')  # NOOOOOOOOO!!!
>>> list2
>>> type(list2)
<type 'NoneType'>

>>> # a_list.extend(another_list) - elements of another_list
>>> #    are added to the end of a_list

>>> L = [1, 2, 3, 4]

>>> L.extend([5, 6, 7])
>>> L
[1, 2, 3, 4, 5, 6, 7]

>>> # a_list.pop() - take the last element off the list,
>>> #    and return that element

>>> L.pop()
7
>>> L
[1, 2, 3, 4, 5, 6]

>>> L.pop()
6
>>> L
[1, 2, 3, 4, 5]

>>> L.push(18)

Traceback (most recent call last):
  File "<pyshell#191>", line 1, in <module>
    L.push(18)
AttributeError: 'list' object has no attribute 'push'

>>> last_thing = L.pop()
>>> last_thing
5

>>> # a_list.reverse() - a_list's elements are reversed

>>> L.reverse()
>>> L
[4, 3, 2, 1]

>>> # del - odd operator for deleting

>>> L
[4, 3, 2, 1]

>>> del L[0]
>>> L
[3, 2, 1]

>>> L.extend([8, 9, 10])
>>> L
[3, 2, 1, 8, 9, 10]

>>> del L[3:6]
>>> L
[3, 2, 1]

>>> # the range function returns a list -
>>> # range(val) the list is the integers from 0 to (val-1),
>>> # range(val1, val2) the list of integers from val1 to (val2-1)

>>> range(6)
[0, 1, 2, 3, 4, 5]

>>> range(5, 10)
[5, 6, 7, 8, 9]

>>> for i in range(30):
	print i

	
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

>>> for count in range(3, 10):
	print count

	
3
4
5
6
7
8
9

>>> # some string methods involve lists....
>>> #
>>> # string method split chops up a string into
>>> #    a list of substrings --- returns a list,
>>> #    that is;
>>> # how does it chop them? if you give it NO arguments,
>>> #    it chops based on white-space;
>>> # if you give it an argument, it chops based on that
>>> #    substring

>>> name = "Cleese Gilliam         Chapman  Idle Jones"

>>> name
'Cleese Gilliam         Chapman  Idle Jones'

>>> name.split()        # return a list of substrings based on spaces
['Cleese', 'Gilliam', 'Chapman', 'Idle', 'Jones']

>>> comsepList = "Cleese, Gilliam, Chapman, Idle, Jones"
>>> comsepList.split(',')
['Cleese', ' Gilliam', ' Chapman', ' Idle', ' Jones']

>>> comsepList.split(', ')
['Cleese', 'Gilliam', 'Chapman', 'Idle', 'Jones']

>>> stringy = "a,    b, c, d,    e"
>>> stringy.split(", ")
['a', '   b', 'c', 'd', '   e']

>>> # string method join takes a list as its argument,
>>> # and returns a new string that is the result of
>>> # concatenating the calling string between all the
>>> # list elements...

>>> comsepList
'Cleese, Gilliam, Chapman, Idle, Jones'

>>> actorsList = comsepList.split(', ')
>>> actorsList
['Cleese', 'Gilliam', 'Chapman', 'Idle', 'Jones']

>>> " ".join(actorsList)
'Cleese Gilliam Chapman Idle Jones'

>>> ",".join(actorsList)
'Cleese,Gilliam,Chapman,Idle,Jones'

>>> ", ".join(actorsList)
'Cleese, Gilliam, Chapman, Idle, Jones'

>>> "spam".join(actorsList)
'CleesespamGilliamspamChapmanspamIdlespamJones'

# example of playing with strings and lists...

>>> myString = "I am here for an argument, Sir!!"
>>> myString
'I am here for an argument, Sir!!'

# split the string on white-space

>>> myList = myString.split()
>>> myList
['I', 'am', 'here', 'for', 'an', 'argument,', 'Sir!!']

# write a function that strips non-alpha characters from a "word"

>>> def keep_letters( aWord ):
	result = ""
	for char in aWord:
		if char.isalpha():
			result = result + char
	return result

>>> keep_letters("Sir!!!")
'Sir'

# ...but, that includes stripping out blanks, too!

>>> keep_letters(myString)
'IamhereforanargumentSir'

# but, we COULD call it for EACH element in a string split into list
#    myList...

>>> newList = []
>>> for item in myList:
	newWord = keep_letters(item)
	newList.append(newWord)

>>> newList
['I', 'am', 'here', 'for', 'an', 'argument', 'Sir']

# and I could walk through the list, converting all of the strings
#    to lowercase

>>> myList = newList
>>> myList
['I', 'am', 'here', 'for', 'an', 'argument', 'Sir']
>>> for i in range( len(myList) ):
	myList[i] = myList[i].lower()

>>> myList
['i', 'am', 'here', 'for', 'an', 'argument', 'sir']

# perhaps I could sort those words alphabetically...

>>> myList.sort()
>>> myList
['am', 'an', 'argument', 'for', 'here', 'i', 'sir']

# and then, if I wanted, join them back into a list (separated,
#    here, by blanks)

>>> myNewString = " ".join(myList)
>>> myNewString
'am an argument for here i sir'

# and if I do the above a lot - perhaps I'll write a function to
#    make it easier:

>>> def alphaWordString(aString):
	wordList = aString.split()
	for i in range( len(wordList) ):
		wordList[i] = keep_letters( wordList[i] )
		wordList[i] = wordList[i].lower()
	wordList.sort()
	return " ".join(wordList)

>>> alphaWordString("That, sir, is an ex-parrot!!!")
'an exparrot is sir that'

>>> alphaWordString("OHHH!!!!! NO!!!!! not that!!!")
'no not ohhh that'

>>>