You are on page 1of 3

61A Lecture 3 Announcements

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

Print and None >>> def does_not_return_square(x):


... x * x
No return
...
>>> does_not_return_square(4) None value is not displayed
(Demo)
The name sixteen >>> sixteen = does_not_return_square(4)
is now bound to
the value None >>> sixteen + 4
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

Pure Functions & Non-Pure Functions Nested Expressions with Print

None, None print(...):


Return value None Does not get
Pure Functions
just return values -2 abs displayed
2
Argument display “None None”
None
2, 100 pow print(print(1), print(2))
1267650600228229401496703205376
2 Arguments func print(...) None None

Returns None! print(1) print(2)


Non-Pure Functions
have side effects -2 print
None func print(...) 1 func print(...) 2
A side effect isn't a
value; it's anything 2 print(...):
1 print(...):
Python displays the output “-2” that happens as a None None
consequence of
calling a function
display “1” display “2”
(Demo)
5 6

Life Cycle of a User-Defined Function


What happens?
Formal parameter
Return
Name expression
Def statement: >>> def square( x ): A new function is created!

Def return mul(x, x) Name bound to that function


in the current frame
statement
Body (return statement)
Multiple Environments
operand: 2+2 Operator & operands evaluated
Call expression: square(2+2) argument: 4
Function (value of operator)
called on arguments 

operator: square (values of operands)

function: func square(x)

A new frame is created!


Calling/Applying: 4 square( x ):
Parameters bound to arguments
Argument 16
Signature
Body is executed in that new
Return value environment
8
Multiple Environments in One Diagram! Multiple Environments in One Diagram!

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

func square(x) func square(x) 9


square(3) square(3)

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

9 10
Interactive Diagram Interactive Diagram

Multiple Environments in One Diagram! Names Have No Meaning Without Environments

1 1

2 2

2 2

1 1

1 1
81 Every expression is
evaluated in the context
square(square(3))
of an environment.

func square(x) 9 A name evaluates to the


square(3) An environment is a sequence of frames. value bound to that name An environment is a sequence of frames.
in the earliest frame of
• The global frame alone the current environment in • The global frame alone
func square(x) 3 which that name is found.
• A local, then the global frame • A local, then the global frame
Interactive Diagram 11
Interactive Diagram 12

Names Have Different Meanings in Different Environments

A call expression and the body of the function being called


are evaluated in different environments

2
Miscellaneous Python Features
1

Every expression is Division


evaluated in the context Multiple Return Values
of an environment.
Source Files
Doctests
A name evaluates to the
value bound to that name Default Arguments
in the earliest frame of
the current environment in
which that name is found. (Demo)

Interactive Diagram 13

Statements

A statement is executed by the interpreter to perform an action

Compound statements:

Statement The first header determines a


Conditional Statements Clause statement’s type

<header>:
<statement> The header of a clause
<statement> Suite “controls” the suite that
... follows
<separating header>:
<statement>
<statement> def statements are compound
... statements
...

16
Compound Statements Conditional Statements
(Demo)
Compound statements:
A suite is a sequence of def absolute_value(x):
<header>: statements """Return the absolute value of x."""
<statement> if x < 0:
<statement> Suite return -x
1 statement,
... 3 clauses,
 elif x == 0:
<separating header>: To “execute” a suite means to 3 headers, return 0
<statement> execute its sequence of 3 suites else:
<statement> statements, in order return x
...
...
Execution Rule for Conditional Statements: Syntax Tips:

Execution Rule for a sequence of statements: Each clause is considered in order. 1. Always starts with "if" clause.

• Execute the first statement 1. Evaluate the header's expression. 2. Zero or more "elif" clauses.

• Unless directed otherwise, execute the rest 2. If it is a true value, 
 3. Zero or one "else" clause,

execute the suite & skip the remaining clauses. always at the end.
17 18

Boolean Contexts Boolean Contexts

def absolute_value(x): def absolute_value(x):


"""Return the absolute value of x.""" """Return the absolute value of x."""
if x < 0: if x < 0:
return -x return -x
elif x == 0: elif x == 0: Two boolean contexts
return 0 return 0
else: else:
return x return x

George Boole George Boole

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

True values in Python: Anything else (True)

Read Section 1.5.4!

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

While Statements

(Demo)

1 2 3
1 3 6
Iteration

George Boole
Execution Rule for 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

You might also like