61A Lecture 3 Wednesday, August 31 Life Cycle of a User-Defined - - PowerPoint PPT Presentation

61a lecture 3
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

61A Lecture 3

Wednesday, August 31

slide-2
SLIDE 2

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?

slide-3
SLIDE 3

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?

slide-4
SLIDE 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 What happens?

slide-5
SLIDE 5

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?

slide-6
SLIDE 6

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?

slide-7
SLIDE 7

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?

slide-8
SLIDE 8

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?

slide-9
SLIDE 9

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?

slide-10
SLIDE 10

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?

slide-11
SLIDE 11

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

slide-12
SLIDE 12

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

slide-13
SLIDE 13

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

slide-14
SLIDE 14

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

slide-15
SLIDE 15

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

slide-16
SLIDE 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

slide-17
SLIDE 17

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

slide-18
SLIDE 18

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

slide-19
SLIDE 19

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

slide-20
SLIDE 20

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

slide-21
SLIDE 21

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

slide-22
SLIDE 22

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

slide-23
SLIDE 23

Multiple Environments in One Diagram!

3

Example: http://goo.gl/668fU

square(square(3)) square(3) 9 3 square square

slide-24
SLIDE 24

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
slide-25
SLIDE 25

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

slide-26
SLIDE 26

Formal Parameters

6

Example: http://goo.gl/OapJa

slide-27
SLIDE 27

Formal Parameters

6

def square(x): return mul(x, x)

Example: http://goo.gl/OapJa

slide-28
SLIDE 28

Formal Parameters

6

def square(x): return mul(x, x) vs

Example: http://goo.gl/OapJa

slide-29
SLIDE 29

Formal Parameters

6

def square(x): return mul(x, x) def square(y): return mul(y, y) vs

Example: http://goo.gl/OapJa

slide-30
SLIDE 30

Formal Parameters

6

def square(x): return mul(x, x) def square(y): return mul(y, y) vs

Example: http://goo.gl/OapJa

slide-31
SLIDE 31

Formal Parameters

6

def square(x): return mul(x, x) def square(y): return mul(y, y) vs

Example: http://goo.gl/OapJa

slide-32
SLIDE 32

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

slide-33
SLIDE 33

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

slide-34
SLIDE 34

Python Feature Demonstration

<Demo> Operators Multiple Return Values Docstrings Doctests Default Arguments Statements </Demo>

7

slide-35
SLIDE 35

Statements

8

A statement is executed by the interpret to perform an action

slide-36
SLIDE 36

<header>: <statement> <statement> ... <separating header>: <statement> <statement> ... ... Compound statements:

Statements

8

A statement is executed by the interpret to perform an action

slide-37
SLIDE 37

<header>: <statement> <statement> ... <separating header>: <statement> <statement> ... ... Compound statements:

Statements

8

A statement is executed by the interpret to perform an action Statement

slide-38
SLIDE 38

<header>: <statement> <statement> ... <separating header>: <statement> <statement> ... ... Compound statements:

Statements

8

A statement is executed by the interpret to perform an action Statement Clause

slide-39
SLIDE 39

<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

slide-40
SLIDE 40

<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

slide-41
SLIDE 41

<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

slide-42
SLIDE 42

<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

slide-43
SLIDE 43

Compound Statements

9

Compound statements: <header>: <statement> <statement> ... <separating header>: <statement> <statement> ... ... Suite

slide-44
SLIDE 44

Compound Statements

9

Compound statements: <header>: <statement> <statement> ... <separating header>: <statement> <statement> ... ... Suite A suite is a sequence

  • f statements
slide-45
SLIDE 45

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

slide-46
SLIDE 46

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

slide-47
SLIDE 47

Local Assignment

def percent_difference(x, y): difference = abs(x-y) return 100 * difference / x percent_difference(40, 50)

10

slide-48
SLIDE 48

Local Assignment

def percent_difference(x, y): difference = abs(x-y) return 100 * difference / x percent_difference(40, 50)

10

slide-49
SLIDE 49

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

slide-50
SLIDE 50

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

slide-51
SLIDE 51

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

slide-52
SLIDE 52

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.
slide-53
SLIDE 53

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

slide-54
SLIDE 54

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

slide-55
SLIDE 55

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

slide-56
SLIDE 56

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

slide-57
SLIDE 57

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

slide-58
SLIDE 58

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

slide-59
SLIDE 59

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

slide-60
SLIDE 60

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

slide-61
SLIDE 61

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!

slide-62
SLIDE 62

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

slide-63
SLIDE 63

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

slide-64
SLIDE 64

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

slide-65
SLIDE 65

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

slide-66
SLIDE 66

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

slide-67
SLIDE 67

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

slide-68
SLIDE 68

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

slide-69
SLIDE 69

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

slide-70
SLIDE 70

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

slide-71
SLIDE 71

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

slide-72
SLIDE 72

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

slide-73
SLIDE 73

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

slide-74
SLIDE 74

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

slide-75
SLIDE 75

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

slide-76
SLIDE 76

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

slide-77
SLIDE 77

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

slide-78
SLIDE 78

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

slide-79
SLIDE 79

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

slide-80
SLIDE 80

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

slide-81
SLIDE 81

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

slide-82
SLIDE 82

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

slide-83
SLIDE 83

The Fibonacci Sequence

15

slide-84
SLIDE 84

The Fibonacci Sequence

15

0, 1, 1, 2, 3, 5, 8, 13, ...

slide-85
SLIDE 85

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

slide-86
SLIDE 86

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

slide-87
SLIDE 87

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

slide-88
SLIDE 88

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

slide-89
SLIDE 89

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

slide-90
SLIDE 90

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

slide-91
SLIDE 91

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

slide-92
SLIDE 92

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

slide-93
SLIDE 93

Project 1: Hog

16

(Demo)