#!/usr/bin/python """ lists.py -- simple samples of a few list and tuple-related operations Jeff Ondich, 24 October 2006 Read, predict the output, experiment. Note that there's a crash in the code as it stands. Why does it crash? Comment out the offending line once you understand why it's offensive. """ print '=============== initializing and iterating through a simple list ==============' someList = ['moose', 'emu', 'goat', 'squid'] for item in someList: print item print print '=============== "slicing" a list ==============' someList = ['moose', 17, 'emu', 'goat', 'squid'] print 'someList --', someList print 'someList[2:3] --', someList[2:3] print 'someList[2:] --', someList[2:] print # Note the difference between ['ant'] and 'ant' in this context someList[2:4] = ['ant'] print 'someList --', someList someList[2:4] = 'ant' print 'someList --', someList print someList.append('cow') someList.extend(['kiwi']) print 'someList --', someList print print '=============== tuples: read-only lists ==============' someTuple = ('dog', 23, 'cat') for thing in someTuple: print thing # This will crash. print 'Try to add to a tuple...' someTuple.append('gerbil') print print '=============== more list operations ==============' # Go to http://docs.python.org/lib/typesseq.html # and play around with the various list operations.