Anda di halaman 1dari 2

Computer Science 8: Midterm 2 Cheat Sheet

Lists

o Mylist = [3, cat, 6.5, 2] o Indexing --- [] o Concatenation --- + o Membership---- in & not in o Length ---- len() o Slicing ---- [:] o Strings are immutable while lists are mutable o Mylist.append(item) o Mylist.insert(i, item) where i is an index o Mylist.pop() o Mylist.pop(i) o Mylist.sort() o Mylist.reverse() o Mylist.index(item) o Mylist.count(item) o Mylist.remove(item) o Mylist.split() --- turns a string into a list based on the spaces in the string o Mylist.split(item) --- splits by the item o min(mylist) o max(mylist) range = max(mylist)-min(mylist) Dictionaries o dict.keys() returns keys in a list with an object o dict.values() returns values o dict.items() returns both keys and values o dict.get(item) o key in dict o key not in dict --- works only for keys, not values o dict[key] gives its index, which works the same as dict.get(key) o del dict[key] File Handling o open(filename,r) o open(filename,w) --- the r and w are used to denote reading or writing a file o file.close() says that the file has completed its use example: file = open(file.txt,r) for line in file: values = line.split() print(values[0], values[1]) o writing a string to the end of a file: o file.write(string) o file.read() returns a string of n characters o file.readlines() returns a list of n strings, each representing a single line of the file. If n Is not provided, then all lines of the file are returned. Inputting lines of a file into a list o file = open(umar.txt,r) o list = [] o for line in file: alist = line.split() list.append(float(alist[0])) o return list o with this list, we can calculate the min, max, mean, std dev, etc. While loop o i=0 o while(i<5): print(i) i=i+1 Digital Image Processing o Red = (255,0,0) o Green = (0,255,0) o Blue = (0,0,255) o Magenta = (255,0,255) o Yellow = (255,255,0) o Cyan = (0,255,255) o White = (255,255,255) o Black = (0,0,0) o Pixel(r,g,b) where r,g,b represent pixel values from 0 to 255 inclusive o getRed() o getGreen() o getBlue() o setRed() o setGreen() o setBlue() o to make an image window--- myWin = ImageWin(title,width, height) o myWin.exitOnClick() o im = FileImage(pic.gif) o im = EmptyImage(width,height) makes a black window o getWidth() o getHeight() o getPixel(col,row) o setPixel(col,row,Pixel(r,g,b)) o setPosition(col,row) position the top-left corner of the image a (col,row) o im.draw(myWin) (little umar into big umar aka empty image into big umar) for num in range(3): o for ch in cat: print(ch,num) negative images are created by changing each pixel by subtracting the old pixel from 255.

grayscale images are created by averaging each of the values of the pixels so that each r,g,b value is the same and creates a particular shade of gray. The closer it is to 255, the lighter it is (white) and the closer to 0 it is, the closer to black it is just remember stuff about name spaces.

Anda mungkin juga menyukai