1 / 82

CSE 115

This announcement provides information about the review of Module 1 concepts and the process of calling functions in Python.

danej
Download Presentation

CSE 115

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 115 Introduction to Computer Science I

  2. Announcements • Baldy 21 is back up and running. • 😊 • Labs will take place as scheduled.

  3. 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

  4. 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

  5. Today's Plan Review/Calling functions Relational expressions Boolean expressions Demo: Coding with Relations and Booleans

  6. 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)

  7. 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

  8. 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

  9. 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

  10. 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

  11. Today's Plan Review/Calling functions Relational expressions Boolean expressions Demo: Coding with Relations and Booleans

  12. Calling functions How the computer actually evaluates this function call averageOfThree( 3, 7, 4 )

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. 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

  31. 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

  32. 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

  33. 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

  34. 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

  35. 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

  36. 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

  37. 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

  38. 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

  39. 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

  40. 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

  41. 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

  42. 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

  43. Today's Plan • Review /Calling functions Relational expressions Boolean expressions • Demo: Coding with Relations and Booleans

  44. Relational operators • As defined in Python's standards documents

  45. Operator Key Concept • ==comparesvalues • while • =assignsvariable

  46. Relational expressions • Just another compound expression • 3 < 5 • 'Hi Mom!' != "hi mom!" • (3 ** 2) < (183 / 183) exp1) Determine the left-handside of theexpression

  47. Relational expressions • Just another compound expression • 3 < 5 • 'Hi Mom!' != "hi mom!" • (3 ** 2) < (183 / 183) exp2) Write the operator

  48. Relational expressions • Just another compound expression • 3 < 5 • 'Hi Mom!' != "hi mom!" • (3 ** 2) < (183 / 183) exp3) Determine the right-handside of theexpression

  49. Relational expressions • Just another compound expression • 3 < 5 • 'Hi Mom!' != "hi mom!" • (3 ** 2) < (183 / 183) exp4) Verify operatorvalid for those types

  50. Relational expressions • Just another compound expression exp1) Determine the left-handside of theexpression valjean = 24601jenny = 8675309 valjean== (2460 * 10) + 1 jenny != valjean

More Related