可变与不可变类型
不可变的数据类型有int、float、bool、str、tuple
可变的数据类型有list、set、dict
可变类型
- 内容发生改变,但是地址没有改变,则认为此类型是可变类型的
不可变类型
- 只要改变变量的值则地址发生变化,则认为此类型是不可变的
a = 5
print("未更改前的a的值为{},地址为:{}".format(a, id(a)))
a = 6
print("更改后的a的值为{},地址为:{}".format(a, id(a)))
list1 = [1, 2, 3, 4]
print("未更改前的list1的值为{},地址为:{}".format(list1, id(list1)))
list1.append(5)
print("更改后的list1的值为{},地址为:{}".format(list1, id(list1)))
结果:
未更改前的a的值为5,地址为:1810001040
更改后的a的值为6,地址为:1810001072
未更改前的list1的值为[1, 2, 3, 4],地址为:2952291296392
更改后的list1的值为[1, 2, 3, 4, 5],地址为:2952291296392
浅拷贝
- list2 = list1.copy()
- x2 = copy.copy(x) 需要导入copy模块–>import copy
以上的的两种拷贝方式都是浅拷贝,作用都一样。
import copy
list1 = ['嘻嘻', 20, '男', ['贾玲', '娜扎', '热巴']]
list2 = list1.copy() # 浅拷贝
list3 = copy.copy(list1)
print("---list3、list2浅拷贝后的结果 ----")
print(list1, "\n", list2, '\n', list3)
print(id(list1), id(list2), id(list3))
# 添加信息
list2[3].append('凤姐')
print("---list2的数据发生修改后的结果 ----")
print('list1:', list1, "\nlist2:", list2, '\nlist3:', list3)
print(id(list1), id(list2), id(list3))
list1[3].remove('热巴')
print("---list3的[可变数据]数据发生修改后的结果 ----")
print('list1:', list1, "\nlist2:", list2, '\nlist3:', list3)
print(id(list1), id(list2), id(list3))
list2[0] = '哈哈'
print("---list2的[不可变数据]的数据发生修改后的结果 ----")
print('list1:', list1, "\nlist2:", list2, '\nlist3:', list3)
print(id(list1), id(list2), id(list3))
结果:
—list3、list2浅拷贝后的结果 ----
[‘嘻嘻’, 20, ‘男’, [‘贾玲’, ‘娜扎’, ‘热巴’]]
[‘嘻嘻’, 20, ‘男’, [‘贾玲’, ‘娜扎’, ‘热巴’]]
[‘嘻嘻’, 20, ‘男’, [‘贾玲’, ‘娜扎’, ‘热巴’]]
2349172827656 2349170304136 2349172821192
—list2的数据发生修改后的结果 ----
list1: [‘嘻嘻’, 20, ‘男’, [‘贾玲’, ‘娜扎’, ‘热巴’, ‘凤姐’]]
list2: [‘嘻嘻’, 20, ‘男’, [‘贾玲’, ‘娜扎’, ‘热巴’, ‘凤姐’]]
list3: [‘嘻嘻’, 20, ‘男’, [‘贾玲’, ‘娜扎’, ‘热巴’, ‘凤姐’]]
2349172827656 2349170304136 2349172821192
—list1的[可变数据]数据发生修改后的结果 ----
list1: [‘嘻嘻’, 20, ‘男’, [‘贾玲’, ‘娜扎’, ‘凤姐’]]
list2: [‘嘻嘻’, 20, ‘男’, [‘贾玲’, ‘娜扎’, ‘凤姐’]]
list3: [‘嘻嘻’, 20, ‘男’, [‘贾玲’, ‘娜扎’, ‘凤姐’]]
2349172827656 2349170304136 2349172821192
—list2的[不可变数据]的数据发生修改后的结果 ----
list1: [‘嘻嘻’, 20, ‘男’, [‘贾玲’, ‘娜扎’, ‘凤姐’]]
list2: [‘哈哈’, 20, ‘男’, [‘贾玲’, ‘娜扎’, ‘凤姐’]]
list3: [‘嘻嘻’, 20, ‘男’, [‘贾玲’, ‘娜扎’, ‘凤姐’]]
2349172827656 2349170304136 2349172821192
浅拷贝的特点:
- 可变类型共用的是同一个(里面的内容改变,但是地址是不会改变)
- 不可变类型一开始是共用的,但是如果有发生改变的则地址就会发生改变了
图示:
深拷贝
- x2 = copy.deepcopy(x1)## 深拷贝与浅拷贝的区别
深拷贝在针对不可变类型时则会重新再内存开辟一个空间用来存放。
# 其他的代码都一样,仅仅修改这两句即可。
list2 = copy.deepcopy(list1) # 浅拷贝
list3 = copy.deepcopy(list1)
"""
运行结果:
---list3、list2深拷贝后的结果 ----
['嘻嘻', 20, '男', ['贾玲', '娜扎', '热巴']]
['嘻嘻', 20, '男', ['贾玲', '娜扎', '热巴']]
['嘻嘻', 20, '男', ['贾玲', '娜扎', '热巴']]
1614328276616 1614325752968 1614328275656
---list2的数据发生修改后的结果 ----
list1: ['嘻嘻', 20, '男', ['贾玲', '娜扎', '热巴']]
list2: ['嘻嘻', 20, '男', ['贾玲', '娜扎', '热巴', '凤姐']]
list3: ['嘻嘻', 20, '男', ['贾玲', '娜扎', '热巴']]
1614328276616 1614325752968 1614328275656
---list1的[可变数据]数据发生修改后的结果 ----
list1: ['嘻嘻', 20, '男', ['贾玲', '娜扎']]
list2: ['嘻嘻', 20, '男', ['贾玲', '娜扎', '热巴', '凤姐']]
list3: ['嘻嘻', 20, '男', ['贾玲', '娜扎', '热巴']]
1614328276616 1614325752968 1614328275656
---list2的[不可变数据]的数据发生修改后的结果 ----
list1: ['嘻嘻', 20, '男', ['贾玲', '娜扎']]
list2: ['哈哈', 20, '男', ['贾玲', '娜扎', '热巴', '凤姐']]
list3: ['嘻嘻', 20, '男', ['贾玲', '娜扎', '热巴']]
1614328276616 1614325752968 1614328275656
"""
图示:
函数
函数就是将某一些代码组合在一块,去干某一件事,防止代码冗余,增强了代码的可读性
无参函数
def func(): # 用def来定义一个函数 def 函数名(): 函数体
print('这是一个无参函数')
# 函数的调用
func()
有参函数
- 普通参数
def add_num(a,b):
r = a+b
print('r=',r)
add_num(3,5)
- 默认值参数
def add_num(a,b=0):# 默认值参数需要放在后面
r = a+b
print('r=',r)
add_num(3) # 3
add_num(3,5) # 8
- 关键字参数
def add_num(a,b=0):# 默认值参数需要放在后面
r = a+b
print('r=',r)
add_num(a=5,b=3) # 8
add_num(b=3,a=5) # 8
# 关键字参数则不需要按照顺序来写,可以改变前后的顺序
- 可变参数
- 拆包
list1 = [6, 8, 2] n1, n2, n3 = list1 # 都会先做拆包的动作,其次再给前面变量赋值 print(n1, n2, n3)
- 装包
# 求和 def add(*args): # r = a + b # print('和:', r) # print(args) s = 0 for i in args: s += i print('和:', s) # 调用add函数 add(6, 1) add() add(1) add(1, 2, 3, 4, 5) add(1, 2, 3)