61a lecture 3 - university of california, berkeleynone indicates that nothing is returned the...

153
61A Lecture 3

Upload: others

Post on 19-Aug-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

61A Lecture 3

Page 2: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Announcements

Page 3: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Print and None

(Demo)

Page 4: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

None Indicates that Nothing is Returned

4

Page 5: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

None Indicates that Nothing is Returned

The special value None represents nothing in Python

4

Page 6: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

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

4

Page 7: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

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

4

Page 8: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

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

4

>>> def does_not_return_square(x):... x * x

...

Page 9: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

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

4

>>> def does_not_return_square(x):... x * x

... No return

Page 10: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

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

4

>>> def does_not_return_square(x):... x * x

... >>> does_not_return_square(4)

No return

Page 11: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

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

4

>>> def does_not_return_square(x):... x * x

... >>> does_not_return_square(4)

No return

None value is not displayed

Page 12: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

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

4

>>> def does_not_return_square(x):... x * x

... >>> does_not_return_square(4)

>>> sixteen = does_not_return_square(4)

No return

None value is not displayed

Page 13: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

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

4

>>> def does_not_return_square(x):... x * x

... >>> does_not_return_square(4)

>>> sixteen = does_not_return_square(4)The name sixteen is now bound to the value None

No return

None value is not displayed

Page 14: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

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

4

>>> def does_not_return_square(x):... x * x

... >>> does_not_return_square(4)

>>> sixteen = does_not_return_square(4)>>> sixteen + 4

Traceback (most recent call last): File "<stdin>", line 1, in <module>

TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

The name sixteen is now bound to the value None

No return

None value is not displayed

Page 15: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Pure Functions & Non-Pure Functions

Pure Functions just return values

Non-Pure Functions have side effects

5

Page 16: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

abs

Pure Functions & Non-Pure Functions

Pure Functions just return values

Non-Pure Functions have side effects

5

Page 17: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

abs

Pure Functions & Non-Pure Functions

-2Pure Functions just return values

Non-Pure Functions have side effects

5

Page 18: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

abs

Pure Functions & Non-Pure Functions

-22

Pure Functions just return values

Non-Pure Functions have side effects

5

Page 19: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

abs

Pure Functions & Non-Pure Functions

-22

Pure Functions just return values

Non-Pure Functions have side effects

Argument

5

Page 20: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

abs

Pure Functions & Non-Pure Functions

-22

Pure Functions just return values

Non-Pure Functions have side effects

Argument

Return value

5

Page 21: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

abs

Pure Functions & Non-Pure Functions

-22

pow

Pure Functions just return values

Non-Pure Functions have side effects

Argument

Return value

5

Page 22: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

abs

Pure Functions & Non-Pure Functions

-22

2, 100 pow

Pure Functions just return values

Non-Pure Functions have side effects

Argument

Return value

5

Page 23: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

abs

Pure Functions & Non-Pure Functions

-22

2, 100 pow

Pure Functions just return values

Non-Pure Functions have side effects

Argument

Return value

5

2 Arguments

Page 24: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

abs

Pure Functions & Non-Pure Functions

-22

2, 1001267650600228229401496703205376

pow

Pure Functions just return values

Non-Pure Functions have side effects

Argument

Return value

5

2 Arguments

Page 25: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

abs

Pure Functions & Non-Pure Functions

-22

print

2, 1001267650600228229401496703205376

pow

Pure Functions just return values

Non-Pure Functions have side effects

Argument

Return value

5

2 Arguments

Page 26: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

abs

Pure Functions & Non-Pure Functions

-22

-2 print

2, 1001267650600228229401496703205376

pow

Pure Functions just return values

Non-Pure Functions have side effects

Argument

Return value

5

2 Arguments

Page 27: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

abs

Pure Functions & Non-Pure Functions

-22

-2None

print

2, 1001267650600228229401496703205376

pow

Pure Functions just return values

