aL = [7, 8.25, 9, 10, "hello", "goodbye"] # a list is like an array # but it is an object with its own methods # can contain multiple data types print "list length = " + str(len(aL)) # len returns an int, so cast to string nItems = len(aL) print range(0,nItems) # range returns a list from start to end-1 print '\n------------------get each element by index number--------------------' for j in range(0,nItems): print aL[j] print " " + str(type(aL[j])) # type returns a type object, cast to string print '\n------------------get each element by magic iterator--------------------' for j in aL: # magic list iterator print j print '\n------------------slice a list--------------------' b = aL[0:2] # includes elements index 0 and 1 but not [2] print b # in interpreter, get help on a list object # help( [] ) # see __getslice__ how it overloads the colon operator # same for help(str) # avoid cookbook approach because if you can't find the right recipe you could end up with a foul tasting soup. # better to learn the elements of the language, establish some tools, build on tools # keywords """ built-in functions dir() dir(__builtins__) type() interpreter help(str) dir(str) """ print '\n------------------create a list, reverse, and sort--------------------' a = range(5) # the range function generates a list [0, 1, 2, 3, 4] print a a.reverse() # list is an object, has methods that you can call # list.reverse() works in place, does not return a new list print a a.sort() print a # think of list as a row with 5 columns. a[2] refers to the third column