820 likes | 823 Views
This announcement provides information about the review of Module 1 concepts and the process of calling functions in Python.
E N D
CSE 115 Introduction to Computer Science I
Announcements • Baldy 21 is back up and running. • 😊 • Labs will take place as scheduled.
Announcements • Graded labs & recitations start this weekMust attend YOUR section; system limits accessEasiest if you swipe in so please bring your UB ID • Can talk to TAs & ask questions in lab activitiesBut will not do your work, since goal is your learning • TAs only help on technical issues on lab examsLab exams are tests & so will be done closed-resource
Announcements • Must have 3 stars on bothModule 1's Expressions&Variablesto take THIS week's lab • Must have 3 stars on bothModule 1's Calling Functions&Defining Functionsto take NEXTweek's lab
Today's Plan Review/Calling functions Relational expressions Boolean expressions Demo: Coding with Relations and Booleans
REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Expressions Simple • Atomic (cannot decompose)(cannot have subexpressions) • Often literal valueint (e.g., 4037, 4037) • float (e.g. 3.1415, 4.0) • boolean (True, False) • str (e.g. 'Y U',"Y not") • Can be a variableprice_per_gallon Compound Must be able to decompose(always has subexpressions) Often contains 2 subexpressions & 1 operator12 * 1'Hi '+"Mom" 56 // 12 4.5 ** 2 Can be a function callarea(w,h)
REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Writing a Compound Expression Subgoals • exp1) Determine the left-hand side of the expression • a) Write the left-hand subexpressionb) Determine the type for the value this subexpression evaluates to • exp2) Write the operator • exp3) Determine the right-hand side of the expression • a) Write the right-hand subexpressionb) Determine the type for the value this subexpression evaluates to • exp4) Verify operator valid for subexpressions' types
REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Writing a Function Call Expression Subgoals • fc1) Write the function name • fc2) Write the argument lista) Start the argument list with an open parenthesisb) For each function input in the order they are listed, write the expression whose value is used for that inputc) Write a comma between each pair of argumentsd) End the argument list with a close parenthesis
REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Writing an Assignment Subgoals • assign1) Write the name of the variable • assign2) Write the assignment operator • assign3) Write the expression whose value will be assigned to the variable
REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Writing a Function Subgoals • fd1) Write comment describing result of evaluating call to this function call (or what function does, if not returning a value) • fd2) Write the function header & delimitersa) Write the def keywordb) Write the function's namei) Choose name reflecting function's SINGLE purposec) Write the parameter list for the function's input(s)i) Choose name(s) expressing value of eachd) Write the function delimiter at the end of the header • fd3) Write the function bodya) Parameter(s) assigned values BEFORE function begins; you should not reassign these valuesb) If call to this function evaluates to a value, make certain that each path through function body ends at a return whose expression evaluates to that value
Today's Plan Review/Calling functions Relational expressions Boolean expressions Demo: Coding with Relations and Booleans
Calling functions How the computer actually evaluates this function call averageOfThree( 3, 7, 4 )
Calling functions Step 0: execute statement containing the function call averageOfThree( 3, 7, 4 ) # Calculates mean of the inputs defaverageOfThree(x, y, z) : average = (x + y + z) / 3 return average
Calling functions Step 0a: evaluate argument expressions to calculate values averageOfThree( 3, 7, 4 ) # Calculates mean of the inputs defaverageOfThree(x, y, z) : average = (x + y + z) / 3 return average
Calling functions Step 1: argument values are assigned to parameters averageOfThree( 3, 7, 4 ) # Calculates mean of the inputs defaverageOfThree(x, y, z) : average = (x + y + z) / 3 return average
Calling functions Step 1: argument values are assigned to parameters averageOfThree( 3, 7, 4 ) # Calculates mean of the inputs defaverageOfThree(x, y, z) : average = (x + y + z) / 3 return average 3 At start of each function call, Python creates new table (environment) to track value of that call's parameters & variables 7 4
Calling functions Step 2: statements in body are executed in sequence averageOfThree( 3, 7, 4 ) # Calculates mean of the inputs defaverageOfThree(x, y, z) : average = (x + y + z) / 3 return average 3 7 4
Calling functions Step 2a: execute first statement in function body averageOfThree( 3, 7, 4 ) # Calculates mean of the inputs defaverageOfThree(x, y, z) :average = (x + y + z) / 3 return average 3 Remember:Writing an Assignment subgoals help you do thisname=expression 7 4
Calling functions Step 2a(1): evaluate assignment statement's expression averageOfThree( 3, 7, 4 ) # Calculates mean of the inputs defaverageOfThree(x, y, z) :average = (x + y + z) / 3 return average (x + y + z) / 3 3 (x + y + z ) / 34 ) / 3 7 4
Calling functions Step 2a(1): evaluate assignment statement's expression averageOfThree( 3, 7, 4 ) # Calculates mean of the inputs defaverageOfThree(x, y, z) :average = (x + y + z) / 3 return average (x + y + z) / 3 3 (x + y + z ) / 3(3 + 7 + 4 ) / 3 7 4
Calling functions Step 2a(1): evaluate assignment statement's expression averageOfThree( 3, 7, 4 ) # Calculates mean of the inputs defaverageOfThree(x, y, z) :average = (x + y + z) / 3 return average (x + y + z) / 3 3 (x + y + z ) / 3(3 + 7 + 4 ) / 3 7 4
Calling functions Step 2a(1): evaluate assignment statement's expression averageOfThree( 3, 7, 4 ) # Calculates mean of the inputs defaverageOfThree(x, y, z) :average = (x + y + z) / 3 return average (x + y + z) / 3 3 (x + y + z ) / 3(3 + 7 + 4 ) / 3 7 4
Calling functions Step 2a(1): evaluate assignment statement's expression averageOfThree( 3, 7, 4 ) # Calculates mean of the inputs defaverageOfThree(x, y, z) :average = (x + y + z) / 3 return average (x + y + z) / 3 3 (x + y + z ) / 3(3 + 7 + 4 ) / 3 7 4
Calling functions Step 2a(1): evaluate assignment statement's expression averageOfThree( 3, 7, 4 ) # Calculates mean of the inputs def averageOfThree(x, y, z) :average = (x + y + z) / 3 return average (x + y + z) / 3 3 (x + y + z ) / 3(3 + 7 + 4 ) / 3 14 / 3 7 4
Calling functions Step 2a(1): evaluate assignment statement's expression averageOfThree( 3, 7, 4 ) # Calculates mean of the inputs defaverageOfThree(x, y, z) :average = (x + y + z) / 3 return average (x + y + z) / 3 3 (x + y + z ) / 3(3 + 7 + 4 ) / 3 14 / 3 4.66666666667 7 4
Calling functions Step 2a(2):variable assigned expression's value averageOfThree( 3, 7, 4 ) # Calculates mean of the inputs defaverageOfThree(x, y, z) :average = (x + y + z) / 3 return average average (x + y + z) / 3 3 (x + y + z ) / 3(3 + 7 + 4 ) / 3 14 / 3 4.66666666667 7 4
Calling functions Step 2a(2):variable assigned expression's value averageOfThree( 3, 7, 4 ) # Calculates mean of the inputs defaverageOfThree(x, y, z) :average = (x + y + z) / 3 return average average (x + y + z) / 3 3 (x + y + z ) / 3(3 + 7 + 4 ) / 3 14 / 3 4.66666666667 7 4 4.6666666667
Calling functions Step 2b: execute second statement in function body averageOfThree( 3, 7, 4 ) # Calculates mean of the inputs defaverageOfThree(x, y, z) :average = (x + y + z) / 3return average 3 7 4 4.6666666667
Calling functions Step 2b(1): evaluate return statement's expression averageOfThree( 3, 7, 4 ) # Calculates mean of the inputs defaverageOfThree(x, y, z) :average = (x + y + z) / 3return average average average4.6666666667 3 7 4 4.6666666667
Calling functions Step 2b(1): evaluate return statement's expression averageOfThree( 3, 7, 4 ) # Calculates mean of the inputs defaverageOfThree(x, y, z) :average = (x + y + z) / 3return average average average4.6666666667 3 7 4 4.6666666667
Calling functions Step 2b(2): return value to statement calling function averageOfThree( 3, 7, 4 ) # Calculates mean of the inputs defaverageOfThree(x, y, z) :average = (x + y + z) / 3return average 4.6666666667 3 7 4 4.6666666667
Calling functions Step 2b(2): return value to statement calling function averageOfThree( 3, 7, 4 ) # Calculates mean of the inputs defaverageOfThree(x, y, z) :average = (x + y + z) / 3return average 4.6666666667
Calling functions Step 3: use return value as value of statement calling function averageOfThree( 3, 7, 4 ) # Calculates mean of the inputs defaverageOfThree(x, y, z) :average = (x + y + z) / 3return average 4.6666666667
Calling functions Step 3: use return value as function call expression's value averageOfThree( 3, 7, 4 ) 4.6666666667 # Calculates mean of the inputs defaverageOfThree(x, y, z) :average = (x + y + z) / 3return average
Calling functions Another example:evaluate a second call to the function averageOfThree( 5, 2, 8 ) # Calculates mean of the inputs defaverageOfThree(x, y, z) : average = (x + y + z) / 3 return average
Calling functions Another example:evaluate a second call to the function averageOfThree( 5, 2, 8) # Calculates mean of the inputs defaverageOfThree(x, y, z) : average = (x + y + z) / 3 return average 5 2 8
Calling functions Another example:evaluate a second call to the function • averageOfThree( 5, 2, 8 ) # Calculates mean of the inputs defaverageOfThree(x, y, z) :average = (x + y + z) / 3 return average 5 (x + y + z ) / 3(5 + 2 + 8 ) / 3 15 / 3 5.0 2 8
Calling functions Another example:evaluate a second call to the function • averageOfThree( 5, 2, 8 ) # Calculates mean of the inputs defaverageOfThree(x, y, z) :average = (x + y + z) / 3 return average 5 (x + y + z ) / 3(5 + 2 + 8 ) / 3 15 / 3 5.0 2 8 5.0
Calling functions Another example:evaluate a second call to the function • averageOfThree( 5, 2, 8 ) # Calculates mean of the inputs defaverageOfThree(x, y, z) : average = (x + y + z) / 3return average 5 2 average 5.0 8 5.0
Calling functions Another example:evaluate a second call to the function • averageOfThree( 5, 2, 8 ) # Calculates mean of the inputs defaverageOfThree(x, y, z) : average = (x + y + z) / 3return average 5.0 5 2 8 5.0
Calling functions Another example:evaluate a second call to the function • averageOfThree( 5, 2, 8) # Calculates mean of the inputs defaverageOfThree(x, y, z) : average = (x + y + z) / 3return average 5.0
Calling functions Another example:evaluate a second call to the function • averageOfThree( 5, 2, 8) 5.0 # Calculates mean of the inputs defaverageOfThree(x, y, z) : average = (x + y + z) / 3 return average
Today's Plan • Review /Calling functions Relational expressions Boolean expressions • Demo: Coding with Relations and Booleans
Relational operators • As defined in Python's standards documents
Operator Key Concept • ==comparesvalues • while • =assignsvariable
Relational expressions • Just another compound expression • 3 < 5 • 'Hi Mom!' != "hi mom!" • (3 ** 2) < (183 / 183) exp1) Determine the left-handside of theexpression
Relational expressions • Just another compound expression • 3 < 5 • 'Hi Mom!' != "hi mom!" • (3 ** 2) < (183 / 183) exp2) Write the operator
Relational expressions • Just another compound expression • 3 < 5 • 'Hi Mom!' != "hi mom!" • (3 ** 2) < (183 / 183) exp3) Determine the right-handside of theexpression
Relational expressions • Just another compound expression • 3 < 5 • 'Hi Mom!' != "hi mom!" • (3 ** 2) < (183 / 183) exp4) Verify operatorvalid for those types
Relational expressions • Just another compound expression exp1) Determine the left-handside of theexpression valjean = 24601jenny = 8675309 valjean== (2460 * 10) + 1 jenny != valjean