Non-Pure Functions have side effects

Argument

Return value

5

2 Arguments

Page 28: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

abs

Pure Functions & Non-Pure Functions

-22

-2None

print

Python displays the output “-2”

2, 1001267650600228229401496703205376

pow

Pure Functions just return values

Non-Pure Functions have side effects

Argument

Return value

5

2 Arguments

Page 29: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

abs

Pure Functions & Non-Pure Functions

-22

-2None

print

Python displays the output “-2”

2, 1001267650600228229401496703205376

pow

Pure Functions just return values

Non-Pure Functions have side effects

Argument

Return value

Returns None!

5

2 Arguments

Page 30: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

abs

Pure Functions & Non-Pure Functions

-22

-2None

print

Python displays the output “-2”

2, 1001267650600228229401496703205376

pow

Pure Functions just return values

Non-Pure Functions have side effects

Argument

Return value

A side effect isn't a value; it's anything that happens as a consequence of

calling a function

Returns None!

5

2 Arguments

Page 31: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

abs

Pure Functions & Non-Pure Functions

-22

-2None

print

Python displays the output “-2”

2, 1001267650600228229401496703205376

pow

Pure Functions just return values

Non-Pure Functions have side effects

Argument

Return value

A side effect isn't a value; it's anything that happens as a consequence of

calling a function

Returns None!

5

2 Arguments

(Demo)

Page 32: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Nested Expressions with Print

6

Page 33: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Nested Expressions with Print

print(print(1), print(2))

6

Page 34: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Nested Expressions with Print

print(print(1), print(2))

6

Page 35: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Nested Expressions with Print

print(print(1), print(2))

func print(...)

6

Page 36: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Nested Expressions with Print

print(print(1), print(2))

func print(...)print(1)

func print(...) 1

6

Page 37: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Nested Expressions with Print

print(print(1), print(2))

func print(...)

print(...):1None

display “1”

print(1)

func print(...) 1

6

Page 38: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Nested Expressions with Print

print(print(1), print(2))

func print(...)

print(...):1None

display “1”

print(1)

func print(...) 1

None

6

Page 39: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Nested Expressions with Print

print(print(1), print(2))

func print(...)

print(...):1None

display “1”

print(1)

func print(...) 1

Noneprint(2)

2

6

func print(...)

Page 40: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Nested Expressions with Print

print(print(1), print(2))

func print(...)

print(...):1None

display “1”

print(...):2None

display “2”

print(1)

func print(...) 1

Noneprint(2)

2

6

func print(...)

Page 41: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Nested Expressions with Print

print(print(1), print(2))

func print(...)

print(...):1None

display “1”

print(...):2None

display “2”

print(1)

func print(...) 1

Noneprint(2)

2

None

6

func print(...)

Page 42: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Nested Expressions with Print

print(print(1), print(2))

func print(...)

print(...):1None

display “1”

print(...):2None

display “2”

print(...):None, NoneNone

display “None None”

print(1)

func print(...) 1

Noneprint(2)

2

None

6

func print(...)

Page 43: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Nested Expressions with Print

Noneprint(print(1), print(2))

func print(...)

print(...):1None

display “1”

print(...):2None

display “2”

print(...):None, NoneNone

display “None None”

print(1)

func print(...) 1

Noneprint(2)

2

None

6

func print(...)

Page 44: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Nested Expressions with Print

Noneprint(print(1), print(2))

func print(...)

print(...):1None

display “1”

print(...):2None

display “2”

print(...):None, NoneNone

display “None None”

print(1)

func print(...) 1

Noneprint(2)

2

None

6

func print(...)

Page 45: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Nested Expressions with Print

Noneprint(print(1), print(2))

func print(...)

print(...):1None

display “1”

print(...):2None

display “2”

print(...):None, NoneNone

display “None None”

print(1)

func print(...) 1

Noneprint(2)

2

None

6

Does not get displayed

func print(...)

