530 likes | 551 Views
CSE 115. Introduction to Computer Science I. Announcements. Un graded labs & recitations this week Attend scheduled section if it meets Tues. – Fri. If yours was cancelled, can attend replacement Since this week's lab is ungraded, no requirements in UBInfinite for lab. Announcements.
E N D
CSE 115 Introduction to Computer Science I
Announcements • Ungraded labs & recitations this weekAttend scheduled section if it meets Tues. – Fri.If yours was cancelled, can attend replacement • Since this week's lab is ungraded, no requirements in UBInfinite for lab
Announcements • Graded labs & recitations starts next weekMust attend YOUR section; system limits accessLab work submitted outside Baldy 21 will be given 0: must submit work from lab computers. • Must have 3 stars on bothModule 1's Expressions&VariablesBEFORE your lab next weekEnforced by the system; TAs cannot do anything about itCannot sit in lab to complete; TAs must ask you to leave
Announcements Graded TopHatquestions starts today • Calling Function & Defining FunctionMay need both lectures for UBInfinite exercisesEven if you cannot finish, good idea to try starting work after class • repl.it helps solve UBInfinite questionsOnly 1st answer earns credit, so make it countOnce code works, copy-and-paste answer back into UBInfinite
Help us help you! Search Piazza before posting: question might be answered When posting to Piazza:
Help us help you! Search Piazza before posting: question might be answered When posting to Piazza: Tell us what YOU think problem is
Help us help you! Search Piazza before posting: question might be answered When posting to Piazza: Tell us what YOU think problem is What you've tried to solve problem on your own
Help us help you! Search Piazza before posting: question might be answered When posting to Piazza: Tell us what YOU think problem is What you've tried to solve problem on your own Explain which topic is confusing or hard-to-understand
Help us help you! Search Piazza before posting: question might be answered When posting to Piazza: Tell us what YOU think problem is What you've tried to solve problem on your own Explain which topic is confusing or hard-to-understand Developing good learning strategies is important part of class; upper-level classes (and bosses) need more than saying 'help'
Today's Plan Review Calling functions repl.itDemo: function calls in Python
REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Variable Key Concept Namethat has beenassigned avalue
REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Variables Assignment name=expression assign1) namewritten on left-handsideof assignment operator assign3) expressionwritten onright-handsideof assignment operator assign2) write the assignment operator
REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW REVIEW Variables in Expressions Variables also expressions After assigned value, variable usable as simple expression:x = 3interesting_name = 6interesting_name = xprint(x * 5)school_name = 'UB'mascot = (school_name + " ") + "Bulls"
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 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
Today's Plan Review Calling functions Demo: function calls in Python
Functions Function is named block of code Executecode suiteby calling the function Code a function callby typing name &providing arguments Good for common tasks, since can call function anytime
Functions Original Ideas From Math
Expressions Function Calls Calling function like hiring it to have it step in & do its thing Function does all of work, you get any result it produces Function calls are another type of expression:Function name is operationArgument(s) are subexpression(s)
Functions Python includes many built-in functions abs(num)min(num_1,num_2)max(num_1,num_2)pow(a_name,other_name)round(consistency_sucks)round(x,y)print(value_to_print)
Imports Also has functions defined in modules Must importmodule to use these functions Example using Python's math module import math hope_close_enough = math.sin(3.141592) module_also_has_constants = math.pi/2 shorter = module_also_has_constants print( math.cos( shorter ) * 4 )
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 of function's input(s) in listed order, 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
Function Calls Examples pow( 3 , 2 ) min( 16/3, 4 ) • print( "Hi there!" ) x = -6y = abs(x) fc1) Write the functionname
Function Calls • Examples • pow( 3 , 2 ) • min( 16/3, 4 ) • print( "Hi there!" ) • x = -6y = abs(x) fc2a)Start argumentlistwithopen parenthesis
Function Calls • Examples • pow(3 , 2 ) • min( 16/3, 4 ) • print("Hi there!" ) • x = -6y = abs(x) fc2b)Write expressionswhose values used for that input
Function Calls • Examples • pow(3 , 2 ) • min( 16/3, 4 ) • print("Hi there!" ) • x = -6y = abs(x) fc2c)Write a commabetween each pairof arguments
Function Calls • Examples • pow(3 , 2 ) • min( 16/3, 4 ) • print("Hi there!" ) • x = -6y = abs(x) fc2d)End the argument list with a close parenthesis
Function Calls Examples • pow(3,2)calculates 32; evaluates to value of 9 • min(16/3,4)calculates minimum of 5⅓ & 4; evaluates to value of 4 • print( "Hi there!" )results in text saying Hi therebeing added to end of console
Function Calls Examples • x = -6y = abs(x)Starts by assigning x a value of -6
Function Calls Examples • x = -6y = abs(x)Starts by assigning x a value of -6Evaluates simple expression x; result of evaluation is value of -6
Function Calls Examples • x = -6y = abs(x)Starts by assigning x a value of -6Evaluates simple expression x; result of evaluation is value of -6Uses -6as input to absolute value; result of evaluation is 6
Function Calls Examples • x = -6y = abs(x)Starts by assigning x a value of -6Evaluates simple expression x; result of evaluation is value of -6Uses-6as input to absolute value; result of evaluation is 6Assigns y result of evaluating function call expression
Function Calls Input toabs is NOTx x's valueinput to abs Examples • x = -6y = abs(x)Starts by assigning x a value of -6Evaluates simple expression x; result of evaluation is value of -6Uses-6as input to absolute value; result of evaluation is 6Assigns y result of evaluating function call expression
Today's Plan Review Calling functions • Dr. Adrienne Decker repl.itDemo: function calls in Python
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 of function's input(s) in listed order, 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
Pick the True Statement • Choose 1 statement which is true in Python: • Only variables have types • Variable can be assigned only once • str literals can use either double (") or single (') quotes • + performs addition if both subexpressions are numbers& catenation if at least 1 is a str
Pick the True Statement Convince Your Neighbor Your Answer Is Correct • Choose 1 statement which is true in Python: • Only variables have types • Variable can be assigned only once • str literals can use either double (") or single (') quotes • + performs addition if both subexpressions are numbers& catenation if at least 1 is a str
Pick the True Statement • Choose 1 statement which is true in Python: • Only variables have types • Variable can be assigned only once • str literals can use either double (") or single (') quotes • + performs addition if both subexpressions are numbers& catenation if at least 1 is a str
Pick the True Statement • Choose 1 statement which is true in Python: • Only variables have types • Variable can be assigned only once • str literals can use either double (") or single (') quotes • + performs addition if both subexpressions are numbers& catenation if at least 1 is a str Only values have a type; • variable provides "name" for location holding value temporarily
Pick the True Statement • Choose 1 statement which is true in Python: • Only variables have types • Variable can be assigned only once • str literals can use either double (") or single (') quotes • + performs addition if both subexpressions are numbers& catenation if at least 1 is a str Variables CAN be reassigned; • just replaces what it's value is
Pick the True Statement • Choose 1 statement which is true in Python: • Only variables have types • Variable can be assigned only once • str literals can use either double (") or single (') quotes • + performs addition if both subexpressions are numbers& catenation if at least 1 is a str + does not combine a number & a string
Pick the True Statement • Choose 1 statement which is true in Python: • Only variables have types • Variable can be assigned only once • str literals can use either double (") or single (') quotes • + performs addition if both subexpressions are numbers& catenation if at least 1 is a str ButCANNOT use both in same literal
Today's Plan Review Calling functions Demo: function calls in Python
Functions Python includes many built-in functions abs(num)min(num_1,num_2)max(num_1,num_2)pow(a_name,other_name)round(consistency_sucks)round(x,y)print(value_to_print)
Imports Also has functions defined in modules Must importmodule to use these functions Example using Python's math module import math hope_close_enough = math.sin(3.141592) module_also_has_constants = math.pi/2 shorter = module_also_has_constants print( math.cos( shorter ) * 4 )
Function Calls Examples • pow(3,2)calculates 32; evaluates to value of 9 • min(16/3,4)calculates minimum of 5⅓ & 4; evaluates to value of 4 • print( "Hi there!" )results in text saying Hi therebeing added to end of console
Function Calls Input toabs is NOTx x's valueinput to abs Examples • x = -6y = abs(x)Starts by assigning x a value of -6Evaluates simple expression x; result of evaluation is value of -6Uses-6as input to absolute value; result of evaluation is 6Assigns y result of evaluating function call expression
Function Calls Examples pow( 3 , 2 ) min( 16/3, 4 ) • print( "Hi there!" ) x = -6y = abs(x) fc1) Write the functionname
Function Calls • Examples • pow( 3 , 2 ) • min( 16/3, 4 ) • print( "Hi there!" ) • x = -6y = abs(x) fc2a)Start argumentlistwithopen parenthesis
Function Calls • Examples • pow(3 , 2 ) • min( 16/3, 4 ) • print("Hi there!" ) • x = -6y = abs(x) fc2b)Write expressionswhose values used for that input