马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
本系列为加州伯克利大学闻名 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 / 除数,因子、index-labeled 标记指数、adjacent / əˈdʒeɪs(ə)nt / 相邻的、primitive value 原始值、compound value 复合值、notation 标记
closure property 闭包性子:在数学中,指一个聚集在某种运算下的封闭性,即该运算的结果仍属于该聚集
hierarchical / ˌhaɪəˈrɑːrkɪkl / structure 条理结构:是按条理分别的数据元素的聚集,指定条理上元素可 以有零个或多个处于下一个条理上的直接所下属层元素
|