Page 46: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Multiple Environments

Page 47: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Life Cycle of a User-Defined Function

Def statement:

Call expression:

Calling/Applying:

What happens?

8

Page 48: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Life Cycle of a User-Defined Function

Def statement:

Call expression:

square( x ):

return mul(x, x)

>>> def

Calling/Applying:

What happens?

8

Page 49: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Life Cycle of a User-Defined Function

Def statement:

Call expression:

square( x ):

return mul(x, x)

>>> def

Calling/Applying:

Def statement

What happens?

8

Page 50: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Life Cycle of a User-Defined Function

Def statement:

Call expression:

square( x ):

return mul(x, x)

>>> def

Calling/Applying:

Def statement

What happens?

Name

8

Page 51: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Life Cycle of a User-Defined Function

Def statement:

Call expression:

square( x ):

return mul(x, x)

>>> def

Calling/Applying:

Def statement

Formal parameter What happens?

Name

8

Page 52: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Life Cycle of a User-Defined Function

Def statement:

Call expression:

square( x ):

return mul(x, x)

>>> def

Calling/Applying:

Def statement

Formal parameter

Body

What happens?

Name

8

Page 53: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Life Cycle of a User-Defined Function

Def statement:

Call expression:

square( x ):

return mul(x, x)

>>> def

Calling/Applying:

Def statement

Formal parameter

Body (return statement)

What happens?

Name

8

Page 54: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Life Cycle of a User-Defined Function

Def statement:

Call expression:

square( x ):

return mul(x, x)

>>> def

Calling/Applying:

Def statement

Formal parameter

Body

Return expression

(return statement)

What happens?

Name

8

Page 55: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Life Cycle of a User-Defined Function

Def statement:

Call expression:

square( x ):

return mul(x, x)

>>> def

Calling/Applying:

Def statement

Formal parameter

Body

Return expression

(return statement)

A new function is created!

What happens?

Name

8

Page 56: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Life Cycle of a User-Defined Function

Def statement:

Call expression:

square( x ):

return mul(x, x)

>>> def

Calling/Applying:

Def statement

Formal parameter

Body

Return expression

(return statement)

A new function is created!

Name bound to that function in the current frame

What happens?

Name

8

Page 57: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Life Cycle of a User-Defined Function

Def statement:

Call expression:

square( x ):

return mul(x, x)

>>> def

square(2+2)

Calling/Applying:

Def statement

Formal parameter

Body

Return expression

(return statement)

A new function is created!

Name bound to that function in the current frame

What happens?

Name

8

Page 58: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Life Cycle of a User-Defined Function

Def statement:

Call expression:

square( x ):

return mul(x, x)

>>> def

square(2+2)

Calling/Applying:

Def statement

Formal parameter

Body

Return expression

(return statement)

A new function is created!

Name bound to that function in the current frame

What happens?

operator: square function: func square(x)

Name

8

Page 59: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Life Cycle of a User-Defined Function

Def statement:

Call expression:

square( x ):

return mul(x, x)

>>> def

square(2+2)

Calling/Applying:

Def statement

Formal parameter

Body

Return expression

(return statement)

A new function is created!

Name bound to that function in the current frame

operand: 2+2 argument: 4

What happens?

operator: square function: func square(x)

Name

8

Page 60: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Life Cycle of a User-Defined Function

Def statement:

Call expression:

square( x ):

return mul(x, x)

>>> def

square(2+2)

Calling/Applying:

Def statement

Formal parameter

Body

Return expression

(return statement)

A new function is created!

Name bound to that function in the current frame

operand: 2+2 argument: 4

Operator & operands evaluated

What happens?

operator: square function: func square(x)

Name

8

Page 61: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Life Cycle of a User-Defined Function

Def statement:

Call expression:

square( x ):

return mul(x, x)

>>> def

square(2+2)

Calling/Applying:

Def statement

Formal parameter

Body

Return expression

(return statement)

A new function is created!

Name bound to that function in the current frame

operand: 2+2 argument: 4

Operator & operands evaluated

Function (value of operator) called on arguments (values of operands)

What happens?

operator: square function: func square(x)

Name

8

Page 62: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Life Cycle of a User-Defined Function

Def statement:

Call expression:

square( x ):

return mul(x, x)

>>> def

square(2+2)

Calling/Applying: square( x ):

Def statement

Formal parameter

Body

Return expression

(return statement)

A new function is created!

Name bound to that function in the current frame

operand: 2+2 argument: 4

Operator & operands evaluated

Function (value of operator) called on arguments (values of operands)

What happens?

operator: square function: func square(x)

Name

8

Page 63: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Life Cycle of a User-Defined Function

Def statement:

Call expression:

square( x ):

return mul(x, x)

>>> def

square(2+2)

Calling/Applying: square( x ):

Def statement

Formal parameter

Body

Return expression

(return statement)

A new function is created!

Name bound to that function in the current frame

operand: 2+2 argument: 4

Operator & operands evaluated

Function (value of operator) called on arguments (values of operands)

What happens?

operator: square function: func square(x)

Signature

Name

8

Page 64: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Life Cycle of a User-Defined Function

Def statement:

Call expression:

square( x ):

return mul(x, x)

>>> def

square(2+2)

Calling/Applying: square( x ):

Def statement

Formal parameter

Body

Return expression

(return statement)

A new function is created!

Name bound to that function in the current frame

operand: 2+2 argument: 4

Operator & operands evaluated

Function (value of operator) called on arguments (values of operands)

What happens?

operator: square function: func square(x)

Signature

4

Name

8

Page 65: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Life Cycle of a User-Defined Function

Def statement:

Call expression:

square( x ):

return mul(x, x)

>>> def

square(2+2)

Calling/Applying: square( x ):

Def statement

Formal parameter

Body

Return expression

(return statement)

A new function is created!

Name bound to that function in the current frame

operand: 2+2 argument: 4

Operator & operands evaluated

Function (value of operator) called on arguments (values of operands)

What happens?

operator: square function: func square(x)

Signature

4

16

Name

8

Page 66: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Life Cycle of a User-Defined Function

Def statement:

Call expression:

square( x ):

return mul(x, x)

>>> def

square(2+2)

Calling/Applying: square( x ):

Def statement

Formal parameter

Body

Return expression

(return statement)

A new function is created!

Name bound to that function in the current frame

operand: 2+2 argument: 4

Operator & operands evaluated

Function (value of operator) called on arguments (values of operands)

What happens?

operator: square function: func square(x)

Signature

4

16Argument

Name

8

Page 67: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Life Cycle of a User-Defined Function

Def statement:

Call expression:

square( x ):

return mul(x, x)

>>> def

square(2+2)

Calling/Applying: square( x ):

Def statement

Formal parameter

Body

Return expression

(return statement)

A new function is created!

Name bound to that function in the current frame

operand: 2+2 argument: 4

Operator & operands evaluated

Function (value of operator) called on arguments (values of operands)

What happens?

operator: square function: func square(x)

Signature

4

16Argument

Return value

Name

8

Page 68: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Life Cycle of a User-Defined Function

Def statement:

Call expression:

square( x ):

return mul(x, x)

>>> def

square(2+2)

Calling/Applying: square( x ):

Def statement

Formal parameter

Body

Return expression

(return statement)

A new function is created!

Name bound to that function in the current frame

operand: 2+2 argument: 4

Operator & operands evaluated

Function (value of operator) called on arguments (values of operands)

What happens?

operator: square function: func square(x)

Signature

4

16

A new frame is created!

Argument

Return value

Name

8

Page 69: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Life Cycle of a User-Defined Function

Def statement:

Call expression:

square( x ):

return mul(x, x)

>>> def

square(2+2)

Calling/Applying: square( x ):

Def statement

Formal parameter

Body

Return expression

(return statement)

A new function is created!

Name bound to that function in the current frame

operand: 2+2 argument: 4

Operator & operands evaluated

Function (value of operator) called on arguments (values of operands)

What happens?

operator: square function: func square(x)

Signature

4

16

A new frame is created!

Parameters bound to argumentsArgument

Return value

Name

8

Page 70: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Life Cycle of a User-Defined Function

Def statement:

Call expression:

square( x ):

return mul(x, x)

>>> def

square(2+2)

Calling/Applying: square( x ):

Def statement

Formal parameter

Body

Return expression

(return statement)

A new function is created!

Name bound to that function in the current frame

operand: 2+2 argument: 4

Operator & operands evaluated

Function (value of operator) called on arguments (values of operands)

What happens?

operator: square function: func square(x)

Signature

4

16

A new frame is created!

Parameters bound to arguments

Body is executed in that new environment

Argument

Return value

Name

8

Page 71: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Multiple Environments in One Diagram!

9Interactive Diagram

Page 72: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Multiple Environments in One Diagram!

9Interactive Diagram

Page 73: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Multiple Environments in One Diagram!

square(square(3))

9Interactive Diagram

Page 74: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Multiple Environments in One Diagram!

square(square(3))

9Interactive Diagram

Page 75: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Multiple Environments in One Diagram!

square(square(3))

func square(x)

9Interactive Diagram

Page 76: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Multiple Environments in One Diagram!

square(square(3))

square(3)func square(x)

9Interactive Diagram

Page 77: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Multiple Environments in One Diagram!

square(square(3))

square(3)func square(x)

9

func square(x)

Interactive Diagram

Page 78: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Multiple Environments in One Diagram!

square(square(3))

square(3)

3

func square(x)

9

func square(x)

Interactive Diagram

Page 79: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Multiple Environments in One Diagram!

square(square(3))

square(3)

3

func square(x)

9

func square(x)

Interactive Diagram

Page 80: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Multiple Environments in One Diagram!

square(square(3))

square(3)

3

func square(x)

10

func square(x)

Interactive Diagram

Page 81: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Multiple Environments in One Diagram!

square(square(3))

square(3)

3

func square(x)

10

func square(x)

Interactive Diagram

Page 82: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Multiple Environments in One Diagram!

square(square(3))

square(3)9

3

func square(x)

10

func square(x)

Interactive Diagram

Page 83: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Multiple Environments in One Diagram!

square(square(3))

square(3)9

3

func square(x)

10

func square(x)

Interactive Diagram

Page 84: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Multiple Environments in One Diagram!

11

square(square(3))

square(3)9

3

func square(x)

func square(x)

81

Interactive Diagram

Page 85: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Multiple Environments in One Diagram!

An environment is a sequence of frames.

11

square(square(3))

square(3)9

3

func square(x)

func square(x)

81

Interactive Diagram

Page 86: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Multiple Environments in One Diagram!

An environment is a sequence of frames.

• The global frame alone

• A local, then the global frame11

square(square(3))

square(3)9

3

func square(x)

func square(x)

81

Interactive Diagram

Page 87: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Multiple Environments in One Diagram!

An environment is a sequence of frames.

1

• The global frame alone

• A local, then the global frame11

square(square(3))

square(3)9

3

func square(x)

func square(x)

81

Interactive Diagram

Page 88: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Multiple Environments in One Diagram!

An environment is a sequence of frames.

1

2

1

• The global frame alone

• A local, then the global frame11

square(square(3))

square(3)9

3

func square(x)

func square(x)

81

Interactive Diagram

Page 89: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Multiple Environments in One Diagram!

An environment is a sequence of frames.

1

2

1

2

1

• The global frame alone

• A local, then the global frame11

square(square(3))

square(3)9

3

func square(x)

func square(x)

81

Interactive Diagram

Page 90: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Names Have No Meaning Without Environments

