我的视频学习笔记
continue
while True:
b = input('type something: ')
if b == '1':
continue # 直接跳过后面的while语句 重新开始while循环
else:
pass
print('still in while')
print('finish run')
输出:
type something: 2
still in while
type something: 1
type something:
break
while True:
b = input('type something: ')
if b == '1':
break # 直接跳出while循环
else:
pass
print('still in while')
print('finish run')
输出:
type something: 2
still in while
type something: 1
finish run
错误处理 Try except else搭配用法
# file = open('eeee', 'r') # FileNotFoundError: [Errno 2] No such file or directory: 'eeee'
try:
file = open('eeee', 'r+') # 读+写的方法打开eeee这个名字的文件
except Exception as e: # 如果没有这个这个文件进入异常处理
print(e)
print('there is no file named as eeee')
response = input('do you want to create a new file ? y/n:') # 是否要新建一个名字为eeee的文件
if response == 'y':
file = open('eeee', 'w')
file.close()
else:
pass
else:
file.write('ssss') # 如果有这个文件 就往里面写入ssss
file.close()
浅复制与深复制 copy
# copy
import copy
a = [1, 2, 3]
b = a # 将两个索引绑在一起 完全copy 同一个存储空间
print(id(a))
print(id(b))
b[0] = 11
print(a)
a[1] = 22
print(b)
print(id(a) == id(b))
c = copy.copy(a) # 浅复制
print(id(a) == id(c))
c[1] = 2222
print(a)
print(c)
a = [1, 2, [3, 4]]
d = copy.copy(a)
print(id(a) == id(d))
print(id(a[2]) == id(d[2]))
a[0] = 111
print(a)
print(d)
a[2][0] = 333
print(a)
print(d)
e = copy.deepcopy(a) # 深复制 地址完全不重复 任何东西都不会存到同一个内存空间
print(id(a[2]) == id(e[2]))
输出:
2555426810440
2555426810440
[11, 2, 3]
[11, 22, 3]
True
False
[11, 22, 3]
[11, 2222, 3]
False
True
[111, 2, [3, 4]]
[1, 2, [3, 4]]
[111, 2, [333, 4]]
[1, 2, [333, 4]]
False