1、列表list去重
l1 = [1,1,2,3,5,5,4,4,4,5,6] set1 = set(l1) # print(set1) # set是集合 l2 = list(set1) # 将集合转换成列表 print(l2) #[1, 2, 3, 4, 5, 6]
2、字典的创建3种方式
# (1)直接创建 dic1 = {'name':'alex','age':28,'sex':'M'} # (2)fromkeys()创建 dic = dict.fromkeys([1,2,3],'alex') print(dic) # (3)通过 dic = dict({'name':'alex','age':28,'sex':'M'}) print(dic)
3,相关面试题(先从纸上写好答案,然后在运行):
5.1,有函数定义如下:
def calc(a,b,c,d=1,e=2):
return (a+b)*(c-d)+e
请分别写出下列标号代码的输出结果,如果出错请写出Error。
print(calc(1,2,3,4,5))__2___
print(calc(1,2))_Error___
print(calc(e=4,c=5,a=2,b=3))_24__
print(calc(1,2,3))__8___
print(calc(1,2,3,e=4))__10__
print(calc(1,2,3,d=5,4))_Error____
4、下面代码打印的结果
分别是_list1=[10,'a'],list1=[123,],list1=['a',10].
def extendList(val,list=[]): list.append(val) return list list1 = extendList(10) list2 = extendList(123,[]) list3 = extendList('a') print('list1=%s'%list1) print('list2=%s'%list2) print('list3=%s'%list3)
5、求结果
v = [i % 2 for i in range(10)] print(v) 结果为: [0,1,0,1,0,1,0,1,0,1] v = (i % 2 for i in range(10)) print(v) 结果为: v:生成器 for i in range(5): print(i) print(i) 结果为: 012344