[2021 Spring] CS61A 学习笔记 lecture 14 List mutation + Identity + Nonlocal
2021/6/29 23:53:35
本文主要是介绍[2021 Spring] CS61A 学习笔记 lecture 14 List mutation + Identity + Nonlocal,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
主要内容:
列表的创建、更新、方法(python基础课程均有讲解,也可以查看菜鸟教程等)
is 和 == 的区别
nonlocal 和 global(可以参考parent frame)
- List creation, List mutation, List methods
- bonus 1: = 与 +=
- bonus 2: append 与 extend
- Equality and Identity
- List
- strings/numbers
- Scope
List creation, List mutation, List methods
bonus 1: = 与 +=
b = b + [8, 9]
改变了b的指向,b += [8, 9]
没有改变。(老师只提出了这个现象,没有解释原因,philosophy)
bonus 2: append 与 extend
- append() adds a single element to a list
- extend() adds all the elements in one list to a list
- pop() removes and returns the last element
- remove() removes the first element equal to the argument
t为列表,t的变化会影响到s.append(t)
,但是不会影响到s.extend(t)
.
Equality and Identity
- Identity : exp0 is exp1
- evaluates to True if both exp0 and exp1 evaluate to the same object
- Equality: exp0 == exp1
- evaluates to True if both exp0 and exp1 evaluate to objects containing equal values
对于列表和字符串/数字来说不同,可能原因:列表是可变类型,字符串和数字是不可变类型。
- evaluates to True if both exp0 and exp1 evaluate to objects containing equal values
List
注意:两个各自定义的列表指向并不相同,所以identity=False,只有值相同。(对于字符串/数字可能不成立,identity=True)
a = ["apples" , "bananas"] b = ["apples" , "bananas"] c = a if a == b == c: print("All equal!") a[1] = "oranges" if a is c and a == c: print("A and C are equal AND identical!") if a == b: print("A and B are equal!") # Nope! if b == c: print("B and C are equal!") # Nope!
strings/numbers
以下全部为True:
a = "orange" b = "orange" c = "o" + "range" print(a is b) print(a is c) a = 100 b = 100 print(a is b) print(a is 10 * 10) print(a == 10 * 10) a = 100000000000000000 b = 100000000000000000 print(a is b) print(100000000000000000 is 100000000000000000 )
Scope
scope rules
这篇关于[2021 Spring] CS61A 学习笔记 lecture 14 List mutation + Identity + Nonlocal的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求
- 2024-10-01使用 golang 将ETH账户的资产平均分散到其他账户
- 2024-10-01JWT用户校验课程:从入门到实践
- 2024-10-01Server Component课程入门指南