''' CS 1112 Spring 2020 In Class Review Session 04/22/2020 ''' ''' Clockwise / Counterclockwise Rotation ''' # Please refer to the ImagingGuide picture I sent out (Nadia). ''' For understanding: ''' # Remember that when it comes to clockwise/counterclockwise rotation, # x is basically the distance we go across and y is the distance we go down from the origin (0,0) # x values go from 0 to the width - 1 # y values go from 0 to the height - 1 # When you get problems that ask you to manipulate the spot, think about how the nx relates to the ox # by drawing it out and seeing how the spots translate over on the new image. I'd be happy to go over # this Thursday as well. ''' Color Function: ''' # YOU WILL ONLY WORRY ABOUT COLOR FUNCTION SO THE SPOT STUFF YOU DON'T REALLY HAVE TO WORRY ABOUT. # That will be filled in for you for dimensions and spots. You will have to worry about changing the color # in the right way! def color(opixel): r,g,b = opixel nr, ng, nb = ... return (nr,ng,nb) # That's it. You just need to fill in line 21. :) ''' Dictionaries / Keys ''' # dictionaries are a collection of key/value mappings {k:v,k:v,k:v} d1 = { 1:10, 10:100, 100:1000, 1000:0 } print( d1.keys() ) # d1.keys() gives you all the keys in a dictionary. print( d1.values() ) # d1.values() gives you all the values in a dictionary. # Loop through a dictionary for key in d1.keys(): # for key in d1 # Same thing! ... # How do I get the value from a key in a dictionary? # dictionary_name[key] dictionary_name.get( key ) # Only if you know the # Returns value if it's in the dictionary # key is in the dictionary # or None if it's not in the dictionary # len(d1) -> number of mappings in d1 (dictionary) - A mapping is one k:v pair. # KEYS are unique. VALUES can be repeated. # Can do: # {1:'odd', 2:'even', 3:'odd', 4:'even'} # Cannot have the same key have a bunch of values: # {1: 'odd', 1:'even'} -> PREVIOUS VALUE OF KEY IS REPLACED IF YOU REASSIGN IT # 1:'even' will be the mapping! # You can map anything to anything. ''' Datasets: Rows and Columns ''' # A table/dataset is a list of lists. dataset1 = [[2, 4, 6], [8], [10, 12]] # 0 1 2 0 0 1 # 0 1 2 # DATASET INDEXES: # 1st row = dataset[0] = [2,4,6] # 2nd value in 1st row = dataset[0][1] = 4 # for row in dataset: Access each smaller list in the dataset! # for cell/column in row: Access each value in the smaller lists (rows) # Each row is the smaller list # Each value is the numbers in the rows # Nested for loop allows you to loop through rows/columns total = 0 for row in dataset1: for value in row: total = total + value print( total ) total = 0 for row in dataset1: row_total = sum( row ) # Call sum( list ) on the smaller row (smaller list) total = total + row_total print( total ) ''' Floats ''' # Floats are just decimals. They are a type in Python. # int is an integer. float is a decimal. # We can cast numbers to a float using the float( x ) function num1 = float(3) # 3.0 (Integer to float) num2 = float('3.25') # 3.25 (String to float) print(num1) print(num2) # Use the int( x ) function to throw away the decimal part of a number. # int(3.98237498235) -> 3 (not rounding! chops off decimal part!) # Read the instructions on the exam and see if you're supposed to use integers / floats. ''' Local Variables ''' # Local variables are variables defined inside a function (not accessible everywhere / outside scope of function). def quack(): number_ducks_earned = 0 # number_ducks_earned is a local variable! return number_ducks_earned # print(number_ducks_earned) # Notice how Python is like ??? What?? What's that?? # It's because that local variable only exists within the scope of the function! # Not accessible everywhere throughout the program! ''' Optional Parameters ''' # Optional parameters allow you to have a way to specify default values for parameters if the user # doesn't supply arguments for those parameters. def function1( parameter1='hello', parameter2='world'): return parameter1 + " " + parameter2 # glues parameters together (concatenation) # Arguments are passed in in the order of the parameters! result1 = function1('CS','1112') # 'CS 1112' - parameter1 is 'CS' parameter2 is '1112' result2 = function1( 'hi' ) # 'hi world' - 1 argument other one goes to default! result3 = function1() # 'hello world' - both default parameters! result4 = function1( parameter2='hi' ) # 'hello hi' - specify specific argument for parameter! print(result1) print(result2) print(result3) print(result4) ''' Function with no return statement ''' # EXAMPLE OF A FUNCTION WITH NO RETURN STATEMENT # THAT MODIFIES THE ORIGINAL LIST (lists are mutable) # You would get the def insert_value(x,i,v) - you would only write the function body. # !!!!! You will get function DEFINITIONS AND TESTERS. Just fill in the function body! def insert_value( x , i, v ): # Takes in a list modifies the list # returns None x.insert(i, v) # Modify the original list x # This function returns None by default (no explicit return statement)! list1 = [1,2,3] insert_value( list1, 2, 4) # We can call the function directly! print( list1 ) # The original list (list1) was modified!