a = True
while a:
b = input('type something:')
if b == '1':
a = False
else:
pass
print('still in while')
print('finish run')
运行,输入,得到
type something:2
still in while
type something:1
still in while
finish run
新建Python文件,输入
while True:
b = input('type something:')
if b == '1':
break
else:
pass
print('still in while')
print('finish run')
运行,输入,得到
type something:2
still in while
type something:3
still in while
type something:1
finish run
新建Python文件,输入
while True:
b = input('type something:')
if b == '1':
continue
else:
pass
print('still in while')
print('finish run')
运行,输入,得到
type something:2
still in while
type something:1
type something:
13.2 try 错误处理
新建Python文件,输入
try:
file = open('eeee', 'r')
except Exception as e:
print(e)
得到
[Errno 2] No such file or directory: 'eeee'
新建Python文件,输入
try:
file = open('eeee.txt', 'r+')
except Exception as e:
print('there is no file named eeee')
response = input('do you want to create a new file')
if response == 'y':
file = open('eeee.txt', 'w')
else:
pass
else:
file.write('ssss')
file.close()
运行,输入y
there is no file named eeee
do you want to create a new filey
得到
再次运行程序,得到
a = [1, 2, 3]
b = [4, 5, 6]
for i, j, z in zip(a, a, b):
print(i/2, j/2, z*2)
得到
0.5 0.5 8
1.0 1.0 10
1.5 1.5 12
输入
def fun1(x, y):
return (x+y)
print(fun1(2, 3))
fun2 = lambda x, y: x+y
print(fun2(2, 3))
得到
5
5
输入
def fun1(x, y):
return (x+y)
print(list(map(fun1, [1, 2], [3, 4])))
得到
[4, 6]
13.4 copy & deepcopy 浅复制 & 深复制
新建Python文件,输入
import copy
a = [1, 2, 3]
b = a
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] = 222
print(a)
print(c)
得到
1384528958984
1384528958984
[11, 2, 3]
[11, 22, 3]
True
False
[11, 22, 3]
[11, 222, 3]
输入
import copy
a = [1, 2, [3, 4]]
b = copy.copy(a)
print(id(a) == id(b))
print(id(a[2]) == id(b[2]))
a[0] = 11
print(a)
print(b)
a[2][0] = 33
print(a)
print(b)
得到
False
True
[11, 2, [3, 4]]
[1, 2, [3, 4]]
[11, 2, [33, 4]]
[1, 2, [33, 4]]
copy只会复制父对象,而子对象是共用的;deepcopy完全复制所有的父对象和子对象。
输入import copy
a = [1, 2, [3, 4]]
b = copy.deepcopy(a)
print(id(a[2]) == id(b[2]))
得到
False
13.5 threading 什么是多线程
多线程就是使Python分批同时处理一些东西,大大减少了计算时间。
13.6 multiprocessing 什么是多进程运用多核来处理Python程序,克服了多线程的缺点。
13.7 什么是tkinter窗口Tkinter 是 Python 的标准 GUI 库。Python 使用 Tkinter 可以快速的创建 GUI 应用程序。
13.8 pickle 保存数据 新建Python文件,输入import pickle
a_dict = {'da': 111, 2: [23, 1, 4], '23': {1: 2, 'd': 'sad'}}
file = open('pickle_example.pickle', 'wb')
pickle.dump(a_dict, file)
file.close()
file1 = open('pickle_example.pickle', 'rb')
a_dict1 = pickle.load(file1)
file1.close()
print(a_dict1)
得到
{'da': 111, 2: [23, 1, 4], '23': {1: 2, 'd': 'sad'}}
输入
import pickle
a_dict = {'da': 111, 2: [23, 1, 4], '23': {1: 2, 'd': 'sad'}}
with open('pickle_example.pickle', 'wb') as file:
pickle.dump(a_dict, file)
with open('pickle_example.pickle', 'rb') as file1:
a_dict1 = pickle.load(file1)
print(a_dict1)
得到
{'da': 111, 2: [23, 1, 4], '23': {1: 2, 'd': 'sad'}}
13.9 set 找不同
新建Python文件,输入
char_list = ['a', 'b', 'c', 'c', 'd', 'd', 'd']
print(set(char_list))
print(type(set(char_list)))
print(type({1: 2}))
sentence = 'Flexible Job Shop Scheduling with AGVs'
print(set(sentence))
unique_char = set(char_list)
unique_char.add('x')
print(unique_char)
unique_char.add('a')
print(unique_char)
unique_char.clear()
print(unique_char)
unique_char = ['d', 'x', 'a', 'b', 'c', 'x']
unique_char.remove('x')
print(unique_char)
得到
{'a', 'd', 'c', 'b'}
{'J', 'l', 's', 'w', 'S', 'o', 'x', 'A', 'u', 'c', ' ', 'g', 'b', 'i', 'h', 'p', 'G', 'e', 'V', 'd', 'F', 'n', 't'}
{'d', 'b', 'a', 'c', 'x'}
{'d', 'b', 'a', 'c', 'x'}
set()
['d', 'a', 'b', 'c', 'x']
找出两个列表中相同和不同的元素。输入:
list1 = ['张三', '李四', '王五', '老二']
list2 = ['张三', '李四', '老二', '王七']
a = [x for x in list1 if x in list2] #两个列表表都存在
b = [y for y in (list1 + list2) if y not in a] #两个列表中的不同元素
print('a的值为:', a)
print('b的值为:', b)
c = [x for x in list1 if x not in list2] #在list1列表中而不在list2列表中
d = [y for y in list2 if y not in list1] #在list2列表中而不在list1列表中
print('c的值为:', c)
print('d的值为:', d)
得到
a的值为: ['张三', '李四', '老二']
b的值为: ['王五', '王七']
c的值为: ['王五']
d的值为: ['王七']
13.10 正则表达式
正则表达式(Regular Expression)又称 RegEx,是用来匹配字符的一种工具。在一大串字符中寻找你需要的内容。它常被用在很多方面,比如网页爬虫,文稿整理,数据筛选等等。该部分等待以后进行学习。