CS 100 Python commands, computing concepts, and algorithmic approaches for final Fall 2015 These pages will NOT BE INCLUDED IN THE MIDTERM. print - Displays a value in the command area - Examples: - print Hello - print 10 - print x, where x is a variable. Its value will be printed. assignment statement - Used to change the value of a variable - Examples: def file = pickafile() color = red - Used to define a function - The names inside the parentheses on the def line are parameters. They get their value when the function is called. def HelloWorld (name) : print Hello, + name calling a function - Executes the function, assigning argument values to parameter names - The names inside the parentheses when a function is called are arguments. Their value is assigned to the parameter in the same position on the def line of the function. return HelloWorld ( Mary Lyon ) - A return statement allows a function to compute a value and report it back to the caller - The caller saves the value in a variable using an assignment statement
def loadpicture () : file = pickafile() pic = makepicture(file) return pic - Then the caller can say: mypic = loadpicture() for loop - Repeats the statements inside the loop for each value looped over for p in getpixels(picture) : setcolor (p, red) - Nested for loops are handy for selecting a rectangular part of an image or for doing image manipulations based on the location of a pixel. if statement - Executes some statements only when a condition is true # Shadows if (grayness < 63) : redness = redness * 1.1 blueness = blueness * 0.9 # Midtones elif (grayness < 192) : redness = redness * 1.15 blueness = blueness * 0.85 # Highlights else : redness = redness * 1.08 blueness = blueness * 0.93 - An if-statement can have any number of elif parts, including 0. The else part is optional and must come last. The statements associated with the first condition that is true are executed and the rest of the if-statement is skipped. The else part is executed if none of the conditions are true. Computing concepts you should be familiar with These are just the highlights. Questions on the exam may go beyond this level of detail. - An algorithm is a sequence of instructions describing how to complete a task. A program is an algorithm written in a programming language.
- Abstraction is describing a solution in a way that you use already understood concepts in your description, rather than providing complete detail. A function is a way of defining an abstraction, by giving a name to a list of instructions. - Syntax refers to the grammar rules of a programming language. - Semantics defines what a programming language construct means. - Variables allow you to use names for values, where the variable can have different values at different times. - The scope of a variable is the part of the program where the variable is known. A parameter or a variable set within a function has the function as its scope. - The computer encodes all data as binary, just 0s and 1s. The same sequence of 0s and 1s can mean different things depending on its context. For example, it could be a number, a letter, or a color. - ASCII is the most common encoding of characters into binary. Unicode is another encoding that can support many different alphabets and common symbols. - JPEG and GIF are two different formats for images that compress images so they take less space on disk than their complete bitmap representation. - Vector graphics is a way of representing an image by providing instructions to draw the image rather than describing the image directly. - A bit can take on one of 2 values, 0 or 1. A byte is 8 bits and can take on any value between 0 and 255. - Images are made up of pixels. A pixel has a location identified by its row and column numbers. Each pixel has a color. - RGB represents a color with a red value, a green value and a blue value. Each of these values can range from 0 to 255. Black is 0, 0, 0. White is 255, 255, 255. Any color where the R, G, and B values are the same is a shade of gray. - A test function allows us to easily re-run functions we are writing without needing to do a lot of typing in the console. - We can compute the negative of a color by subtracting each RGB color component from 255 and using those values to create a new color. - We can convert an image to grayscale by averaging the red, green and blue values of each pixel. Luminance is another word we use for this average. - Chromakey is a technique used to record something or someone against a solid background, often bright green, so that the object or person can be separated from the background and copied cleanly into another image. - Sound is caused by changes in air pressure. A sound wave is a visual representation of these air pressure changes over time. - The amplitude of a sound wave is its height above or below a middle line. Amplitude affects the volume. - The frequency of a sound wave is how often the sound wave repeats in one second. Frequency determines pitch.
- Sound is recorded digitally by sampling it at fixed intervals and recording the values found. More frequent sampling generally results in higher quality sound. - Normalizing a sound changes the amplitude of a sound so that its highest amplitude is at the maximum value of 32,767. - Notes are chorded by normalizing each and then averaging their values. - A sound wave that is a sine wave produces a "pure" note. The same pitch can be created with different shape sounds, like square waves, or less regular waves such as those that come from a musical instrument as long as the frequency is the same as that of the sound wave. Algorithmic approaches We have been working with some standard algorithmic approaches in the functions that we have been writing in class. Image algorithms When we want to treat all pixels in an image the same way for pixel in getpixels ( <picture> ) : <instructions to change the pixel> When we want to change a rectangular portion of a picture for row in range ( <top row>, <bottom row> ) : for col in range ( <left col>, <right col> ) : pixel = getpixel ( <picture>, col, row ) <instructions to change the pixel> When we want to create a new picture based on the contents of an existing picture <canvas> = makeemptypicture ( <width>, <height> ) for row in range ( <top row>, <bottom row> ) : for col in range ( <left col>, <right col> ) : <original pixel> = getpixel ( <picture>, col, row ) <calculate row & column to copy to on the canvas> <canvas pixel> = getpixel (<canvas>, <canvas col>, <canvas row> ) setcolor ( <canvas pixel>, getcolor ( <original pixel> ) ) return <canvas> When we want to treat pixels differently based on their original color, we use an ifstatement for p in getpixels (pic) : if <some condition is true> : setcolor (p, <some color> ) Testing We separate the loading of files and the displaying of images into a test function to keep the image manipulation function more reusable.
When the image manipulation function modifies the image in place, the test function generally follows this pattern def test<func> () : file = <file name> picture = makepicture (file) explore (picture) <image manipulation function> (picture) explore (picture) When the image manipulation function creates a new canvas, the test function generally follows this pattern def test<func> () : file = <file name> picture = makepicture (file) explore (picture) copy = <image manipulation function> (picture) explore (copy) Sound algorithms When we want to treat all samples in a sound the same way for sample in getsamples ( <sound> ) : <instructions to change the sample> When we want to change a sample based on its position for index in range ( <first position>, <last position> ) : sample = getsamplevalueat ( <sound>, index ) <instructions to change the sample> When we want to create a new sound based on the contents of an existing sound <new sound> = makeemptysound ( <number of samples> ) for index in range ( <first position>, <last position> ) : <original sample> = getsamplevalueat ( <sound>, index ) <calculate position to copy to in the new sound> <calculate new sample value> setsamplevalueat ( <new sound>, <new position>, <new value> ) return <new sound> Before combining sounds, we generally normalize the sounds so that they have the same relative volume. def normalize (sound): # Find the highest value in the sound highestvalue = 0 for sample in getsamples (sound): if getsamplevalue (sample) > highestvalue : highestvalue = getsamplevalue ( sample ) # Calculate the multiplier to maximize the highest value
multiplier = 32767.0 / highestvalue # Apply the multiplier to all the samples in the sound changevolume (sound, multiplier) To chord two notes, we first normalize the notes and then average their values def chord ( note1, note2 ) : # Normalize the notes normalize (note1) normalize (note2) # Create a new sound if getlength ( note1 ) < getlength (note2) : newlength = getlength (note1) else : newlength = getlength (note2) newsound = makeemptysound (newlength) # Walk over the 2 notes for index in range (newlength) : value1 = getsamplevalueat (note1, index) value2 = getsamplevalueat (note2, index) # Average their values and store that in the chorded sound setsamplevalueat (newsound, index, (value1 + value2) / 2) return newsound