An environment is a sequence of frames.

• The global frame alone

• A local, then the global frame12

1

2

1

2

1

Interactive Diagram

Page 91: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Names Have No Meaning Without Environments

An environment is a sequence of frames.

• The global frame alone

• A local, then the global frame12

Every expression is evaluated in the context of an environment.

1

2

1

2

1

Interactive Diagram

Page 92: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Names Have No Meaning Without Environments

An environment is a sequence of frames.

• The global frame alone

• A local, then the global frame12

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.

1

2

1

2

1

Interactive Diagram

Page 93: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Names Have No Meaning Without Environments

An environment is a sequence of frames.

• The global frame alone

• A local, then the global frame12

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.

2

1

Interactive Diagram

Page 94: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Names Have No Meaning Without Environments

An environment is a sequence of frames.

• The global frame alone

• A local, then the global frame12

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.

2

1

Interactive Diagram

Page 95: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Names Have No Meaning Without Environments

An environment is a sequence of frames.

• The global frame alone

• A local, then the global frame12

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.

2

1

Interactive Diagram

Page 96: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Names Have Different Meanings in Different Environments

13Interactive Diagram

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.

Page 97: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Names Have Different Meanings in Different Environments

13

A call expression and the body of the function being called are evaluated in different environments

Interactive Diagram

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.

Page 98: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Names Have Different Meanings in Different Environments

13

A call expression and the body of the function being called are evaluated in different environments

Interactive Diagram

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.

Page 99: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Names Have Different Meanings in Different Environments

13

A call expression and the body of the function being called are evaluated in different environments

Interactive Diagram

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.

Page 100: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Names Have Different Meanings in Different Environments

13

1

A call expression and the body of the function being called are evaluated in different environments

Interactive Diagram

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.

Page 101: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Names Have Different Meanings in Different Environments

13

1

2

1

A call expression and the body of the function being called are evaluated in different environments

Interactive Diagram

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.

Page 102: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Miscellaneous Python Features

Division Multiple Return Values

Source Files Doctests

Default Arguments

(Demo)

Page 103: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Conditional Statements

Page 104: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Statements

16

A statement is executed by the interpreter to perform an action

Page 105: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

<header>: <statement> <statement> ... <separating header>: <statement> <statement> ... ...

Compound statements:

Statements

16

A statement is executed by the interpreter to perform an action

Page 106: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

<header>: <statement> <statement> ... <separating header>: <statement> <statement> ... ...

Compound statements:

Statements

Statement

16

A statement is executed by the interpreter to perform an action

Page 107: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

<header>: <statement> <statement> ... <separating header>: <statement> <statement> ... ...

Compound statements:

Statements

StatementClause

16

A statement is executed by the interpreter to perform an action

Page 108: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

<header>: <statement> <statement> ... <separating header>: <statement> <statement> ... ...

Compound statements:

Statements

Statement

Suite

Clause

16

A statement is executed by the interpreter to perform an action

Page 109: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

<header>: <statement> <statement> ... <separating header>: <statement> <statement> ... ...

Compound statements:

Statements

Statement

Suite

ClauseThe first header determines a statement’s type

16

A statement is executed by the interpreter to perform an action

Page 110: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

<header>: <statement> <statement> ... <separating header>: <statement> <statement> ... ...

Compound statements:

Statements

Statement

Suite

ClauseThe first header determines a statement’s type

The header of a clause “controls” the suite that follows

16

A statement is executed by the interpreter to perform an action

Page 111: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

<header>: <statement> <statement> ... <separating header>: <statement> <statement> ... ...

Compound statements:

Statements

Statement

Suite

ClauseThe first header determines a statement’s type

The header of a clause “controls” the suite that follows

def statements are compound statements

16

A statement is executed by the interpreter to perform an action

Page 112: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Compound Statements

Compound statements:

<header>: <statement> <statement> ... <separating header>: <statement> <statement> ... ...

