CS61A notes

Lecture 1

An important role for you as a programmer is to structure expressions so that they remain interpretable by yourself, your programming partners, and other people who may read your expressions in the future.

Names can also be bound to functions.

1
2
3
4
5
6
7
8
9
>>> f = max

>>> f

<built-in function max>

>>> f(2, 3, 4)

4

One can even bind built-in names to new values.

assign multiple values to multiple names in a single statement

area, circumference = pi * radius * radius, 2 * pi * radius

swapping the values bound to two names can be performed in a single statement.

y, x = x, y

Lecture 2

Functions may do additional things when called besides returning a value.

Lecture 3

  1. Function names are lowercase, with words separated by underscores. Descriptive names are encouraged.
  2. Function names typically evoke operations applied to arguments by the interpreter (e.g., print, add, square) or the name of the quantity that results (e.g., max, abs, sum).
  3. Parameter names are lowercase, with words separated by underscores. Single-word names are preferred.
  4. Parameter names should evoke the role of the parameter in the function, not just the kind of argument that is allowed.
  5. Single letter parameter names are acceptable when their role is obvious, but avoid “l” (lowercase ell), “O” (capital oh), or “I” (capital i) to avoid confusion with numerals.
  • Each function should have exactly one job. That job should be identifiable with a short name and characterizable in a single line of text. Functions that perform multiple jobs in sequence should be divided into multiple functions.
  • Don’t repeat yourself is a central tenet of software engineering. The so-called DRY principle states that multiple fragments of code should not describe redundant logic. Instead, that logic should be implemented once, given a name, and applied multiple times. If you find yourself copying and pasting a block of code, you have probably found an opportunity for functional abstraction.
  • Functions should be defined generally. Squaring is not in the Python Library precisely because it is a special case of the pow function, which raises numbers to arbitrary powers.

A function definition will often include documentation describing the function, called a docstring, which must be indented along with the function body. Docstrings are conventionally triple quoted. The first line describes the job of the function in one line. The following lines can describe arguments and clarify the behavior of the function:

1
2
3
4
5
6
7
8
9
10
11
>>> def pressure(v, t, n):
"""Compute the pressure in pascals of an ideal gas.

Applies the ideal gas law: http://en.wikipedia.org/wiki/Ideal_gas_law

v -- volume of gas, in cubic meters
t -- absolute temperature in degrees kelvin
n -- particles of gas
"""
k = 1.38e-23 # Boltzmann's constant
return n * k * t / v

给代码写注释非常重要!!!

Programmers use assert statements to verify expectations, such as the output of a function being tested. An assert statement has an expression in a boolean context, followed by a quoted line of text (single or double quotes are both fine, but be consistent) that will be displayed if the expression evaluates to a false value.

When writing Python in files, rather than directly into the interpreter, tests are typically written in the same file or a neighboring file with the suffix _test.py.

Doctests.

Lecture 4

  • Functions on Functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def summation(N, f):
k, sum = 1, 0
while k <= N:
sum, k = sum+f(k), k+1
return sum

def sum_squares(N):
def square(x):
return x*x
return summation(N, square)

# or

def sum_squares(N):
return summation(N, lambda x: x*x)
  • Writing lambda PARAMS: EXPRESSION is the same as writing NAME, where NAME is a name that appears nowhere else in the program and is defined by
1
2
def NAME(PARAMS):
return EXPRESSION

evaluated in the same environment in which the original lambda was.

summation(10, lambda x: x**3) # Sum of cubes

  • Functions that return functions
1
2
3
4
5
6
7
8
def add_func(f, g):
"""Return function that returns F(x)+G(x) for argument x."""
def adder(x): #
return f(x) + g(x) # or return lambda x: f(x) + g(x)
return adder #

h = add_func(add, neg)
print(h(-5))