参考小甲鱼python课程,就此记录下我的学习问题,以方便后续复习。
目录
参考小甲鱼python课程,就此记录下我的学习问题,以方便后续复习。
1.1 请写出下列表达式的答案not 1 or 0 and 1 or 3 and 4 or 5 and 6 or 7 and 8 and 9
2.3 (x < y and [x] or [y])[0] 表达式的功能
4.1 请问分别使用什么BIF,可以把一个可迭代对象转换为列表、元祖和字符串?
1. 常用操作符
1.1 请写出下列表达式的答案not 1 or 0 and 1 or 3 and 4 or 5 and 6 or 7 and 8 and 9
答案:4(短路逻辑)
学习链接:https://www.jianshu.com/nb/27681479?native.theme=1
2. 了不起的分支与循环
2.1 assert关键字的作用
答案:当我们在写代码的时候,防止系统突然崩溃,在程序后面添加assert断言字段,实现自我中断。例如assert(3>4)就会抛出AssertionError异常。用途:单元测试。
2.2 值的互换
答案:
x = 1
y = 2
z = 3
x, y, z = z, y, x
#3, 2, 1
2.3 (x < y and [x] or [y])[0] 表达式的功能
答案:x<y输出x,否则输出y,[0]含义是当x,y存在0的情况表达式出错,所以用[x],[y]表示。通常用x if x < y else y代替。
2.4 print不换行
i = 0
string = '我爱中国'
length = len(string)
while i < length:
print(string[i], end = '')
i += 1
3. 一个打了激素的数组
3.1 列表赋值
list1 = [1,3,2]
list2 = list1[:] //拷贝
list3 = list1 //非拷贝
请问list3与list2是否相同?
答案:list2与list3所含元素相同,但是地址不同。list3与list1同属一个地址,list2开辟了新地址空间。如果list1.sort() -->list1 = [1,2,3]。list3 = [1,2,3]。list2 = [1,3,2]。
3.2 列表解析
表达式[i*i for i in range(10)]返回值是什么?
答案:list1 = [i*i for i in range(10)]。 list1 = [0,1, 4, 9, 16, 25, 36, 49, 81]
3.3 合并字符串
list1 = ['1.Jost do It','2.一切皆有可能','3.让变成改变世界','4.Impossible is nothing']
list2 = ['4.阿迪达斯','2.李宁','3.鱼C工作室','1.耐克']
将前两个->list3 = ['1.耐克:Jost do It', '2.李宁:一切皆有可能', '3.鱼C工作室:让变成改变世界', '4.阿迪达斯:Impossible is nothing']
第一种方法:
list1 = ['1.Jost do It','2.一切皆有可能','3.让变成改变世界','4.Impossible is nothing']
list2 = ['4.阿迪达斯','2.李宁','3.鱼C工作室','1.耐克']
list3=[]
for slogan in list1:
for name in list2:
if slogan[0]==name[0]:
list3.append(name + ":" + slogan[2:])
print(list3)
第二种方法:
list1 = ['1.Jost do It','2.一切皆有可能','3.让变成改变世界','4.Impossible is nothing']
list2 = ['4.阿迪达斯','2.李宁','3.鱼C工作室','1.耐克']
list3 = [(y+':'+x[2:]) for x in list1 for y in list2 if x[0] == y[0]]
3.4 跨行注释
'''
注释
'''
3.5 下列代码含义
str1 = '<a href="http://www.fishc.com/dvd" target="_blank">鱼C资源打包</a>'
str2 = str1[16:29]
str3 = str1[-45:-32]
str4 = str1[20:-36]
print(str2)
print(str3)
print(str4)
答案:输出 str2 = www.fishc.com str3 = www.fishc.com str4 = fishc
4. 序列
4.1 请问分别使用什么BIF,可以把一个可迭代对象转换为列表、元祖和字符串?
迭代器: enumerate, zip, reversed
list([iterator]), tuple([iterator]), str([obj])。
str1 = 'ILOVEFISHC'
for each in enumerate(str1):
print(each, end = ' ')
#答案: (0, 'I') (1, 'L') (2, 'O') (3, 'V') (4, 'E') (5, 'F') (6, 'I') (7, 'S') (8, 'H') (9, 'C')
4.2 迭代概念
所谓迭代,就是重复反馈过程的活动,其目的通常是为了接近并到达所需的目标和结果。每一次对过程的重复成为一次‘迭代’,而每一次迭代的结果会被用来作为下一次迭代的初始值。
4.3 统计字符串各种字符出现的次数
def counts(string):
word = 0
num = 0
space = 0
other = 0
length = len(string)
# for i in range(length):
# if(string[i] >= '0' and string[i] <= '9'):
# num += 1
# elif(string[i] == ' '):
# space += 1
# elif((string[i] >= 'a' and string[i] <= 'z') or (string[i] >= 'A' and string[i] <= 'Z')):
# word += 1
# else:
# other += 1
for i in range(length):
if(string[i].isdigit()):
num += 1
elif(string[i].isalpha()):
word += 1
elif(string[i] == ' '):
space += 1
else:
other += 1
return [('数字:',num), ('字母:', word), ('空格:', space), ('其他字符:', other)]
str1 = 'Hello world! 123456@163.com'
print(counts(str1))
5. 函数
5.1 内嵌函数与闭包函数
def fun1(x):
def fun2(y):
return x * y
x = int(input('请输入第一个参数x:'))
print('形成了一个以x为基准的函数fun2(y)')
y = int(input('请输入第二个参数y:'))
while y != 0 :
result = x(y)
print('结果是:', result)
y = int(input('请输入第二个参数y:'))
print('内嵌函数、闭包函数展示完毕')
5.2 匿名函数、过滤函数与map函数
#匿名函数初使用
g = lambda x, y : x + y
print(g(2,3))
#过滤函数初使用
g = list(filter(lambda x : x % 2, range(10)))
print(g)
# g = [1, 3, 5, 7, 9]
#map函数初使用
g = list(map(lambda x : x * 2, range(10)))
print(g)
# g = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
#求100以内3的倍数
g = list(filter(lambda x : x % 3 == 0, range(100)))
#filter与lambda的同时使用
print(g)
g = list(x for x in range(100) if x % 3 == 0)
#列表推导式再次使用
print(g)
6.字典
6.1 访问键'C'的值。'F':70,'C':67,'h':104,'i':105,'s':115
>>> 第一种方法
>>> MyDict_1 = dict((('F',70),('C',67),('h',104),('i',105),('s',115)))
>>> MyDict_1['C']
67
>>> 第二种方法
>>> MyDict_2 = {'F':70,'C':67,'h':104,'i':105,'s':115}
>>> MyDict_2['C']
67
>>> 第三种方法
>>> MyDict_3 = dict(F = 70, C = 67, h = 104, i = 105, s = 115)
>>> MyDict_3['C']
67
>>> 第四种方法
>>> MyDict_4 = dict([('F',70),('C',67),('h',104),('i',105),('s',115)])
>>> MyDict_4['C']
67
>>> 第五种方法
>>> MyDict_5 = dict({'F':70,'C':67,'h':104,'i':105,'s':115})
>>> MyDict_5['C']
67
>>> 第六种方法
>>> MyDict_6 = dict(zip(['F', 'C', 'h', 'i', 's'], [70, 67, 104, 105, 115]))
>>> MyDict_6['C']
67
7. 文件
7.1 文件操作
7.2 持久化工具pickle
对于一些既定字典,集合,序列等数据的处理,利用pickle进行序列化和反序列化操作,能对代码进行良好的维护与简化。
当然pickle也可以处理txt文件,二进制处理后可能乱码。
import pickle
pick_file = open('list.pkl', 'wb')
mylist = ['1', '2', 1, 2, 'ssss']
#dump:倾销 列表->文件
pickle.dump(mylist, pick_file)
#读取文件到列表中
pick_file = open('list.pkl', 'rb')
yourlist = pickle.load(pick_file)
print(yourlist)
7.3 处理异常
try:
n = int('abc')
sum = 1 + '1'
file_name = input('文件名:')
f = open(file_name)
print(f)
print('打开成功')
except OSError as reason:
print('文件出错')
except (TypeError, ValueError) as reason:
print('类型出错了')
else:
print('没有异常')
# 方式二:
# except ValueError as reason:
# print('值类型出错了')
# input()
finally:
f.close()
注意:其中 f = open(file_name)语句可以用with语句代替,with open(file_name, 'r...w...') as f 相当于自带了finally语句f.close()
8. Easygui窗口
8.1 安装命令
终端输入pip install easygui,安装完成进入python界面
import easygui as g
g.msgbox('第一个窗口')
#### 课外
1. 短横线意思
转载于https://blog.csdn.net/tcx1992/article/details/80105645