Suite

17

Page 113: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Compound Statements

Compound statements:

<header>: <statement> <statement> ... <separating header>: <statement> <statement> ... ...

Suite

A suite is a sequence of statements

17

Page 114: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Compound Statements

Compound statements:

<header>: <statement> <statement> ... <separating header>: <statement> <statement> ... ...

Suite

A suite is a sequence of statements

To “execute” a suite means to execute its sequence of statements, in order

17

Page 115: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Compound Statements

Compound statements:

<header>: <statement> <statement> ... <separating header>: <statement> <statement> ... ...

Execution Rule for a sequence of statements:

• Execute the first statement

• Unless directed otherwise, execute the rest

Suite

A suite is a sequence of statements

To “execute” a suite means to execute its sequence of statements, in order

17

Page 116: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Conditional Statements

18

(Demo)

Page 117: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Conditional Statements

18

(Demo)

def absolute_value(x): """Return the absolute value of x.""" if x < 0: return -x elif x == 0: return 0 else: return x

Page 118: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Conditional Statements

1 statement, 3 clauses,3 headers, 3 suites

18

(Demo)

def absolute_value(x): """Return the absolute value of x.""" if x < 0: return -x elif x == 0: return 0 else: return x

Page 119: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Conditional Statements

1 statement, 3 clauses,3 headers, 3 suites

18

(Demo)

def absolute_value(x): """Return the absolute value of x.""" if x < 0: return -x elif x == 0: return 0 else: return x

Execution Rule for Conditional Statements:

Page 120: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Conditional Statements

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 remaining clauses.

18

(Demo)

def absolute_value(x): """Return the absolute value of x.""" if x < 0: return -x elif x == 0: return 0 else: return x

Execution Rule for Conditional Statements:

Page 121: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Conditional Statements

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 remaining clauses.

18

Syntax Tips:

(Demo)

def absolute_value(x): """Return the absolute value of x.""" if x < 0: return -x elif x == 0: return 0 else: return x

Execution Rule for Conditional Statements:

Page 122: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Conditional Statements

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 remaining clauses.

18

Syntax Tips:

1. Always starts with "if" clause.

2. Zero or more "elif" clauses.

3. Zero or one "else" clause,always at the end.

(Demo)

def absolute_value(x): """Return the absolute value of x.""" if x < 0: return -x elif x == 0: return 0 else: return x

Execution Rule for Conditional Statements:

Page 123: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Boolean Contexts

19

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

Page 124: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

def absolute_value(x): """Return the absolute value of x.""" if x < 0: return -x elif x == 0: return 0 else: return x

Boolean Contexts

George Boole

20

Page 125: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

def absolute_value(x): """Return the absolute value of x.""" if x < 0: return -x elif x == 0: return 0 else: return x

Boolean Contexts

George Boole

20

Two boolean contextsTwo boolean contexts

Page 126: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

def absolute_value(x): """Return the absolute value of x.""" if x < 0: return -x elif x == 0: return 0 else: return x

Boolean Contexts

False values in Python: False, 0, '', None

George Boole

20

Two boolean contextsTwo boolean contexts

Page 127: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

def absolute_value(x): """Return the absolute value of x.""" if x < 0: return -x elif x == 0: return 0 else: return x

Boolean Contexts

False values in Python: False, 0, '', None (more to come)

George Boole

20

Two boolean contextsTwo boolean contexts

Page 128: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

def absolute_value(x): """Return the absolute value of x.""" if x < 0: return -x elif x == 0: return 0 else: return x

Boolean Contexts

False values in Python: False, 0, '', None

True values in Python: Anything else (True)

(more to come)

George Boole

20

Two boolean contextsTwo boolean contexts

Page 129: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

def absolute_value(x): """Return the absolute value of x.""" if x < 0: return -x elif x == 0: return 0 else: return x

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!

20

Two boolean contextsTwo boolean contexts

