An Intro to
Programming
with Python

Fall 2006
course
navigation

oct 30

# lists list = [ 'zero', 'one', 'two', 'three', 'four' ] print list[0] # a single element print list + ['five', 'six'] # concatenation print list * 2 # repitition print len(list) # length print list[1:2] # slice print list[:2] # slice from beginning print list[2:] # slice to end for word in list: # iteration print word if 'two' in list: # membership check print 'two is in list' # most of these modify the list; they don't return a new one. list.append('six') # add something to end of list list.sort() # order list (in place) list.reverse() # reverse list (in place) list.index('one') # return index of where 'one' is list.insert(3, 'SURPRISE') # insert into list list.remove('SURPRISE') # remove (first) 'SURPRISE' in list list.pop(i) # remove ith elelment, and return it
Dictionaries
dict = { 'jim' : 48, 'ian' : 17, } print dict['jim'] print dict.keys()
http://cs.marlboro.edu/ courses/ fall2006/python/ notes/ oct_30
last modified Monday October 30 2006 10:26 am EST

attachments [paper clip]

     name last modified size
   playing_card.py Oct 30 2006 10:50 am 1.73kB