本系列为加州伯克利大学著名 Python 基础课程 CS61A 的课堂笔记整理,全英文内容,文末附词汇解释。
目录
01 Lists 列表
02 Containers 容器
03 For Statements For 语句
04 Ranges 范围
05 List Comprehensions 列表理解
06 Box-and-Pointer Notation 箱点标记
Ⅰ The Closure Property of Data Types
Ⅱ Box-and-Pointer Notation in Environment Diagrams
07 Slicing 切片
08 Processing Container Values 处理容器值
Ⅰ sum(iterable[ , start]) -> value
Ⅱ max(iterable[ , key = func]) -> value and max(a, b, c, ... [ , key = func]) -> value
Ⅲ all(iterable) -> bool
附:词汇解释
01 Lists 列表
>>> digits = [0, 5, 2, 6]
>>> digits = [2-2, 1+1+3, 2, 2*3]
#The number of elements
>>> len(digits)
#An element selected by its index
>>> digits[3]
>>> getitem(digits, 3)
#Concarenation and repetition
>>> [2, 7] + digits * 2
>>> add([2, 7], mul(digits, 2))
#Nested lists
>>> pairs = [[10, 20], [30, 40]]
>>> pairs[1] #[30, 40]
>>> pairs[1][0] #30
02 Containers 容器
Built-in operators for testing whether an element appears in a compound value.
>>> digits = [0, 5, 2, 6]
>>> 0 in digits
True
>>> 1 not in digits
True
>>> not(1 in digits)
True
>>> '5' == 5
False
>>> '5' in digits
False
>>> [0, 5] in digits
False
>>> [0, 5] in [[0, 5], 2, 6]
03 For Statements For 语句
for <name> in <expression>:
<suite>
① Evaluate the header <expression>, while must yield an iterable value (a sequence).
② For each element in that sequence, in order:
A Bind <name> to that element in the current frame.
B Execute the <suite>.
def count(s, value):
'''Count the number of times that value occurs in sequence s.
>>> count([1, 2, 1, 3, 1], 1)
3
'''
total = 0
#Name bound in the first frame of the current environment.
for element in s:
if element == value:
total += 1
return total
Sequence unpacking in for statements.
pairs = [[1, 2], [2, 3], [2, 2], [1, 1]]
count = 0
for x,y in pairs:
if x == y:
count += 1
04 Ranges 范围
A range is a sequence of consecutive integers.
Length: ending value - starting value
Element selection: starting value + index
>>> list(range(-2, 2))
[-2, -1, 0, 1]
#Range with a 0 starting value
>>> list(range(4))
[0, 1, 2, 3]
def sumBelow(n):
total = 0
for i in range(n):
total += i
return total
def cheer():
for _ in range(3):
print('Go Bears!')
05 List Comprehensions 列表理解
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
>>> [letters[i] for i in [3, 4, 6, 8]]
['d', 'e', 'g', 'i']
>>> odds = [1, 3, 5, 7, 9]
>>> [x+1 for x in odds]
[2, 4, 6, 8, 10]
>>> [x for x in odds if 25 % x == 0]
[1, 5]
def divisors(n):
return [1] + [x for x in range(2, n) if n % x == 0]
>>>divisors(1)
[1]
>>>divisors(4)
[1, 2]
>>>divisors(9)
[1, 3]
>>>divisors(12)
[1, 2, 3, 4, 6]
06 Box-and-Pointer Notation 箱点标记
Ⅰ The Closure Property of Data Types
A method for combing data values satisfies the closure property if the result of combination can itself be combined using the same method.
Closure is powerful because it permits us to create hierarchical structures.
Hierarchical structures are made up of parts, while themselves are made up of parts, and so on.
Attention: Lists can contain lists as elements (in addition to anything else).
Ⅱ Box-and-Pointer Notation in Environment Diagrams
Lists are represented as a row of index-labeled adjacent boxes, one per element.
Each box either contains a primitive value or points to a compound value.
07 Slicing 切片
>>> odds = [3, 5, 7, 9, 11]
>>> odds[1:3]
[5, 7]
>>> odds[:3]
[3, 5, 7]
>>> odds[1:]
[5, 7, 9, 11]
>>> odds[:]
[3, 5, 7, 9, 11]
Slicing creates new values.
08 Processing Container Values 处理容器值
Several built-in functions take iterable arguments and aggregate them into a value.
Ⅰ sum(iterable[ , start]) -> value
Return the sum of an iterable of numbers (NOT strings) plus the value of parameter 'start' (which defaults to 0). When the iterable is empty, return start.
>>> sum([2, 3, 4])
9
>>> sum([2, 3, 4], 5)
14
>>> [2, 3] + [4]
[2, 3, 4]
>>> sum([[2, 3], [4]], [])
[2, 3, 4]
Ⅱ max(iterable[ , key = func]) -> value and max(a, b, c, ... [ , key = func]) -> value
With a single iterable argument, return its largest item.
With two or more arguments, return the largest argument.
>>> max(range(5))
4
>>> max(0, 1, 2, 3, 4)
4
>>> max(range(10), key=lambda x: 7-(x-4)*(x-2))
3
>>> (lambda x: 7-(x-4)*(x-2))(3)
8
>>> (lambda x: 7-(x-4)*(x-2))(2)
7
>>> (lambda x: 7-(x-4)*(x-2))(4)
7
>>> (lambda x: 7-(x-4)*(x-2))(5)
4
Ⅲ all(iterable) -> bool
Return True if bool(x) is True for all values x in the iterable.
If the iterable is empty, return True.
>>> bool(5)
True
>>> bool(True)
True
>>> bool(-1)
True
>>> bool(0)
False
>>> bool('hello')
True
>>> bool('')
False
>>> range(5)
range(0, 5)
>>> [x<5 for x in range(5)]
[True, True, True, True, True]
>>> all([x<5 for x in range(5)])
True
>>> all(range(5))
False
附:词汇解释
yield / jiːld / 产生
iterable 可迭代的
consecutive / kənˈsekjətɪv / 连续的,不间断的
comprehension 理解,领悟
odd 奇数的
divisor/ dɪˈvaɪzər / 除数,因子
closure property 闭包性质:在数学中,指一个集合在某种运算下的封闭性,即该运算的结果仍属于该集合
hierarchical / ˌhaɪəˈrɑːrkɪkl / structure 层次结构:是按层次划分的数据元素的集合,指定层次上元素可 以有零个或多个处于下一个层次上的直接所属下层元素
index-labeled 标记指数
adjacent / əˈdʒeɪs(ə)nt / 相邻的
primitive value 原始值
compound value 复合值
notation 标记