Iteration Announcements Return Return Statements 4 Return - - PowerPoint PPT Presentation

iteration announcements return return statements
SMART_READER_LITE
LIVE PREVIEW

Iteration Announcements Return Return Statements 4 Return - - PowerPoint PPT Presentation

Iteration Announcements Return Return Statements 4 Return Statements A return statement completes the evaluation of a call expression and provides its value: 4 Return Statements A return statement completes the evaluation of a call


slide-1
SLIDE 1

Iteration

slide-2
SLIDE 2

Announcements

slide-3
SLIDE 3

Return

slide-4
SLIDE 4

Return Statements

4

slide-5
SLIDE 5

Return Statements

A return statement completes the evaluation of a call expression and provides its value:

4

slide-6
SLIDE 6

Return Statements

A return statement completes the evaluation of a call expression and provides its value:

4

f(x) for user-defined function f: switch to a new environment; execute f's body

slide-7
SLIDE 7

Return Statements

A return statement completes the evaluation of a call expression and provides its value:

4

f(x) for user-defined function f: switch to a new environment; execute f's body return statement within f: switch back to the previous environment; f(x) now has a value

slide-8
SLIDE 8

Return Statements

A return statement completes the evaluation of a call expression and provides its value:

4

f(x) for user-defined function f: switch to a new environment; execute f's body return statement within f: switch back to the previous environment; f(x) now has a value Only one return statement is ever executed while executing the body of a function

slide-9
SLIDE 9

Return Statements

A return statement completes the evaluation of a call expression and provides its value:

4

f(x) for user-defined function f: switch to a new environment; execute f's body return statement within f: switch back to the previous environment; f(x) now has a value Only one return statement is ever executed while executing the body of a function def end(n, d): """Print the final digits of N in reverse order until D is found. >>> end(34567, 5) 7 6 5 """

slide-10
SLIDE 10

Return Statements

A return statement completes the evaluation of a call expression and provides its value:

4

f(x) for user-defined function f: switch to a new environment; execute f's body return statement within f: switch back to the previous environment; f(x) now has a value Only one return statement is ever executed while executing the body of a function def end(n, d): """Print the final digits of N in reverse order until D is found. >>> end(34567, 5) 7 6 5 """ while n > 0: last, n = n % 10, n // 10 print(last)

slide-11
SLIDE 11

Return Statements

A return statement completes the evaluation of a call expression and provides its value:

4

f(x) for user-defined function f: switch to a new environment; execute f's body return statement within f: switch back to the previous environment; f(x) now has a value Only one return statement is ever executed while executing the body of a function def end(n, d): """Print the final digits of N in reverse order until D is found. >>> end(34567, 5) 7 6 5 """ while n > 0: last, n = n % 10, n // 10 print(last) if d == last: return None

slide-12
SLIDE 12

Return Statements

A return statement completes the evaluation of a call expression and provides its value:

4

f(x) for user-defined function f: switch to a new environment; execute f's body return statement within f: switch back to the previous environment; f(x) now has a value Only one return statement is ever executed while executing the body of a function def end(n, d): """Print the final digits of N in reverse order until D is found. >>> end(34567, 5) 7 6 5 """ while n > 0: last, n = n % 10, n // 10 print(last) if d == last: return None (Demo)

slide-13
SLIDE 13

Self-Reference

(Demo)

slide-14
SLIDE 14

Returning a Function Using Its Own Name

6

http://pythontutor.com/composingprograms.html#code=def%20print_all%28k%29%3A%0A%20%20%20%20print%28k%29%0A%20%20%20%20return%20print_all%0A%20%20%20%20%0Aprint_all%281%29%283%29%285%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D http://pythontutor.com/composingprograms.html#code=def%20print_sums%28n%29%3A%0A%20%20%20%20print%28n%29%0A%20%20%20%20def%20next_sum%28k%29%3A%0A%20%20%20%20%20%20%20%20return%20print_sums%28n%2Bk%29%0A%20%20%20%20return%20next_sum%0A%0Aprint_sums%281%29%283%29%285%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D

slide-15
SLIDE 15

Control

slide-16
SLIDE 16

If Statements and Call Expressions

Let's try to write a function that does the same thing as an if statement.

8

slide-17
SLIDE 17

If Statements and Call Expressions

Let's try to write a function that does the same thing as an if statement.

8

if __________: _________ else: _________

slide-18
SLIDE 18

If Statements and Call Expressions

Let's try to write a function that does the same thing as an if statement.

8

Execution Rule for Conditional Statements: if __________: _________ else: _________

slide-19
SLIDE 19

If Statements and Call Expressions

Let's try to write a function that does the same thing as an if statement.

8

Each clause is considered in order. Execution Rule for Conditional Statements: if __________: _________ else: _________

slide-20
SLIDE 20

If Statements and Call Expressions

Let's try to write a function that does the same thing as an if statement.

8

Each clause is considered in order.

  • 1. Evaluate the header's expression (if present).

Execution Rule for Conditional Statements: if __________: _________ else: _________

slide-21
SLIDE 21

If Statements and Call Expressions

Let's try to write a function that does the same thing as an if statement.

8

Each clause is considered in order.

  • 1. Evaluate the header's expression (if present).
  • 2. If it is a true value (or an else header), 


execute the suite & skip the remaining clauses. Execution Rule for Conditional Statements: if __________: _________ else: _________

slide-22
SLIDE 22

If Statements and Call Expressions

Let's try to write a function that does the same thing as an if statement.

8

Each clause is considered in order.

  • 1. Evaluate the header's expression (if present).
  • 2. If it is a true value (or an else header), 


execute the suite & skip the remaining clauses. Execution Rule for Conditional Statements: if __________: _________ else: _________ "if" clause

slide-23
SLIDE 23

If Statements and Call Expressions

Let's try to write a function that does the same thing as an if statement.

8

Each clause is considered in order.

  • 1. Evaluate the header's expression (if present).
  • 2. If it is a true value (or an else header), 


execute the suite & skip the remaining clauses. Execution Rule for Conditional Statements: if __________: _________ else: _________ "if" clause "else" clause

slide-24
SLIDE 24

If Statements and Call Expressions

Let's try to write a function that does the same thing as an if statement.

8

Each clause is considered in order.

  • 1. Evaluate the header's expression (if present).
  • 2. If it is a true value (or an else header), 


execute the suite & skip the remaining clauses. Execution Rule for Conditional Statements: if __________: _________ else: _________ "if" clause "else" clause "if" header expression

slide-25
SLIDE 25

If Statements and Call Expressions

Let's try to write a function that does the same thing as an if statement.

8

Each clause is considered in order.

  • 1. Evaluate the header's expression (if present).
  • 2. If it is a true value (or an else header), 


execute the suite & skip the remaining clauses. Execution Rule for Conditional Statements: if __________: _________ else: _________ "if" clause "else" clause "if" header expression "if" suite

slide-26
SLIDE 26

If Statements and Call Expressions

Let's try to write a function that does the same thing as an if statement.

8

Each clause is considered in order.

  • 1. Evaluate the header's expression (if present).
  • 2. If it is a true value (or an else header), 


execute the suite & skip the remaining clauses. Execution Rule for Conditional Statements: if __________: _________ else: _________ "if" clause "else" clause "if" header expression "if" suite "else" suite

slide-27
SLIDE 27

If Statements and Call Expressions

Let's try to write a function that does the same thing as an if statement.

8

Each clause is considered in order.

  • 1. Evaluate the header's expression (if present).
  • 2. If it is a true value (or an else header), 


execute the suite & skip the remaining clauses. Execution Rule for Conditional Statements: if __________: _________ else: _________ "if" clause "else" clause "if" header expression "if" suite "else" suite

slide-28
SLIDE 28

If Statements and Call Expressions

Let's try to write a function that does the same thing as an if statement.

8

Each clause is considered in order.

  • 1. Evaluate the header's expression (if present).
  • 2. If it is a true value (or an else header), 


execute the suite & skip the remaining clauses. Execution Rule for Conditional Statements: if __________: _________ else: _________ if_(________, ________, ________) "if" clause "else" clause "if" header expression "if" suite "else" suite

slide-29
SLIDE 29

If Statements and Call Expressions

Let's try to write a function that does the same thing as an if statement.

8

Each clause is considered in order.

  • 1. Evaluate the header's expression (if present).
  • 2. If it is a true value (or an else header), 


execute the suite & skip the remaining clauses. Execution Rule for Conditional Statements: if __________: _________ else: _________ if_(________, ________, ________) "if" clause "else" clause "if" header expression "if" suite "else" suite "if" header expression

slide-30
SLIDE 30

If Statements and Call Expressions

Let's try to write a function that does the same thing as an if statement.

8

Each clause is considered in order.

  • 1. Evaluate the header's expression (if present).
  • 2. If it is a true value (or an else header), 


execute the suite & skip the remaining clauses. Execution Rule for Conditional Statements: if __________: _________ else: _________ if_(________, ________, ________) "if" clause "else" clause "if" header expression "if" suite "else" suite "if" header expression "if" suite

slide-31
SLIDE 31

If Statements and Call Expressions

Let's try to write a function that does the same thing as an if statement.

8

Each clause is considered in order.

  • 1. Evaluate the header's expression (if present).
  • 2. If it is a true value (or an else header), 


execute the suite & skip the remaining clauses. Execution Rule for Conditional Statements: if __________: _________ else: _________ if_(________, ________, ________) "if" clause "else" clause "if" header expression "if" suite "else" suite "if" header expression "if" suite "else" suite

slide-32
SLIDE 32

If Statements and Call Expressions

Let's try to write a function that does the same thing as an if statement.

8

Each clause is considered in order.

  • 1. Evaluate the header's expression (if present).
  • 2. If it is a true value (or an else header), 


execute the suite & skip the remaining clauses. Execution Rule for Conditional Statements: if __________: _________ else: _________ if_(________, ________, ________) "if" clause "else" clause "if" header expression "if" suite "else" suite This function doesn't exist "if" header expression "if" suite "else" suite

slide-33
SLIDE 33

If Statements and Call Expressions

Let's try to write a function that does the same thing as an if statement.

8

Each clause is considered in order.

  • 1. Evaluate the header's expression (if present).
  • 2. If it is a true value (or an else header), 


execute the suite & skip the remaining clauses. Execution Rule for Conditional Statements: if __________: _________ else: _________ if_(________, ________, ________) "if" clause "else" clause "if" header expression "if" suite "else" suite This function doesn't exist def if_(c, t, f): if c: t else: f "if" header expression "if" suite "else" suite

slide-34
SLIDE 34

If Statements and Call Expressions

Let's try to write a function that does the same thing as an if statement.

8

Each clause is considered in order.

  • 1. Evaluate the header's expression (if present).
  • 2. If it is a true value (or an else header), 


execute the suite & skip the remaining clauses. Execution Rule for Conditional Statements: if __________: _________ else: _________ if_(________, ________, ________) "if" clause "else" clause "if" header expression "if" suite "else" suite This function doesn't exist def if_(c, t, f): if c: t else: f "if" header expression "if" suite "else" suite Evaluation Rule for Call Expressions:

slide-35
SLIDE 35

If Statements and Call Expressions

Let's try to write a function that does the same thing as an if statement.

8

Each clause is considered in order.

  • 1. Evaluate the header's expression (if present).
  • 2. If it is a true value (or an else header), 


execute the suite & skip the remaining clauses. Execution Rule for Conditional Statements: if __________: _________ else: _________ if_(________, ________, ________) "if" clause "else" clause "if" header expression "if" suite "else" suite This function doesn't exist def if_(c, t, f): if c: t else: f "if" header expression "if" suite "else" suite Evaluation Rule for Call Expressions:

  • 1. Evaluate the operator and then the
  • perand subexpressions
slide-36
SLIDE 36

If Statements and Call Expressions

Let's try to write a function that does the same thing as an if statement.

8

Each clause is considered in order.

  • 1. Evaluate the header's expression (if present).
  • 2. If it is a true value (or an else header), 


execute the suite & skip the remaining clauses. Execution Rule for Conditional Statements: if __________: _________ else: _________ if_(________, ________, ________) "if" clause "else" clause "if" header expression "if" suite "else" suite This function doesn't exist def if_(c, t, f): if c: t else: f "if" header expression "if" suite "else" suite Evaluation Rule for Call Expressions:

  • 1. Evaluate the operator and then the
  • perand subexpressions
  • 2. Apply the function that is the

value of the operator 
 to the arguments that are the values of the operands

slide-37
SLIDE 37

If Statements and Call Expressions

Let's try to write a function that does the same thing as an if statement.

8

Each clause is considered in order.

  • 1. Evaluate the header's expression (if present).
  • 2. If it is a true value (or an else header), 


execute the suite & skip the remaining clauses. Execution Rule for Conditional Statements: if __________: _________ else: _________ if_(________, ________, ________) "if" clause "else" clause "if" header expression "if" suite "else" suite This function doesn't exist def if_(c, t, f): if c: t else: f "if" header expression "if" suite "else" suite Evaluation Rule for Call Expressions:

  • 1. Evaluate the operator and then the
  • perand subexpressions
  • 2. Apply the function that is the

value of the operator 
 to the arguments that are the values of the operands (Demo)

slide-38
SLIDE 38

Control Expressions

slide-39
SLIDE 39

Logical Operators

10

slide-40
SLIDE 40

Logical Operators

To evaluate the expression <left> and <right>:

10

slide-41
SLIDE 41

Logical Operators

To evaluate the expression <left> and <right>:

  • 1. Evaluate the subexpression <left>.

10

slide-42
SLIDE 42

Logical Operators

To evaluate the expression <left> and <right>:

  • 1. Evaluate the subexpression <left>.
  • 2. If the result is a false value v, then the expression evaluates to v.

10

slide-43
SLIDE 43

Logical Operators

To evaluate the expression <left> and <right>:

  • 1. Evaluate the subexpression <left>.
  • 2. If the result is a false value v, then the expression evaluates to v.
  • 3. Otherwise, the expression evaluates to the value of the subexpression <right>.

10

slide-44
SLIDE 44

Logical Operators

To evaluate the expression <left> and <right>:

  • 1. Evaluate the subexpression <left>.
  • 2. If the result is a false value v, then the expression evaluates to v.
  • 3. Otherwise, the expression evaluates to the value of the subexpression <right>.

To evaluate the expression <left> or <right>:

10

slide-45
SLIDE 45

Logical Operators

To evaluate the expression <left> and <right>:

  • 1. Evaluate the subexpression <left>.
  • 2. If the result is a false value v, then the expression evaluates to v.
  • 3. Otherwise, the expression evaluates to the value of the subexpression <right>.

To evaluate the expression <left> or <right>:

  • 1. Evaluate the subexpression <left>.

10

slide-46
SLIDE 46

Logical Operators

To evaluate the expression <left> and <right>:

  • 1. Evaluate the subexpression <left>.
  • 2. If the result is a false value v, then the expression evaluates to v.
  • 3. Otherwise, the expression evaluates to the value of the subexpression <right>.

To evaluate the expression <left> or <right>:

  • 1. Evaluate the subexpression <left>.
  • 2. If the result is a true value v, then the expression evaluates to v.

10

slide-47
SLIDE 47

Logical Operators

To evaluate the expression <left> and <right>:

  • 1. Evaluate the subexpression <left>.
  • 2. If the result is a false value v, then the expression evaluates to v.
  • 3. Otherwise, the expression evaluates to the value of the subexpression <right>.

To evaluate the expression <left> or <right>:

  • 1. Evaluate the subexpression <left>.
  • 2. If the result is a true value v, then the expression evaluates to v.
  • 3. Otherwise, the expression evaluates to the value of the subexpression <right>.

10

slide-48
SLIDE 48

Logical Operators

To evaluate the expression <left> and <right>:

  • 1. Evaluate the subexpression <left>.
  • 2. If the result is a false value v, then the expression evaluates to v.
  • 3. Otherwise, the expression evaluates to the value of the subexpression <right>.

To evaluate the expression <left> or <right>:

  • 1. Evaluate the subexpression <left>.
  • 2. If the result is a true value v, then the expression evaluates to v.
  • 3. Otherwise, the expression evaluates to the value of the subexpression <right>.

10

(Demo)

slide-49
SLIDE 49

Conditional Expressions

11

slide-50
SLIDE 50

Conditional Expressions

A conditional expression has the form <consequent> if <predicate> else <alternative>

11

slide-51
SLIDE 51

Conditional Expressions

A conditional expression has the form <consequent> if <predicate> else <alternative> Evaluation rule:

11

slide-52
SLIDE 52

Conditional Expressions

A conditional expression has the form <consequent> if <predicate> else <alternative> Evaluation rule:

  • 1. Evaluate the <predicate> expression.

11

slide-53
SLIDE 53

Conditional Expressions

A conditional expression has the form <consequent> if <predicate> else <alternative> Evaluation rule:

  • 1. Evaluate the <predicate> expression.
  • 2. If it's a true value, the value of the whole expression is the value of the <consequent>.

11

slide-54
SLIDE 54

Conditional Expressions

A conditional expression has the form <consequent> if <predicate> else <alternative> Evaluation rule:

  • 1. Evaluate the <predicate> expression.
  • 2. If it's a true value, the value of the whole expression is the value of the <consequent>.
  • 3. Otherwise, the value of the whole expression is the value of the <alternative>.

11

slide-55
SLIDE 55

Conditional Expressions

A conditional expression has the form <consequent> if <predicate> else <alternative> Evaluation rule:

  • 1. Evaluate the <predicate> expression.
  • 2. If it's a true value, the value of the whole expression is the value of the <consequent>.
  • 3. Otherwise, the value of the whole expression is the value of the <alternative>.

11

>>> x = 0 >>> abs(1/x if x != 0 else 0)