REVIEW FOR MIDTERM #1 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) String "hello" List [3, "frog", 2+5, [3, 8], "goat"] Tuple (3, "frog", 2+5, [3, 8], "goat") "Mutable" vs. "Immutable" types 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) 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 a List Empty list: [] 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, and Lists 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 range(N): 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 Predefined Python Functions len(string) len(list) range(N) range(N1,N2) range(N1,N2,N3) math.sqrt(N) User Defined Functions def Name (parameter,parameter,....,parameter): "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 List Comprehensions (simple form) [X for X in range(10)] [math.sqrt(X) for X in range(1,50,3)] JES Environment Functions requestInteger(message) requestIntegerInRange(message,low,high) requestNumber(message) showInformation(message) pickAFile() Canvasses Canvas = makeEmptyPicture(width,height,color) Canvas = makePicture(filename) 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): for x in range(getWidth(Canvas)): for y in range(getHeight(Canvas)): pixel = getPixel(Canvas,x,y) addLine(Canvas,x1,y1,x2,y2,color) addOvalFilled(Canvas,x,y,width,height,color) addRectFilled(Canvas,x,y,width,height,color) black, white, red, green, blue, yellow, cyan, magenta 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 ... How do you design centered circles and squares with existing functions? How do you bounce a box/ball around screen?