Reading: http://composingprograms.com/pages/15-control.html#conditional-statements

Page 130: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

Iteration

Page 131: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

While Statements

22

(Demo)

Page 132: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

While Statements

22

(Demo)

Page 133: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

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

(Demo)

Execution Rule for While Statements:

Page 134: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

George Boole

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

(Demo)

Execution Rule for While Statements:

Page 135: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

George Boole

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

(Demo)

Execution Rule for While Statements:

Page 136: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

George Boole

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

(Demo)

Execution Rule for While Statements:

Page 137: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

George Boole

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

(Demo)

Execution Rule for While Statements:

Page 138: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

George Boole

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

(Demo)

Execution Rule for While Statements:

Page 139: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

George Boole

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

(Demo)

Execution Rule for While Statements:

Page 140: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

George Boole

While Statements

1. Evaluate the header’s expression.

2. If it is a true value, execute the (whole) suite, then return to step 1.

1

22

(Demo)

Execution Rule for While Statements:

Page 141: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

George Boole

While Statements

1. Evaluate the header’s expression.

2. If it is a true value, execute the (whole) suite, then return to step 1.

1

22

(Demo)

Execution Rule for While Statements:

Page 142: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

George Boole

While Statements

1. Evaluate the header’s expression.

2. If it is a true value, execute the (whole) suite, then return to step 1.

11

22

(Demo)

Execution Rule for While Statements:

Page 143: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

George Boole

While Statements

1. Evaluate the header’s expression.

2. If it is a true value, execute the (whole) suite, then return to step 1.

11

22

(Demo)

Execution Rule for While Statements:

Page 144: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

George Boole

While Statements

1. Evaluate the header’s expression.

2. If it is a true value, execute the (whole) suite, then return to step 1.

11

22

(Demo)

Execution Rule for While Statements:

Page 145: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

George Boole

While Statements

1. Evaluate the header’s expression.

2. If it is a true value, execute the (whole) suite, then return to step 1.

1 21

22

(Demo)

Execution Rule for While Statements:

Page 146: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

George Boole

While Statements

1. Evaluate the header’s expression.

2. If it is a true value, execute the (whole) suite, then return to step 1.

1 21

22

(Demo)

Execution Rule for While Statements:

Page 147: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

George Boole

While Statements

1. Evaluate the header’s expression.

2. If it is a true value, execute the (whole) suite, then return to step 1.

1 21 3

22

(Demo)

Execution Rule for While Statements:

Page 148: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

George Boole

While Statements

1. Evaluate the header’s expression.

2. If it is a true value, execute the (whole) suite, then return to step 1.

1 21 3

22

(Demo)

Execution Rule for While Statements:

Page 149: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

George Boole

While Statements

1. Evaluate the header’s expression.

2. If it is a true value, execute the (whole) suite, then return to step 1.

1 21 3

22

(Demo)

Execution Rule for While Statements:

Page 150: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

George Boole

While Statements

1. Evaluate the header’s expression.

2. If it is a true value, execute the (whole) suite, then return to step 1.

1 2 31 3

22

(Demo)

Execution Rule for While Statements:

Page 151: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

George Boole

While Statements

1. Evaluate the header’s expression.

2. If it is a true value, execute the (whole) suite, then return to step 1.

1 2 31 3

22

(Demo)

Execution Rule for While Statements:

Page 152: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

George Boole

While Statements

1. Evaluate the header’s expression.

2. If it is a true value, execute the (whole) suite, then return to step 1.

1 2 31 3 6

22

(Demo)

Execution Rule for While Statements:

Page 153: 61A Lecture 3 - University of California, BerkeleyNone Indicates that Nothing is Returned The special value None represents nothing in PythonA function that does not explicitly return

George Boole

While Statements

1. Evaluate the header’s expression.

2. If it is a true value, execute the (whole) suite, then return to step 1.

1 2 31 3 6

22

(Demo)

Execution Rule for While Statements: