61A Lecture 3 Wednesday, August 31 Life Cycle of a User-Defined - - PowerPoint PPT Presentation
61A Lecture 3 Wednesday, August 31 Life Cycle of a User-Defined - - PowerPoint PPT Presentation
61A Lecture 3 Wednesday, August 31 Life Cycle of a User-Defined Function What happens? Def statement: >>> def square ( x ): return mul(x, x) Call expression: square(2+2) Calling/Applying: square ( x ): return mul(x, x) 2 Life
Life Cycle of a User-Defined Function
2
Def statement: Call expression: square( x ): return mul(x, x) >>> def square(2+2) Calling/Applying: square( x ): return mul(x, x) What happens?
Life Cycle of a User-Defined Function
2
Def statement: Call expression: square( x ): return mul(x, x) >>> def square(2+2) Calling/Applying: square( x ): return mul(x, x) D e f s t a t e m e n t What happens?
Life Cycle of a User-Defined Function
2
Def statement: Call expression: square( x ): return mul(x, x) >>> def square(2+2) Calling/Applying: square( x ): return mul(x, x) D e f s t a t e m e n t Formal parameter What happens?
Life Cycle of a User-Defined Function
2
Def statement: Call expression: square( x ): return mul(x, x) >>> def square(2+2) Calling/Applying: square( x ): return mul(x, x) D e f s t a t e m e n t Formal parameter B
- d
y What happens?
Life Cycle of a User-Defined Function
2
Def statement: Call expression: square( x ): return mul(x, x) >>> def square(2+2) Calling/Applying: square( x ): return mul(x, x) D e f s t a t e m e n t Formal parameter B
- d
y (return statement) What happens?
Life Cycle of a User-Defined Function
2
Def statement: Call expression: square( x ): return mul(x, x) >>> def square(2+2) Calling/Applying: square( x ): return mul(x, x) D e f s t a t e m e n t Formal parameter B
- d
y R e t u r n e x p r e s s i
- n
(return statement) What happens?
Life Cycle of a User-Defined Function
2
Def statement: Call expression: square( x ): return mul(x, x) >>> def square(2+2) Calling/Applying: square( x ): return mul(x, x) D e f s t a t e m e n t Formal parameter B
- d
y R e t u r n e x p r e s s i
- n
(return statement) Function created What happens?
Life Cycle of a User-Defined Function
2
Def statement: Call expression: square( x ): return mul(x, x) >>> def square(2+2) Calling/Applying: square( x ): return mul(x, x) D e f s t a t e m e n t Formal parameter B
- d
y R e t u r n e x p r e s s i
- n
(return statement) Function created Name bound What happens?
Life Cycle of a User-Defined Function
2
Def statement: Call expression: square( x ): return mul(x, x) >>> def square(2+2) Calling/Applying: square( x ): return mul(x, x) D e f s t a t e m e n t Formal parameter B
- d
y R e t u r n e x p r e s s i
- n
(return statement) Function created Name bound
- perand: 2+2
argument: 4 What happens?
Life Cycle of a User-Defined Function
2
Def statement: Call expression: square( x ): return mul(x, x) >>> def square(2+2) Calling/Applying: square( x ): return mul(x, x) D e f s t a t e m e n t Formal parameter B
- d
y R e t u r n e x p r e s s i
- n
(return statement) Function created Name bound
- perand: 2+2
argument: 4 What happens?
- perator: square
function: square
Life Cycle of a User-Defined Function
2
Def statement: Call expression: square( x ): return mul(x, x) >>> def square(2+2) Calling/Applying: square( x ): return mul(x, x) D e f s t a t e m e n t Formal parameter B
- d
y R e t u r n e x p r e s s i
- n
(return statement) Function created Name bound
- perand: 2+2
argument: 4 Op's evaluated What happens?
- perator: square
function: square
Life Cycle of a User-Defined Function
2
Def statement: Call expression: square( x ): return mul(x, x) >>> def square(2+2) Calling/Applying: square( x ): return mul(x, x) D e f s t a t e m e n t Formal parameter B
- d
y R e t u r n e x p r e s s i
- n
(return statement) Function created Name bound
- perand: 2+2
argument: 4 Op's evaluated Function called with argument(s) What happens?
- perator: square
function: square
Life Cycle of a User-Defined Function
2
Def statement: Call expression: square( x ): return mul(x, x) >>> def square(2+2) Calling/Applying: square( x ): return mul(x, x) D e f s t a t e m e n t Formal parameter B
- d
y R e t u r n e x p r e s s i
- n
(return statement) Function created Name bound
- perand: 2+2
argument: 4 Op's evaluated Function called with argument(s) What happens?
- perator: square
function: square Signature
Life Cycle of a User-Defined Function
2
Def statement: Call expression: square( x ): return mul(x, x) >>> def square(2+2) Calling/Applying: square( x ): return mul(x, x) D e f s t a t e m e n t Formal parameter B
- d
y R e t u r n e x p r e s s i
- n
(return statement) Function created Name bound
- perand: 2+2
argument: 4 Op's evaluated Function called with argument(s) What happens?
- perator: square
function: square Signature 4
Life Cycle of a User-Defined Function
2
Def statement: Call expression: square( x ): return mul(x, x) >>> def square(2+2) Calling/Applying: square( x ): return mul(x, x) D e f s t a t e m e n t Formal parameter B
- d
y R e t u r n e x p r e s s i
- n
(return statement) Function created Name bound
- perand: 2+2
argument: 4 Op's evaluated Function called with argument(s) What happens?
- perator: square
function: square Signature 4 16
Life Cycle of a User-Defined Function
2
Def statement: Call expression: square( x ): return mul(x, x) >>> def square(2+2) Calling/Applying: square( x ): return mul(x, x) D e f s t a t e m e n t Formal parameter B
- d
y R e t u r n e x p r e s s i
- n
(return statement) Function created Name bound
- perand: 2+2
argument: 4 Op's evaluated Function called with argument(s) What happens?
- perator: square
function: square Signature 4 16 Argument
Life Cycle of a User-Defined Function
2
Def statement: Call expression: square( x ): return mul(x, x) >>> def square(2+2) Calling/Applying: square( x ): return mul(x, x) D e f s t a t e m e n t Formal parameter B
- d
y R e t u r n e x p r e s s i
- n
(return statement) Function created Name bound
- perand: 2+2
argument: 4 Op's evaluated Function called with argument(s) What happens?
- perator: square
function: square Signature 4 16 Argument Return value
Life Cycle of a User-Defined Function
2
Def statement: Call expression: square( x ): return mul(x, x) >>> def square(2+2) Calling/Applying: square( x ): return mul(x, x) D e f s t a t e m e n t Formal parameter B
- d
y R e t u r n e x p r e s s i
- n
(return statement) Function created Name bound
- perand: 2+2
argument: 4 Op's evaluated Function called with argument(s) What happens?
- perator: square
function: square Signature 4 16 New frame! Argument Return value
Life Cycle of a User-Defined Function
2
Def statement: Call expression: square( x ): return mul(x, x) >>> def square(2+2) Calling/Applying: square( x ): return mul(x, x) D e f s t a t e m e n t Formal parameter B
- d
y R e t u r n e x p r e s s i
- n
(return statement) Function created Name bound
- perand: 2+2
argument: 4 Op's evaluated Function called with argument(s) What happens?
- perator: square
function: square Signature 4 16 New frame! Params bound Argument Return value
Life Cycle of a User-Defined Function
2
Def statement: Call expression: square( x ): return mul(x, x) >>> def square(2+2) Calling/Applying: square( x ): return mul(x, x) D e f s t a t e m e n t Formal parameter B
- d
y R e t u r n e x p r e s s i
- n
(return statement) Function created Name bound
- perand: 2+2
argument: 4 Op's evaluated Function called with argument(s) What happens?
- perator: square
function: square Signature 4 16 New frame! Params bound Body executed Argument Return value
Life Cycle of a User-Defined Function
2
Def statement: Call expression: square( x ): return mul(x, x) >>> def square(2+2) Calling/Applying: square( x ): return mul(x, x) D e f s t a t e m e n t Formal parameter B
- d
y R e t u r n e x p r e s s i
- n
(return statement) Function created Name bound
- perand: 2+2
argument: 4 Op's evaluated Function called with argument(s) What happens?
- perator: square
function: square Signature 4 16 New frame! Params bound Body executed Argument Return value N a m e
Multiple Environments in One Diagram!
3
Example: http://goo.gl/668fU
square(square(3)) square(3) 9 3 square square
Multiple Environments in One Diagram!
4
Example: http://goo.gl/668fU
27 square(square(3)) square(3) 9 3 square square An environment is a sequence of frames.
1 2 1 2 1
- The global frame alone
- A local, then the global frame
Names Have No Meaning Without Environments
5
Example: http://goo.gl/668fU
1 2 2 1 1
Every expression is evaluated in the context of an environment. A name evaluates to the value bound to that name in the earliest frame of the current environment in which that name is found. mul(x, x) “ m u l ” i s n
- t
f
- u
n d
Formal Parameters
6
Example: http://goo.gl/OapJa
Formal Parameters
6
def square(x): return mul(x, x)
Example: http://goo.gl/OapJa
Formal Parameters
6
def square(x): return mul(x, x) vs
Example: http://goo.gl/OapJa
Formal Parameters
6
def square(x): return mul(x, x) def square(y): return mul(y, y) vs
Example: http://goo.gl/OapJa
Formal Parameters
6
def square(x): return mul(x, x) def square(y): return mul(y, y) vs
Example: http://goo.gl/OapJa
Formal Parameters
6
def square(x): return mul(x, x) def square(y): return mul(y, y) vs
Example: http://goo.gl/OapJa
Formal Parameters
6
def square(x): return mul(x, x) def square(y): return mul(y, y) vs Formal parameters have local scope
Example: http://goo.gl/OapJa
Formal Parameters
6
def square(x): return mul(x, x) def square(y): return mul(y, y) vs Formal parameters have local scope (Demo)
Example: http://goo.gl/OapJa
Python Feature Demonstration
<Demo> Operators Multiple Return Values Docstrings Doctests Default Arguments Statements </Demo>
7
Statements
8
A statement is executed by the interpret to perform an action
<header>: <statement> <statement> ... <separating header>: <statement> <statement> ... ... Compound statements:
Statements
8
A statement is executed by the interpret to perform an action
<header>: <statement> <statement> ... <separating header>: <statement> <statement> ... ... Compound statements:
Statements
8
A statement is executed by the interpret to perform an action Statement
<header>: <statement> <statement> ... <separating header>: <statement> <statement> ... ... Compound statements:
Statements
8
A statement is executed by the interpret to perform an action Statement Clause
<header>: <statement> <statement> ... <separating header>: <statement> <statement> ... ... Compound statements:
Statements
8
A statement is executed by the interpret to perform an action Statement Suite Clause
<header>: <statement> <statement> ... <separating header>: <statement> <statement> ... ... Compound statements:
Statements
8
A statement is executed by the interpret to perform an action Statement Suite Clause The first header determines a statement’s type
<header>: <statement> <statement> ... <separating header>: <statement> <statement> ... ... Compound statements:
Statements
8
A statement is executed by the interpret to perform an action Statement Suite Clause The first header determines a statement’s type The header of a clause “controls” the suite that follows
<header>: <statement> <statement> ... <separating header>: <statement> <statement> ... ... Compound statements:
Statements
8
A statement is executed by the interpret to perform an action Statement Suite Clause The first header determines a statement’s type The header of a clause “controls” the suite that follows def statements are compound statements
Compound Statements
9
Compound statements: <header>: <statement> <statement> ... <separating header>: <statement> <statement> ... ... Suite
Compound Statements
9
Compound statements: <header>: <statement> <statement> ... <separating header>: <statement> <statement> ... ... Suite A suite is a sequence
- f statements
Compound Statements
9
Compound statements: <header>: <statement> <statement> ... <separating header>: <statement> <statement> ... ... Suite A suite is a sequence
- f statements
To “execute” a suite means to execute its sequence of statements, in order
Compound Statements
9
Compound statements: <header>: <statement> <statement> ... <separating header>: <statement> <statement> ... ... Execution Rule for a sequence of statements:
- Execute the first
- Unless directed otherwise, execute the rest
Suite A suite is a sequence
- f statements
To “execute” a suite means to execute its sequence of statements, in order
Local Assignment
def percent_difference(x, y): difference = abs(x-y) return 100 * difference / x percent_difference(40, 50)
10
Local Assignment
def percent_difference(x, y): difference = abs(x-y) return 100 * difference / x percent_difference(40, 50)
10
Conditional Statements
11
def absolute_value(x): """Return the absolute value of x.""" if x > 0: return x elif x == 0: return 0 else: return -x
Conditional Statements
11
def absolute_value(x): """Return the absolute value of x.""" if x > 0: return x elif x == 0: return 0 else: return -x 1 statement, 3 clauses, 3 headers, 3 suites
Conditional Statements
Execution rule for conditional statements:
11
def absolute_value(x): """Return the absolute value of x.""" if x > 0: return x elif x == 0: return 0 else: return -x 1 statement, 3 clauses, 3 headers, 3 suites
Conditional Statements
Execution rule for conditional statements:
11
def absolute_value(x): """Return the absolute value of x.""" if x > 0: return x elif x == 0: return 0 else: return -x 1 statement, 3 clauses, 3 headers, 3 suites Each clause is considered in order.
- 1. Evaluate the header's expression.
- 2. If it is a true value, execute the suite & skip the rest.
Boolean Contexts
12
def absolute_value(x): """Return the absolute value of x.""" if x > 0: return x elif x == 0: return 0 else: return -x
George Boole
Boolean Contexts
13
def absolute_value(x): """Return the absolute value of x.""" if x > 0: return x elif x == 0: return 0 else: return -x
George Boole
Boolean Contexts
13
def absolute_value(x): """Return the absolute value of x.""" if x > 0: return x elif x == 0: return 0 else: return -x Two boolean contexts
George Boole
Boolean Contexts
13
def absolute_value(x): """Return the absolute value of x.""" if x > 0: return x elif x == 0: return 0 else: return -x Two boolean contexts
George Boole
Boolean Contexts
13
def absolute_value(x): """Return the absolute value of x.""" if x > 0: return x elif x == 0: return 0 else: return -x Two boolean contexts
George Boole
Boolean Contexts
13
def absolute_value(x): """Return the absolute value of x.""" if x > 0: return x elif x == 0: return 0 else: return -x Two boolean contexts False values in Python: False, 0, '', None
George Boole
Boolean Contexts
13
def absolute_value(x): """Return the absolute value of x.""" if x > 0: return x elif x == 0: return 0 else: return -x Two boolean contexts False values in Python: False, 0, '', None (more to come)
George Boole
Boolean Contexts
13
def absolute_value(x): """Return the absolute value of x.""" if x > 0: return x elif x == 0: return 0 else: return -x Two boolean contexts False values in Python: False, 0, '', None True values in Python: Anything else (True) (more to come)
George Boole
Boolean Contexts
13
def absolute_value(x): """Return the absolute value of x.""" if x > 0: return x elif x == 0: return 0 else: return -x Two boolean contexts False values in Python: False, 0, '', None True values in Python: Anything else (True) (more to come)
George Boole
Read Section 1.5.4!
Iteration
14
- 1. Evaluate the header’s expression.
- 2. If it is a true value,
execute the (whole) suite, then return to step 1. Execution rule for while statements: i, total = 0, 0 while i < 3: i = i + 1 total = total + i 1 2 3 6
Iteration
14
- 1. Evaluate the header’s expression.
- 2. If it is a true value,
execute the (whole) suite, then return to step 1. Execution rule for while statements: i, total = 0, 0 while i < 3: i = i + 1 total = total + i 1 2 3 6
Iteration
14
- 1. Evaluate the header’s expression.
- 2. If it is a true value,
execute the (whole) suite, then return to step 1. Execution rule for while statements: i, total = 0, 0 while i < 3: i = i + 1 total = total + i 1 2 3 6
Iteration
14
- 1. Evaluate the header’s expression.
- 2. If it is a true value,
execute the (whole) suite, then return to step 1. Execution rule for while statements: i, total = 0, 0 while i < 3: i = i + 1 total = total + i i: total: 1 2 3 6
Iteration
14
- 1. Evaluate the header’s expression.
- 2. If it is a true value,
execute the (whole) suite, then return to step 1. Execution rule for while statements: i, total = 0, 0 while i < 3: i = i + 1 total = total + i i: total: 1 2 3 6
Iteration
14
- 1. Evaluate the header’s expression.
- 2. If it is a true value,
execute the (whole) suite, then return to step 1. Execution rule for while statements: i, total = 0, 0 while i < 3: i = i + 1 total = total + i i: total: 1 2 3 6
Iteration
14
- 1. Evaluate the header’s expression.
- 2. If it is a true value,
execute the (whole) suite, then return to step 1. Execution rule for while statements: i, total = 0, 0 while i < 3: i = i + 1 total = total + i i: total: 1 2 3 6
Iteration
14
- 1. Evaluate the header’s expression.
- 2. If it is a true value,
execute the (whole) suite, then return to step 1. Execution rule for while statements: i, total = 0, 0 while i < 3: i = i + 1 total = total + i i: total: 1 2 3 6
Iteration
14
- 1. Evaluate the header’s expression.
- 2. If it is a true value,
execute the (whole) suite, then return to step 1. Execution rule for while statements: i, total = 0, 0 while i < 3: i = i + 1 total = total + i i: total: 1 2 3 6
Iteration
14
- 1. Evaluate the header’s expression.
- 2. If it is a true value,
execute the (whole) suite, then return to step 1. Execution rule for while statements: i, total = 0, 0 while i < 3: i = i + 1 total = total + i i: total: 1 2 3 6
Iteration
14
- 1. Evaluate the header’s expression.
- 2. If it is a true value,
execute the (whole) suite, then return to step 1. Execution rule for while statements: i, total = 0, 0 while i < 3: i = i + 1 total = total + i i: total: 1 2 3 6
Iteration
14
- 1. Evaluate the header’s expression.
- 2. If it is a true value,
execute the (whole) suite, then return to step 1. Execution rule for while statements: i, total = 0, 0 while i < 3: i = i + 1 total = total + i i: total: 1 2 3 6
Iteration
14
- 1. Evaluate the header’s expression.
- 2. If it is a true value,
execute the (whole) suite, then return to step 1. Execution rule for while statements: i, total = 0, 0 while i < 3: i = i + 1 total = total + i i: total: 1 2 3 6
Iteration
14
- 1. Evaluate the header’s expression.
- 2. If it is a true value,
execute the (whole) suite, then return to step 1. Execution rule for while statements: i, total = 0, 0 while i < 3: i = i + 1 total = total + i i: total: 1 2 3 6
Iteration
14
- 1. Evaluate the header’s expression.
- 2. If it is a true value,
execute the (whole) suite, then return to step 1. Execution rule for while statements: i, total = 0, 0 while i < 3: i = i + 1 total = total + i i: total: 1 2 3 6
Iteration
14
- 1. Evaluate the header’s expression.
- 2. If it is a true value,
execute the (whole) suite, then return to step 1. Execution rule for while statements: i, total = 0, 0 while i < 3: i = i + 1 total = total + i i: total: 1 2 3 6
Iteration
14
- 1. Evaluate the header’s expression.
- 2. If it is a true value,
execute the (whole) suite, then return to step 1. Execution rule for while statements: i, total = 0, 0 while i < 3: i = i + 1 total = total + i i: total: 1 2 3 6
Iteration
14
- 1. Evaluate the header’s expression.
- 2. If it is a true value,
execute the (whole) suite, then return to step 1. Execution rule for while statements: i, total = 0, 0 while i < 3: i = i + 1 total = total + i i: total: 1 2 3 6
Iteration
14
- 1. Evaluate the header’s expression.
- 2. If it is a true value,
execute the (whole) suite, then return to step 1. Execution rule for while statements: i, total = 0, 0 while i < 3: i = i + 1 total = total + i i: total: 1 2 3 6
Iteration
14
- 1. Evaluate the header’s expression.
- 2. If it is a true value,
execute the (whole) suite, then return to step 1. Execution rule for while statements: i, total = 0, 0 while i < 3: i = i + 1 total = total + i i: total: 1 2 3 6
Iteration
14
- 1. Evaluate the header’s expression.
- 2. If it is a true value,
execute the (whole) suite, then return to step 1. Execution rule for while statements: i, total = 0, 0 while i < 3: i = i + 1 total = total + i i: total: 1 2 3 6
The Fibonacci Sequence
15
The Fibonacci Sequence
15
0, 1, 1, 2, 3, 5, 8, 13, ...
The Fibonacci Sequence
def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" pred, curr = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fib number is curr while k < n: pred, curr = curr, pred + curr k = k + 1 return curr
15
0, 1, 1, 2, 3, 5, 8, 13, ...
pred: curr: ...
The Fibonacci Sequence
def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" pred, curr = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fib number is curr while k < n: pred, curr = curr, pred + curr k = k + 1 return curr
15
0, 1, 1, 2, 3, 5, 8, 13, ...
pred: curr: ...
The Fibonacci Sequence
def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" pred, curr = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fib number is curr while k < n: pred, curr = curr, pred + curr k = k + 1 return curr
15
0, 1, 1, 2, 3, 5, 8, 13, ...
pred: curr: ...
The Fibonacci Sequence
def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" pred, curr = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fib number is curr while k < n: pred, curr = curr, pred + curr k = k + 1 return curr
15
0, 1, 1, 2, 3, 5, 8, 13, ...
pred: curr: ...
The Fibonacci Sequence
def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" pred, curr = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fib number is curr while k < n: pred, curr = curr, pred + curr k = k + 1 return curr
15
0, 1, 1, 2, 3, 5, 8, 13, ...
pred: curr: ...
The Fibonacci Sequence
def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" pred, curr = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fib number is curr while k < n: pred, curr = curr, pred + curr k = k + 1 return curr
15
0, 1, 1, 2, 3, 5, 8, 13, ...
pred: curr: ...
The Fibonacci Sequence
def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" pred, curr = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fib number is curr while k < n: pred, curr = curr, pred + curr k = k + 1 return curr
15
0, 1, 1, 2, 3, 5, 8, 13, ...
pred: curr: ...
The Fibonacci Sequence
def fib(n): """Compute the nth Fibonacci number, for n >= 2.""" pred, curr = 0, 1 # First two Fibonacci numbers k = 2 # Tracks which Fib number is curr while k < n: pred, curr = curr, pred + curr k = k + 1 return curr
15
0, 1, 1, 2, 3, 5, 8, 13, ...
Project 1: Hog
16