# Lists >>> names = ["dave", "mark", "ann", "phil"] >>> names.insert(2, "thomas") >>> print names ['dave', 'mark', 'thomas', 'ann', 'phil'] >>> alist = [[1,2,3], [4,5,6], [7,8,9]] >>> alist[2] [7, 8, 9] >>> alist[2][1] 8 >>> alist[2].insert(1,7.5) >>> print alist [[1, 2, 3], [4, 5, 6], [7, 7.5, 8, 9]] # Iteration >>> nlist = [2,5,7,18,19,24,220] >>> mysum = 0 >>> for x in nlist: ... mysum = mysum + x ... >>> print mysum 295 >>> mysum = 0 >>> for x in nlist: ... mysum = mysum + x ... print "Sum is now", mysum ... Sum is now 2 Sum is now 7 Sum is now 14 Sum is now 32 Sum is now 51 Sum is now 75 Sum is now 295 >>> print nlist [2, 5, 7, 18, 19, 24, 220] >>> counter = 0 >>> for x in nlist: ... counter = counter + 1 ... >>> print counter 7 >>> mysum/counter 42 >>> mysum/float(counter) 42.142857142857146 >>> counter = 0.0 >>> for x in nlist: ... counter = counter + 1 ... >>> mysum/counter 42.142857142857146 >>> counter 7.0 >>> sum(nlist) 295 >>> len(nlist) 7 >>> sum(nlist)/float(len(nlist)) 42.142857142857146 >>> nlist [2, 5, 7, 18, 19, 24, 220] >>> nlist[2] 7 >>> nlist[2:5] [7, 18, 19] >>> nlist[2:] [7, 18, 19, 24, 220] >>> nlist[:7] [2, 5, 7, 18, 19, 24, 220] >>> nlist[0:7] [2, 5, 7, 18, 19, 24, 220] >>> nlist[0:4] [2, 5, 7, 18] >>> firstfour = nlist[:4] >>> firstfour.append(5) >>> print firstfour [2, 5, 7, 18, 5] >>> for c in "Mus Musculus": ... print c.lower() ... m u s m u s c u l u s >>> s = '' >>> for c in "Mus musculus": ... s = s + c.lower() ... >>> s 'mus musculus'