61a lecture 3
play

61A Lecture 3 Similar in format to a homework assignment. If you - PDF document

Announcements Homework 1 is due next Tuesday at 5pm (no email when you submit). Homework is graded for effort. Take-home quiz released next Wednesday 9/11 at 1pm, due Thursday 9/12 at 11:59pm. 3 points, graded for correctness. 61A


  1. Announcements • Homework 1 is due next Tuesday at 5pm (no email when you submit).  Homework is graded for effort. • Take-home quiz released next Wednesday 9/11 at 1pm, due Thursday 9/12 at 11:59pm.  3 points, graded for correctness. 61A Lecture 3  Similar in format to a homework assignment.  If you receive 0/3, you will need to talk to the course staff or be dropped. Friday, September 6  Open-computer : You can use the Python interpreter, watch course videos, and read the online text (http://composingprograms.com).  No external resources : Please don't search for answers, talk to your classmates, etc. • Project 1 posted this Friday, due Thursday 9/19 at 11:59pm.  Demo during next lecture 2 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 square ( x ): Calling/Applying: Parameters bound to arguments 16 Argument Signature Body is executed in that new environment Return value 4 Multiple Environments in One Diagram! Multiple Environments in One Diagram! square(square(3)) square(square(3)) func square(x) func square(x) 9 square(3) square(3) func square(x) func square(x) 3 3 5 6 Example: http://goo.gl/XVtEms Example: http://goo.gl/XVtEms

  2. Multiple Environments in One Diagram! Names Have No Meaning Without Environments 1 1 2 2 2 2 1 1 81 square(square(3)) Every expression is 1 1 func square(x) 9 evaluated in the context of an environment. square(3) A name evaluates to the func square(x) An environment is a sequence of frames . An environment is a sequence of frames . 3 value bound to that name in the earliest frame of • • The global frame alone The global frame alone the current environment in • • A local, then the global frame which that name is found. A local, then the global frame 7 8 Example: http://goo.gl/XVtEms Example: http://goo.gl/XVtEms Miscellaneous Python Features Conditional Statements Operators Multiple Return Values Docstrings Doctests Default Arguments (Demo) Statements Compound Statements Compound statements : A statement is executed by the interpreter to perform an action A suite is a sequence of <header>: statements <statement> Suite Compound statements : <statement> ... The first header determines a <separating header>: Statement To “execute” a suite means to statement’s type <statement> Clause execute its sequence of <statement> statements, in order ... <header>: ... <statement> The header of a clause <statement> Suite “controls” the suite that ... follows <separating header>: Execution Rule for a sequence of statements: <statement> <statement> def statements are compound ... • Execute the first statement statements ... • Unless directed otherwise, execute the rest 11 12

  3. Conditional Statements Boolean Contexts (Demo) 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: 1 statement, return -x return -x elif x == 0: elif x == 0: 3 clauses, 3 headers, return 0 return 0 else : else : 3 suites return x return x George Boole Execution rule for conditional statements: Syntax Tips Each clause is considered in order. 1. Always starts with "if" clause. 1. Evaluate the header's expression. 2. Zero or more "elif" clauses. 2. If it is a true value, 3. Zero or one "else" clause, execute the suite & skip the remaining clauses. always at the end. 13 14 Boolean Contexts def absolute_value(x): """Return the absolute value of x.""" if x < 0: return -x elif x == 0: Two boolean contexts Two boolean contexts return 0 Iteration else : return x George Boole False values in Python: False, 0, '', None (more to come) True values in Python: Anything else (True) Read Section 1.5.4! 15 Reading: http://composingprograms.com/pages/15-control.html#conditional-statements While Statements Discussion Question (Demo) Complete the following definition by placing an expression in ______________________ . def choose(total, selection): """Return the number of ways to choose SELECTION items from TOTAL. 1 2 3 1 3 6 choose(n, k) is typically defined in math as: n! / (n-k)! / k! >>> choose(5, 2) n · ( n − 1) · ( n − 2) · . . . · ( n − k + 1) 10 k · ( k − 1) · ( k − 2) · . . . · 2 · 1 >>> choose(20, 6) Execution rule for while statements: George Boole 38760 """ 1. Evaluate the header’s expression. ways = 1 2. If it is a true value, selected = 0 execute the ( whole ) suite, ... while selected < selection: then return to step 1. ... selected = selected + 1 total // selected ways, total = ways * ______________________ , total - 1 return ways 17 18 Example: http://goo.gl/0d2cjF Example: http://goo.gl/38ch3o

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend