Lecture 03 Control
These are my notes for SICP(Structure and Interpretation of Computer Programs). Hope they’ll be of some help to you.
None
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.
>>> def does_not_return_square(x):
... x * x #No return
...
>>> does_not_return_square(4) #None value is not displayed
>>> sixteen = does_not_return_square(4) #The name sixteen 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
- Pure Functions: just return values
- Non-pure Functions: have side effects
- A side effect isn’t a value; it’s anything that happens as a consequence of calling a function.
print(-2)
Python displays the output-2
, but this function return none.
- Nested Expressions with Print
>>> print(print(1), print(2))
1
2
None None
Control
Conditional statements (if statements)
if <conditional expression>:
<suite of statements> #Always start with if clause
elif <conditional expression>:
<suite of statements> #Zero or more elif clauses
else:
<suite of statements> #Zero or one else clause, always at the end
If the expression evaluates to true or the header is an else, execute the suite and skip the remaining headers
- an if example
def abs(x):
"""Return the absolute value of x."""
if x < 0:
return -x
elif x == 0:
return 0
else:
return x
#There are two boolean contexts in this assignment statement.
Boolean Contexts
Boolean context is any place where an expression is evaluated to check if it’s a True value or a False value.
- False values in Python:
False
,None
,0
,(empty strings)
- True values: everything else
Boolean Expressions
<exp1> and <exp2> and <exp3> and ...
- Evaluate to the first false value.
- If none are false, evaluate to the last expression
<exp1> or <exp2> or <exp3> or ...
- Evaluate to the first true value.
- If none are true, evaluates to the last expression
not <exp>
When using
and
oror
, there could be short-circuiting. Because usually python won’t evaluate all the expressions contained.
Iteration
While statements
i, total = 0, 0
while i < 3:
i = i + 1
total = total + i
- Execution Rule for While Statements:
-
Evaluate the header’s expression
-
If it is a true value, execute the (whole) suite, then return to step 1
The Fibonacci Sequence (example)
def fib(n):
"""Compute the nth Fibonacci number, for N >= 1"""
pred, curr = 0, 1
k = 1
while k < n:
pred, curr = curr, pred + curr
k = k + 1
return curr
The next Fibonacci number is the sum of the current one and its predecessor.
Summary
- print vs None
- None represents when an expressions doesn’t evaluate to a value
- Print displays values in the interpreter
- Control
- Allow for the interpreter to selectively or repetitively execute parts of a program
- Iteration
- A particular variant of control which is based in the idea of repeating operations to compute a value