python基础2-目录
1、列表推导式
#多层循环是从内到外循环的
import random
lists = [(i,j,m) for i in range(1) for j in range(2) for m in range(3)]
print(lists)
#[(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 1, 0), (0, 1, 1), (0, 1, 2)]
lists1 = [(index,content) for index,content in enumerate([1,'a',3,4,5])]
print(lists1)
# [(0, 1), (1, 'a'), (2, 3), (3, 4), (4, 5)]
lists2 = [(key,value) for key,value in {1:2,2:3}.items()]
print(lists2)
# [(1, 2), (2, 3)]
2、函数================
参数类型参考:https://blog.csdn.net/feifeiyechuan/article/details/83421503
def fun(*args,**kwargs):
return 1,2 #返回列表(1,2)
a,b = fun() #拆包a=1,b=2
匿名函数
lambda x,y:x+y
3、快捷方法
flag = [False,True]['abc'.find('a') != -1]
print(flag)
#True
4、异常
#try后必须有except或者finally,如果有else必有except
#BaseException Exception IOError IndexError
try:
print(c)
raise Exception('Error')
except IndexError as e:
print('处理异常1:\n',e)
except Exception as e:
print('处理异常2:\n',e)
else:
print('execute else')
finally:
print('ecxecute finally')
#断言
a = 3
# assert a == 4; #如果不成立,抛出AssertionError异常
5、注意:
(1) Number类型可以内部比较,不同类型之间不能进行比较
(2) print((1,2)>(2,‘a’)) #在通过第一个元素可以必能比出大小的时候,不会报错,否则就报错