#An illustration of a large number of list #functions. def liststuff(): some_numbers=[3,9,2,11,5] some_veggies=['arugula','asparagus','avocado','artichoke'] a_mess=['broccoli',[7,19],3.78] print some_numbers #we use bracket notation to address the elements of a list #observe that this works much as in strings, with 0 the index #of the first element, negative indices, and slices to form #sublists. print some_numbers[3] print some_veggies[2] print a_mess[1] print a_mess[-3:-1] #We can also find the length of a list with len. print len(some_numbers),len(some_veggies),len(a_mess) #You can concatenate lists with + numveg=some_numbers+some_veggies print numveg print len(numveg) #There is even a funny multiply operation zeros=[0]*12 print zeros numrepeat=some_numbers*5 print numrepeat #Note that, in spite of some similarities, #strings are NOT lists of characters. not_a_string=['h','e','l','l','o'] print not_a_string #There's an empty list empt=[] print len(empt) #You can use in to test membership in a lit. if 'broccoli' in some_veggies: print 'broccoli' else: print 'no broccoli' if 'asparagus' in some_veggies: print 'asparagus' else: print 'no asparagus' #and you can also use the word in when looping over #a list for veg in some_veggies: print veg #ranges are lists my_range=range(0,7) print my_range print my_range[3] #lists are mutable my_range[3]=999 print my_range #you can even sort lists (note the peculiar 'object-oriented' syntax some_veggies.sort() print some_veggies some_numbers.sort() print some_numbers