Control Announcements Print and None (Demo) None Indicates that - - PowerPoint PPT Presentation

control announcements print and none
SMART_READER_LITE
LIVE PREVIEW

Control Announcements Print and None (Demo) None Indicates that - - PowerPoint PPT Presentation

Control Announcements Print and None (Demo) None Indicates that Nothing is Returned 4 None Indicates that Nothing is Returned The special value None represents nothing in Python 4 None Indicates that Nothing is Returned The special value


slide-1
SLIDE 1

Control

slide-2
SLIDE 2

Announcements

slide-3
SLIDE 3

Print and None

(Demo)

slide-4
SLIDE 4

None Indicates that Nothing is Returned

4

slide-5
SLIDE 5

None Indicates that Nothing is Returned

The special value None represents nothing in Python

4

slide-6
SLIDE 6

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

slide-7
SLIDE 7

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

slide-8
SLIDE 8

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

slide-9
SLIDE 9

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

slide-10
SLIDE 10

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

slide-11
SLIDE 11

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

slide-12
SLIDE 12

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

slide-13
SLIDE 13

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

slide-14
SLIDE 14

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

slide-15
SLIDE 15

Pure Functions & Non-Pure Functions

Pure Functions just return values Non-Pure Functions have side effects

5

slide-16
SLIDE 16

abs

Pure Functions & Non-Pure Functions

Pure Functions just return values Non-Pure Functions have side effects

5

slide-17
SLIDE 17

abs

Pure Functions & Non-Pure Functions

  • 2

Pure Functions just return values Non-Pure Functions have side effects

5

slide-18
SLIDE 18

abs

Pure Functions & Non-Pure Functions

  • 2

2 Pure Functions just return values Non-Pure Functions have side effects

5

slide-19
SLIDE 19

abs

Pure Functions & Non-Pure Functions

  • 2

2 Pure Functions just return values Non-Pure Functions have side effects Argument

5

slide-20
SLIDE 20

abs

Pure Functions & Non-Pure Functions

  • 2

2 Pure Functions just return values Non-Pure Functions have side effects Argument Return value

5

slide-21
SLIDE 21

abs

Pure Functions & Non-Pure Functions

  • 2

2 pow Pure Functions just return values Non-Pure Functions have side effects Argument Return value

5

slide-22
SLIDE 22

abs

Pure Functions & Non-Pure Functions

  • 2

2 2, 100 pow Pure Functions just return values Non-Pure Functions have side effects Argument Return value

5

slide-23
SLIDE 23

abs

Pure Functions & Non-Pure Functions

  • 2

2 2, 100 pow Pure Functions just return values Non-Pure Functions have side effects Argument Return value

5

2 Arguments

slide-24
SLIDE 24

abs

Pure Functions & Non-Pure Functions

  • 2

2 2, 100 1267650600228229401496703205376 pow Pure Functions just return values Non-Pure Functions have side effects Argument Return value

5

2 Arguments

slide-25
SLIDE 25

abs

Pure Functions & Non-Pure Functions

  • 2

2 print 2, 100 1267650600228229401496703205376 pow Pure Functions just return values Non-Pure Functions have side effects Argument Return value

5

2 Arguments

slide-26
SLIDE 26

abs

Pure Functions & Non-Pure Functions

  • 2

2

  • 2

print 2, 100 1267650600228229401496703205376 pow Pure Functions just return values Non-Pure Functions have side effects Argument Return value

5

2 Arguments

slide-27
SLIDE 27

abs

Pure Functions & Non-Pure Functions

  • 2

2

  • 2

None print 2, 100 1267650600228229401496703205376 pow Pure Functions just return values Non-Pure Functions have side effects Argument Return value

5

2 Arguments

slide-28
SLIDE 28

abs

Pure Functions & Non-Pure Functions

  • 2

2

  • 2

None print Python displays the output “-2” 2, 100 1267650600228229401496703205376 pow Pure Functions just return values Non-Pure Functions have side effects Argument Return value

5

2 Arguments

slide-29
SLIDE 29

abs

Pure Functions & Non-Pure Functions

  • 2

2

  • 2

None print Python displays the output “-2” 2, 100 1267650600228229401496703205376 pow Pure Functions just return values Non-Pure Functions have side effects Argument Return value Returns None!

5

2 Arguments

slide-30
SLIDE 30

abs

Pure Functions & Non-Pure Functions

  • 2

2

  • 2

None print Python displays the output “-2” 2, 100 1267650600228229401496703205376 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

slide-31
SLIDE 31

Nested Expressions with Print

6

slide-32
SLIDE 32

Nested Expressions with Print

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

6

slide-33
SLIDE 33

Nested Expressions with Print

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

6

slide-34
SLIDE 34

Nested Expressions with Print

print(print(1), print(2)) func print(...)

6

slide-35
SLIDE 35

Nested Expressions with Print

print(print(1), print(2)) func print(...) print(1) func print(...) 1

6

slide-36
SLIDE 36

Nested Expressions with Print

print(print(1), print(2)) func print(...) print(...): 1 None display “1” print(1) func print(...) 1

6

slide-37
SLIDE 37

Nested Expressions with Print

print(print(1), print(2)) func print(...) print(...): 1 None display “1” print(1) func print(...) 1 None

6

slide-38
SLIDE 38

Nested Expressions with Print

print(print(1), print(2)) func print(...) print(...): 1 None display “1” print(1) func print(...) 1 None print(2) 2

6

func print(...)

slide-39
SLIDE 39

Nested Expressions with Print

print(print(1), print(2)) func print(...) print(...): 1 None display “1” print(...): 2 None display “2” print(1) func print(...) 1 None print(2) 2

6

func print(...)

slide-40
SLIDE 40

Nested Expressions with Print

print(print(1), print(2)) func print(...) print(...): 1 None display “1” print(...): 2 None display “2” print(1) func print(...) 1 None print(2) 2 None

6

func print(...)

slide-41
SLIDE 41

Nested Expressions with Print

print(print(1), print(2)) func print(...) print(...): 1 None display “1” print(...): 2 None display “2” print(...): None, None None display “None None” print(1) func print(...) 1 None print(2) 2 None

6

func print(...)

slide-42
SLIDE 42

Nested Expressions with Print

None print(print(1), print(2)) func print(...) print(...): 1 None display “1” print(...): 2 None display “2” print(...): None, None None display “None None” print(1) func print(...) 1 None print(2) 2 None

6

func print(...)

slide-43
SLIDE 43

Nested Expressions with Print

None print(print(1), print(2)) func print(...) print(...): 1 None display “1” print(...): 2 None display “2” print(...): None, None None display “None None” print(1) func print(...) 1 None print(2) 2 None

6

func print(...)

slide-44
SLIDE 44

Nested Expressions with Print

None print(print(1), print(2)) func print(...) print(...): 1 None display “1” print(...): 2 None display “2” print(...): None, None None display “None None” print(1) func print(...) 1 None print(2) 2 None

6

Does not get displayed func print(...)

slide-45
SLIDE 45

Multiple Environments

slide-46
SLIDE 46

Life Cycle of a User-Defined Function

Def statement: Call expression: Calling/Applying: What happens?

8

slide-47
SLIDE 47

Life Cycle of a User-Defined Function

Def statement: Call expression: square( x ): return mul(x, x) >>> def Calling/Applying: What happens?

8

slide-48
SLIDE 48

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

slide-49
SLIDE 49

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

slide-50
SLIDE 50

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

slide-51
SLIDE 51

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

slide-52
SLIDE 52

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

slide-53
SLIDE 53

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

slide-54
SLIDE 54

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

slide-55
SLIDE 55

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

slide-56
SLIDE 56

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

slide-57
SLIDE 57

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?

  • perator: square

function: func square(x) Name

8

slide-58
SLIDE 58

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

  • perand: 2+2

argument: 4 What happens?

  • perator: square

function: func square(x) Name

8

slide-59
SLIDE 59

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

  • perand: 2+2

argument: 4 Operator & operands evaluated What happens?

  • perator: square

function: func square(x) Name

8

slide-60
SLIDE 60

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

  • perand: 2+2

argument: 4 Operator & operands evaluated Function (value of operator) called on arguments (values of operands) What happens?

  • perator: square

function: func square(x) Name

8

slide-61
SLIDE 61

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

  • perand: 2+2

argument: 4 Operator & operands evaluated Function (value of operator) called on arguments (values of operands) What happens?

  • perator: square

function: func square(x) Name

8

slide-62
SLIDE 62

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

  • perand: 2+2

argument: 4 Operator & operands evaluated Function (value of operator) called on arguments (values of operands) What happens?

  • perator: square

function: func square(x) Signature Name

8

slide-63
SLIDE 63

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

  • perand: 2+2

argument: 4 Operator & operands evaluated Function (value of operator) called on arguments (values of operands) What happens?

  • perator: square

function: func square(x) Signature 4 Name

8

slide-64
SLIDE 64

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

  • perand: 2+2

argument: 4 Operator & operands evaluated Function (value of operator) called on arguments (values of operands) What happens?

  • perator: square

function: func square(x) Signature 4 16 Name

8

slide-65
SLIDE 65

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

  • perand: 2+2

argument: 4 Operator & operands evaluated Function (value of operator) called on arguments (values of operands) What happens?

  • perator: square

function: func square(x) Signature 4 16 Argument Name

8

slide-66
SLIDE 66

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

  • perand: 2+2

argument: 4 Operator & operands evaluated Function (value of operator) called on arguments (values of operands) What happens?

  • perator: square

function: func square(x) Signature 4 16 Argument Return value Name

8

slide-67
SLIDE 67

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

  • perand: 2+2

argument: 4 Operator & operands evaluated Function (value of operator) called on arguments (values of operands) What happens?

  • perator: square

function: func square(x) Signature 4 16 A new frame is created! Argument Return value Name

8

slide-68
SLIDE 68

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

  • perand: 2+2

argument: 4 Operator & operands evaluated Function (value of operator) called on arguments (values of operands) What happens?

  • perator: square

function: func square(x) Signature 4 16 A new frame is created! Parameters bound to arguments Argument Return value Name

8

slide-69
SLIDE 69

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

  • perand: 2+2

argument: 4 Operator & operands evaluated Function (value of operator) called on arguments (values of operands) What happens?

  • perator: 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

slide-70
SLIDE 70

Multiple Environments in One Diagram!

9

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-71
SLIDE 71

Multiple Environments in One Diagram!

9

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-72
SLIDE 72

Multiple Environments in One Diagram!

square(square(3))

9

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-73
SLIDE 73

Multiple Environments in One Diagram!

square(square(3))

9

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-74
SLIDE 74

Multiple Environments in One Diagram!

square(square(3)) func square(x)

9

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-75
SLIDE 75

Multiple Environments in One Diagram!

square(square(3)) square(3) func square(x)

9

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-76
SLIDE 76

Multiple Environments in One Diagram!

square(square(3)) square(3) func square(x)

9

func square(x)

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-77
SLIDE 77

Multiple Environments in One Diagram!

square(square(3)) square(3) 3 func square(x)

9

func square(x)

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-78
SLIDE 78

Multiple Environments in One Diagram!

square(square(3)) square(3) 3 func square(x)

9

func square(x)

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-79
SLIDE 79

Multiple Environments in One Diagram!

square(square(3)) square(3) 3 func square(x)

10

func square(x)

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-80
SLIDE 80

Multiple Environments in One Diagram!

square(square(3)) square(3) 3 func square(x)

10

func square(x)

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-81
SLIDE 81

Multiple Environments in One Diagram!

square(square(3)) square(3) 9 3 func square(x)

10

func square(x)

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-82
SLIDE 82

Multiple Environments in One Diagram!

square(square(3)) square(3) 9 3 func square(x)

10

func square(x)

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-83
SLIDE 83

Multiple Environments in One Diagram!

11

square(square(3)) square(3) 9 3 func square(x) func square(x) 81

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-84
SLIDE 84

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

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-85
SLIDE 85

Multiple Environments in One Diagram!

An environment is a sequence of frames.

  • The global frame alone
  • A local, then the global frame

11

square(square(3)) square(3) 9 3 func square(x) func square(x) 81

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-86
SLIDE 86

Multiple Environments in One Diagram!

An environment is a sequence of frames.

1

  • The global frame alone
  • A local, then the global frame

11

square(square(3)) square(3) 9 3 func square(x) func square(x) 81

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-87
SLIDE 87

Multiple Environments in One Diagram!

An environment is a sequence of frames.

1 2 1

  • The global frame alone
  • A local, then the global frame

11

square(square(3)) square(3) 9 3 func square(x) func square(x) 81

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-88
SLIDE 88

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 frame

11

square(square(3)) square(3) 9 3 func square(x) func square(x) 81

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-89
SLIDE 89

Names Have No Meaning Without Environments

An environment is a sequence of frames.

  • The global frame alone
  • A local, then the global frame

12

1 2 1 2 1

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-90
SLIDE 90

Names Have No Meaning Without Environments

An environment is a sequence of frames.

  • The global frame alone
  • A local, then the global frame

12

Every expression is evaluated in the context

  • f an environment.

1 2 1 2 1

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-91
SLIDE 91

Names Have No Meaning Without Environments

An environment is a sequence of frames.

  • The global frame alone
  • A local, then the global frame

12

Every expression is evaluated in the context

  • f 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

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-92
SLIDE 92

Names Have No Meaning Without Environments

An environment is a sequence of frames.

  • The global frame alone
  • A local, then the global frame

12

Every expression is evaluated in the context

  • f 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

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-93
SLIDE 93

Names Have No Meaning Without Environments

An environment is a sequence of frames.

  • The global frame alone
  • A local, then the global frame

12

Every expression is evaluated in the context

  • f 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

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-94
SLIDE 94

Names Have No Meaning Without Environments

An environment is a sequence of frames.

  • The global frame alone
  • A local, then the global frame

12

Every expression is evaluated in the context

  • f 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

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28square%283%29%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-95
SLIDE 95

Names Have Different Meanings in Different Environments

13

Every expression is evaluated in the context

  • f 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.

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28square%29%3A%0A%20%20%20%20return%20mul%28square,%20square%29%0Asquare%284%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-96
SLIDE 96

Names Have Different Meanings in Different Environments

13

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

  • f 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.

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28square%29%3A%0A%20%20%20%20return%20mul%28square,%20square%29%0Asquare%284%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-97
SLIDE 97

Names Have Different Meanings in Different Environments

13

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

  • f 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.

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28square%29%3A%0A%20%20%20%20return%20mul%28square,%20square%29%0Asquare%284%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-98
SLIDE 98

Names Have Different Meanings in Different Environments

13

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

  • f 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.

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28square%29%3A%0A%20%20%20%20return%20mul%28square,%20square%29%0Asquare%284%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-99
SLIDE 99

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 Every expression is evaluated in the context

  • f 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.

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28square%29%3A%0A%20%20%20%20return%20mul%28square,%20square%29%0Asquare%284%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-100
SLIDE 100

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 Every expression is evaluated in the context

  • f 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.

http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28square%29%3A%0A%20%20%20%20return%20mul%28square,%20square%29%0Asquare%284%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-101
SLIDE 101

Miscellaneous Python Features

Division Multiple Return Values Source Files Doctests Default Arguments (Demo)

slide-102
SLIDE 102

Conditional Statements

slide-103
SLIDE 103

Statements

16

A statement is executed by the interpreter to perform an action

slide-104
SLIDE 104

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

Statements

16

A statement is executed by the interpreter to perform an action

slide-105
SLIDE 105

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

Statements

Statement

16

A statement is executed by the interpreter to perform an action

slide-106
SLIDE 106

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

Statements

Statement Clause

16

A statement is executed by the interpreter to perform an action

slide-107
SLIDE 107

<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

slide-108
SLIDE 108

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

Statements

Statement Suite Clause The first header determines a statement’s type

16

A statement is executed by the interpreter to perform an action

slide-109
SLIDE 109

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

Statements

Statement Suite Clause The 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

slide-110
SLIDE 110

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

Statements

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

16

A statement is executed by the interpreter to perform an action

slide-111
SLIDE 111

Compound Statements

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

17

slide-112
SLIDE 112

Compound Statements

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

17

slide-113
SLIDE 113

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

slide-114
SLIDE 114

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

slide-115
SLIDE 115

Conditional Statements

18

slide-116
SLIDE 116

Conditional Statements

18

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

slide-117
SLIDE 117

Conditional Statements

1 statement, 3 clauses, 3 headers, 3 suites

18

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

slide-118
SLIDE 118

Conditional Statements

1 statement, 3 clauses, 3 headers, 3 suites

18

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:

slide-119
SLIDE 119

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

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:

slide-120
SLIDE 120

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: 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:

slide-121
SLIDE 121

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

slide-122
SLIDE 122

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

slide-123
SLIDE 123

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

slide-124
SLIDE 124

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 contexts Two boolean contexts

slide-125
SLIDE 125

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 contexts Two boolean contexts

slide-126
SLIDE 126

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 contexts Two boolean contexts

slide-127
SLIDE 127

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 contexts Two boolean contexts

slide-128
SLIDE 128

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 contexts Two boolean contexts

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

slide-129
SLIDE 129

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 contexts Two boolean contexts

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

(Demo)

slide-130
SLIDE 130

Iteration

slide-131
SLIDE 131

While Statements

22

(Demo)

slide-132
SLIDE 132

While Statements

22

(Demo)

slide-133
SLIDE 133

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:

slide-134
SLIDE 134

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:

slide-135
SLIDE 135

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:

slide-136
SLIDE 136

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:

slide-137
SLIDE 137

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:

slide-138
SLIDE 138

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:

slide-139
SLIDE 139

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:

slide-140
SLIDE 140

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:

slide-141
SLIDE 141

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:

slide-142
SLIDE 142

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 1

22

(Demo) Execution Rule for While Statements:

slide-143
SLIDE 143

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 1

22

(Demo) Execution Rule for While Statements:

slide-144
SLIDE 144

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 1

22

(Demo) Execution Rule for While Statements:

slide-145
SLIDE 145

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 1

22

(Demo) Execution Rule for While Statements:

slide-146
SLIDE 146

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 1

22

(Demo) Execution Rule for While Statements:

slide-147
SLIDE 147

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 1 3

22

(Demo) Execution Rule for While Statements:

slide-148
SLIDE 148

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 1 3

22

(Demo) Execution Rule for While Statements:

slide-149
SLIDE 149

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 1 3

22

(Demo) Execution Rule for While Statements:

slide-150
SLIDE 150

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 3 1 3

22

(Demo) Execution Rule for While Statements:

slide-151
SLIDE 151

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 3 1 3

22

(Demo) Execution Rule for While Statements:

slide-152
SLIDE 152

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 3 1 3 6

22

(Demo) Execution Rule for While Statements:

slide-153
SLIDE 153

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 3 1 3 6

22

(Demo) Execution Rule for While Statements:

slide-154
SLIDE 154

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 3 1 3 6

22

(Demo) Execution Rule for While Statements: (Demo)

slide-155
SLIDE 155

Example: Prime Factorization

slide-156
SLIDE 156

Prime Factorization

24

slide-157
SLIDE 157

Prime Factorization

Each positive integer n has a set of prime factors: primes whose product is n

24

slide-158
SLIDE 158

Prime Factorization

Each positive integer n has a set of prime factors: primes whose product is n ... 8 = 2 * 2 * 2 9 = 3 * 3 10 = 2 * 5 11 = 11 12 = 2 * 2 * 3 ...

24

slide-159
SLIDE 159

Prime Factorization

Each positive integer n has a set of prime factors: primes whose product is n ... 8 = 2 * 2 * 2 9 = 3 * 3 10 = 2 * 5 11 = 11 12 = 2 * 2 * 3 ... One approach: Find the smallest prime factor of n, then divide by it

24

slide-160
SLIDE 160

Prime Factorization

Each positive integer n has a set of prime factors: primes whose product is n ... 8 = 2 * 2 * 2 9 = 3 * 3 10 = 2 * 5 11 = 11 12 = 2 * 2 * 3 ... One approach: Find the smallest prime factor of n, then divide by it

24

858

slide-161
SLIDE 161

Prime Factorization

Each positive integer n has a set of prime factors: primes whose product is n ... 8 = 2 * 2 * 2 9 = 3 * 3 10 = 2 * 5 11 = 11 12 = 2 * 2 * 3 ... One approach: Find the smallest prime factor of n, then divide by it

24

858 = 2 * 429

slide-162
SLIDE 162

Prime Factorization

Each positive integer n has a set of prime factors: primes whose product is n ... 8 = 2 * 2 * 2 9 = 3 * 3 10 = 2 * 5 11 = 11 12 = 2 * 2 * 3 ... One approach: Find the smallest prime factor of n, then divide by it

24

858 = 2 * 429 = 2 * 3 * 143

slide-163
SLIDE 163

Prime Factorization

Each positive integer n has a set of prime factors: primes whose product is n ... 8 = 2 * 2 * 2 9 = 3 * 3 10 = 2 * 5 11 = 11 12 = 2 * 2 * 3 ... One approach: Find the smallest prime factor of n, then divide by it

24

858 = 2 * 429 = 2 * 3 * 143 = 2 * 3 * 11 * 13

slide-164
SLIDE 164

Prime Factorization

Each positive integer n has a set of prime factors: primes whose product is n ... 8 = 2 * 2 * 2 9 = 3 * 3 10 = 2 * 5 11 = 11 12 = 2 * 2 * 3 ... One approach: Find the smallest prime factor of n, then divide by it

24

858 = 2 * 429 = 2 * 3 * 143 = 2 * 3 * 11 * 13 (Demo)