03 Control 1pp
03 Control 1pp
Announcements
Print and None
(Demo)
None Indicates that Nothing is Returned
A function that does not explicitly return a value will return None
4
Pure Functions & Non-Pure Functions
Return value
Pure Functions
just return values -2 abs
2
Argument
2, 100 pow
1267650600228229401496703205376
2 Arguments
Returns None!
Non-Pure Functions
have side effects -2 print
None
A side effect isn't a
value; it's anything
Python displays the output “-2” that happens as a
consequence of
calling a function
5
Nested Expressions with Print
1 print(...): 2 print(...):
None None
6
Multiple Environments
Life Cycle of a User-Defined Function
What happens?
Formal parameter
Return
Name expression
Def statement: >>> def square( x ): A new function is created!
square(square(3))
func square(x)
square(3)
func square(x) 3
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
9
Multiple Environments in One Diagram!
square(square(3))
func square(x) 9
square(3)
func square(x) 3
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
10
Multiple Environments in One Diagram!
1
81
square(square(3))
func square(x) 9
square(3) An environment is a sequence of frames.
1
Every expression is
evaluated in the context
of an environment.
Every expression is
evaluated in the context
of an environment.
13
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
Miscellaneous Python Features
Division
Multiple Return Values
Source Files
Doctests
Default Arguments
(Demo)
Conditional Statements
Statements
Compound statements:
<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
Compound statements:
A suite is a sequence of
<header>: statements
<statement>
Suite
<statement>
...
<separating header>: To “execute” a suite means to
<statement> execute its sequence of
<statement> statements, in order
...
...
17
Conditional Statements
def absolute_value(x):
"""Return the absolute value of x."""
if x < 0:
1 statement, return -x
3 clauses, elif x == 0:
3 headers, return 0
3 suites else:
return x
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
19
Boolean Contexts
def absolute_value(x):
"""Return the absolute value of x."""
if x < 0:
return -x
elif x == 0: Two boolean contexts
return 0
else:
return x
George Boole
(Demo)
20
Reading: http://composingprograms.com/pages/15-control.html#conditional-statements
Iteration
While Statements
(Demo)
1 2 3
1 3 6
2. If it is a true value,
execute the (whole) suite,
then return to step 1.
(Demo)
22
Example: Prime Factorization
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
...
(Demo)
24