SICP-Notes-Lecture 03 Control

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 or or , 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:
  1. Evaluate the header’s expression

  2. 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值