############################################################################### # Fun commands to try in the Python command-line interpreter. # Blame/ask questions: Andrew Uzilov (auzilov@ucsc.edu), Fall 2009. ############################################################################### # Using the interpreter as a calculator. 4 / 3 8 / 5 4.0 / 3 8 / 5.0 # Create an integer object, bind reference 'anInt' to it. anInt = 111 anInt = int (111) # Create a float object, bind reference 'aFloat' to it. aFloat = 222.0 aFloat = float (222) # Convering from 'int' to 'float' type and back. aFloat = float (anInt) anInt = int (aFloat) # Type these in to see if it is a built-in function or Python type; # if it is, DO NOT use as a reference name! int float type max frobnicate # Intro to making lists (which are mutable sequences). aList = [0, 1, 2] aComplexList = [1, 2.5, anInt, [-1, -2], aFloat] # Some list methods and functions. aList = [57, 2, 0, -13] aList.append (anInt) min (aList) max (aList) aList.sort() # How NOT to make a new, sorted list while keeping unsorted original. listOrig = [6, 3, 7, 2, 1] listSorted = listOrig listSorted.sort() # Proper way to make a new, sorted list while keeping unsorted original. listOrig = [6, 3, 7, 2, 1] listSorted = sorted (listOrig) # Accessing list contents. aList[0] aList[4] aList[-1] aList[-1] = 700 # rebind a reference in list to int object "700" aList[500] # Accessing "nested" lists (lists that are list items). aComplexList[3] aComplexList[3][1] # Slices. aList[1:4] aList[1:500] subList = aList[1:4] subList = aList[1:] subList = aList[:4] # Slices with explicit step size/sign. aList[0:5:2] aList[4:0:-1] aList[4::-1] aList[::-1] # reversing a list (makes a copy!) # Replacing a part of a list with another list. aList[0:2] = [-2000, -1000] # List comprehensions. incrList = [x+1 for x in aList] floatList = [float (x) for x in aList] replList = [0 if x < 0 else x for x in aList] # Intro to strings (which are immutable sequences). aString = " bioinformatics is awesome!! " subString = aString[2:16] aString[2] aString[2] = 'B' # this will not work! strings are immutable # Removing leading/trailing whitespace. aString.strip() aString.rstrip() aString.rstrip(' !') # Convert string to list and back again; # capitalize "bioinformatics". stringAsList = aString.split() stringAsList[0] = stringAsList[0].capitalize() fixedString = ' '.join (stringAsList) '~~~'.join (stringAsList) # Make string even MORE awesome. fixedString.replace ('is', '***IS***') # Caution about using 'join' on lists containing non-strings; # the iterable passed to 'join' MUST contain only strings! listOfInts = [1, 2, 3] '_'.join (listOfInts) # this won't work '_'.join (map (str, listOfInts)) # one way to fix it '_'.join ([str (x) for x in listOfInts]) # another way to fix it # Intro to tuples (which are immutable sequences) aTuple = (1, 2.5, None, (-3, -7), []) aTuple[2] aTuple[3] aTuple[3][0] aTuple[2] = 45.678 # this won't work; can't rebind refs in tuples aTuple[4].append ('foo') # this WILL work; can you tell why? # Intro to dictionaries (aka hashes, maps, associative arrays). aMap = dict() aMap = {} # Accesing dict contents, adding contents. capitals = {'CA': 'Sacramento', 'TX': 'Austin', 'NY': 'Albany'} capitals['TX'] myFavState = 'CA' capitals[myFavState] capitals['PA'] = 'Harrisburg' capitals.keys() capitals.values() # Note that dict keys do not have to be strings! # Any immutable object will do. wackyDict = {0: 'zero', 5: 'five'} wackyDict[0] wackyDict[1] type (wackyDict) type (aList) # Even tuples can be used as keys because they are immutable. tupleKey = ('a', 'b') tupleDict = {} tupleDict[tupleKey] = 'value' # Intro to the print function. print 'Hello, world!' print 'Hello', 'world!' print 'Here are some capitals:', capitals.values() print 'Here are some capitals:\n', '\n'.join (capitals.values()) # Using the % string formatting operator. print 'I know %d capitals.' % len (capitals) print 'I know %d capitals and they are: %s' % (len (capitals), capitals) # You can use the % operator anywhere, not just with 'print'. output = 'I know %d capitals and they are: %s' % (len (capitals), capitals) print output # Printing floats. print 'Look, a float: %f' % aFloat print "Don't do this: %f" % 4.0 / 3 # oops! operator precedence problem print "Do this instead: %f" % (4.0 / 3) # this will fix it print "For multiple floats: %f %f" % (4.0 / 3, 8.0 / 5) print "Even more carefully: %f %f" % ((4.0 / 3), (8.0 / 5))