announcements 61a lecture 3
play

Announcements 61A Lecture 3 None Indicates that Nothing is Returned - PDF document

Announcements 61A Lecture 3 None Indicates that Nothing is Returned The special value None represents nothing in Python A function that does not explicitly return a value will return None Careful : None is not displayed by the interpreter as the


  1. Announcements 61A Lecture 3 None Indicates that Nothing is Returned The special value None represents nothing in Python A function that does not explicitly return a value will return None Careful : None is not displayed by the interpreter as the value of an expression Print and None >>> def does_not_return_square(x): ... x * x No return ... None value is not displayed >>> does_not_return_square(4) (Demo) The name sixteen >>> sixteen = does_not_return_square(4) is now bound to >>> sixteen + 4 the value None Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'NoneType' and 'int' 4 Pure Functions & Non-Pure Functions Nested Expressions with Print None, None print(...): Return value Does not get None Pure Functions -2 abs displayed just return values 2 Argument display “None None” None 2, 100 pow print(print(1), print(2)) 1267650600228229401496703205376 2 Arguments None func print(...) None print(1) print(2) Returns None! Non-Pure Functions -2 print have side effects func print(...) 1 func print(...) None 2 A side effect isn't a value; it's anything 2 print(...): 1 print(...): Python displays the output “-2” that happens as a None None consequence of calling a function display “2” display “1” (Demo) 5 6 Life Cycle of a User-Defined Function What happens? Formal parameter R e t u r n e x p r e s s i o n N a m e Def statement: >>> def square( x ): A new function is created! return mul(x, x) Name bound to that function D e f in the current frame s t a t e m e n t B o d y (return statement) Multiple Environments operand: 2+2 Operator & operands evaluated Call expression: square(2+2) argument: 4 Function (value of operator) called on arguments 
 operator: square (values of operands) 
 function: func square(x) A new frame is created! 4 Calling/Applying: square( x ): Parameters bound to arguments 16 Argument Signature Body is executed in that new environment Return value 8

  2. Multiple Environments in One Diagram! Multiple Environments in One Diagram! square(square(3)) square(square(3)) func square(x) 9 func square(x) square(3) square(3) func square(x) 3 func square(x) 3 Interactive Diagram 9 Interactive Diagram 10 Multiple Environments in One Diagram! Names Have No Meaning Without Environments 1 1 2 2 2 2 1 1 1 1 Every expression is 81 evaluated in the context square(square(3)) of an environment. 9 func square(x) A name evaluates to the value bound to that name square(3) An environment is a sequence of frames. An environment is a sequence of frames. in the earliest frame of the current environment in • The global frame alone • The global frame alone func square(x) 3 which that name is found. • • A local, then the global frame A local, then the global frame Interactive Diagram Interactive Diagram 11 12 Names Have Different Meanings in Different Environments A call expression and the body of the function being called are evaluated in different environments 1 2 Miscellaneous Python Features 1 Every expression is Division evaluated in the context Multiple Return Values of an environment. Source Files Doctests A name evaluates to the Default Arguments value bound to that name in the earliest frame of the current environment in which that name is found. (Demo) Interactive Diagram 13 Statements A statement is executed by the interpreter to perform an action Compound statements: The first header determines a Statement Conditional Statements Clause statement’s type <header>: The header of a clause <statement> Suite “controls” the suite that <statement> follows ... <separating header>: <statement> <statement> def statements are compound ... statements ... 16

  3. Compound Statements Conditional Statements (Demo) Compound statements: def absolute_value(x): A suite is a sequence of <header>: """Return the absolute value of x.""" statements <statement> if x < 0: Suite <statement> return -x 1 statement, ... elif x == 0: 3 clauses, 
 To “execute” a suite means to <separating header>: return 0 3 headers, execute its sequence of else: <statement> 3 suites statements, in order <statement> return x ... ... Execution Rule for Conditional Statements: Syntax Tips: Execution Rule for a sequence of statements: Each clause is considered in order. 1. Always starts with "if" clause. • Execute the first statement 1. Evaluate the header's expression. 2. Zero or more "elif" clauses. • Unless directed otherwise, execute the rest 2. If it is a true value, 
 3. Zero or one "else" clause, 
 execute the suite & skip the remaining clauses. always at the end. 17 18 Boolean Contexts Boolean Contexts def absolute_value(x): def absolute_value(x): """Return the absolute value of x.""" """Return the absolute value of x.""" if x < 0: if x < 0: return -x return -x elif x == 0: elif x == 0: Two boolean contexts Two boolean contexts return 0 return 0 else: else: return x return x George Boole George Boole False values in Python: False, 0, '', None (more to come) True values in Python: Anything else (True) Read Section 1.5.4! 19 Reading: http://composingprograms.com/pages/15-control.html#conditional-statements 20 While Statements (Demo) 1 2 3 1 3 6 Iteration George Boole Execution Rule for While Statements: 1. Evaluate the header’s expression. 2. If it is a true value, 
 execute the (whole) suite, 
 then return to step 1. 22

Recommend


More recommend