# variables and lists # pmb 10/2/02 # variable are sort-of like the cells in a spreadsheet # they can hold numbers or character strings x=5 iamavariable='hi mom' print x,iamavariable # all variables have names; variables with different caps # are different variables IamAVariable='hi dad' print iamavariable, IamAVariable # you can do math on variables if they both have numbers in them zxy = 32 print "zxy is now ", zxy, zxy = zxy + 10 print " and now its ",zxy # lists- you can make lists of variables if it's a handy way to access them years = [ 1980, 1982, 1984, 1986, 1977 ] print " list of years is ", years # lists are accessed thusly- this gets the 4th year in the list print "4th year is ",years[4] # for loops let you cycle through entries in a list easily for zxy in years: print zxy # you can cycle through consecutive numbers with a range list for zxy in range( 2,10 ): print "for list with range ",zxy # there are also lists of characters presidents = [ 'carter','ford','reagan','bush','clinton'] print "there are ",len(presidents)," presidents, first is ",presidents[0] for x in presidents: print 'hail to chief ', x # sorting lists of chars is easy presidents.sort() print "sorted presidents ", presidents # can even have lists of chars and numbers aMixedList = [ 1996, 'clinton',2002, 'churchill' ] for x in aMixedList: print x #variables can also be objects, like in vpython demos #there are also hashes and tuples