REVIEW FOR MIDTERM #2 Data types Integer (-5, 0, 27, 1000000000) Floating point (-5.1, 0.0, 27.4E-34, 1.0E+308) Long Integer (1L, or any integer >= 2**31) Complex (3-4j) Bool (False, True) String, immutable: "hello" or 'hello' or """hello""" (may span rows) List, mutable: [3, "frog", 2+5, [3, 8], "goat"] Tuple, immutable: (3, "frog", 2+5, [3, 8], "goat") Variables Case sensitive Start with letter, may contain digits Data type depends on value assigned Data type may change as program runs Format: var = expression Examples: X = 0 Frog = X * 15.0 X = X + 1 L = [1, 2, 3] T = (4, 5, 6, 7) B = True Operations on Data Add, Subtract, Multiply, Divide, Exponentiation Order of Operations (**, * and /, + and -) Integer Divide vs. Floating-Point Divide Concatenating Strings by "adding" them Initializing structures (constructor symbols) Empty list: [] Empty tuple: () Empty string: "" Accessing an item in a list (indexing starts at zero) Changing an item in a list Initializing a String Double-Quote Single-Quote Triple-Double-Quote (span line breaks) Accessing a character in a string (indexing starts at zero) Accessing an item in a tuple (indexing starts at zero) The "len" function for Strings, Tuples, Lists, Dictionaries Setting a value of a list: L = [2, 9, 7] L is now [2, 9, 7] L[2] = 6 L is now [2, 9, 6] L = [2, 9, 7] L is now [2, 9, 7] L = L + [4] L is now [2, 9, 7, 4] L = [4] + L L is now [4, 2, 9, 7, 4] Python Program Structure Interactive Mode vs. Program Mode Statements Comments (start with #) Indentation Flow of control Assignment (variable = expression) The "print" statement The "pass" statement Comparisons (used in if/elif/while) Equality == Inequality <> or != Less-Than < Less-Equal <= Greater-Than > Greater-Equal >= Set Membership in Selections if (?): elif (?): else: Loops while (?): for variable in list: for I in [3,7,1,4]: for variable in string: for I in "Hello": for variable in range(N): for I in range(10): for variable in getPixels(Canvas): Functions Functions and Function Calls Calling a function multiple times with different actual parameters Passing the result of a function to another function or assigning it to a variable Passing a function as a parameter to another function Making a function local to another function Using global variables to pass information between functions Predefined Python Functions len(string) len(list) len(dictionary) range(N) range(N1,N2) range(N1,N2,N3) math.sqrt(N) min(a,b,c,...,z) max(a,b,c,...,z) User Defined Functions def Name (parameter,parameter,...,parameter): def Name (parameter,parameter,...,parameter=default,parameter=default): "return x" vs. "return" Formal Parameters vs. Actual Parameters Importing Packages import random random.random() random.randrange(n) import time time.sleep(n) Advanced Coding Idea (statements on the same line as a keyword ending in colon) if (?): statement elif (?): statement else: statement while (?): statement for variable in list: statement List Comprehensions (simple form) [X for X in range(10)] returns [0,1,2,3,4,5,6,7,8,9] [0 for X in range(10)] returns [0,0,0,0,0,0,0,0,0,0] [X*X for X in range(5)] returns [0,1,4,9,16] [math.sqrt(X) for X in range(1,50,3)] JES Environment I/O Functions requestInteger(message) requestIntegerInRange(message,low,high) requestNumber(message) showInformation(message) pickAFile() pickAFolder() Canvasses Canvas = makeEmptyPicture(width,height,color) Canvas = makePicture(filename) Canvas = duplicatePicture(OtherCanvas) show(Canvas) repaint(Canvas) getColor(pixel) setColor(pixel,color) getRed(pixel) setRed(pixel,value) getGreen(pixel) setGreen(pixel,value) getBlue(pixel) setBlue(pixel,value) setAllPixelsToAColor(Canvas,color) for pixel in getPixels(Canvas): # Simple pixel transforms for y in range(getHeight(Canvas)): for x in range(getWidth(Canvas)): PX = getPixel(Canvas,x,y) R = getRed(PX) G = getGreen(PX) B = getBlue(PX) # Do something to R, G, and B setRed(PX,R) setGreen(PX,G) setBlue(PX,B) repaint(Canvas) addLine(Canvas,X1,Y1,X2,Y2,Color=black) addOval(Canvas,X,Y,Width,Height,Color=black) addRect(Canvas,X,Y,Width,Height,Color=black) addOvalFilled(Canvas,X,Y,Width,Height,Color=black) addRectFilled(Canvas,X,Y,Width,Height,Color=black) black, white, red, green, blue, yellow, cyan, magenta, gray NewColor = makeColor(red,green,blue) Questions How do you print out numbers and square roots between 0 and 100? How do you convert an existing image to: Black and White Gray Scale Red-Green-Blue (8 colors) Brighter Darker Brighter Green Sepia ... How do you create a command processor? How do you design centered circles and squares with existing functions? How do you bounce a box/ball around screen? How do you hierarchically decompose a visual object into function calls? How do you nest functions hierarchially? How do you dither an image? How do you mirror/flip an image? How do you filter (focus, emboss, etc.) an image?