第三次作业讲解:
# 第三次作业讲解: #2.根据范围获取其中3和7整除的所有数的和,并返回调用者 #符合条件的数字个数以及符合条件的数字的总数和 def func(start,end): n=[] sum=0 for i in range(100): if i % 3 == 0 and i % 7 == 0: n.append(i) sum+=i print(len(n)) print(‘总数和是 %d‘ %sum) func(1,100) #8、使用 set 集合获取两个列表 l1 = [11, 22, 33], l2 = [22, 33, 44] 中相同的元素集合 l1 = [11, 22, 33] l2 = [22, 33, 44] same=set(l1)&set(l2) print(same) #9、定义函数统计一个字符串中大写字母、小写字母、数字的个数,并以字典为结果返回给调用者 def num(ret): yy={‘大写‘:0,‘小写‘:0,‘数字‘:0} for i in ret: i = str(i) if i.isupper(): yy[‘大写‘]+=1 elif i.islower(): yy[‘小写‘]+=1 elif i.isdecimal(): yy[‘数字‘]+=1 return yy ret=input(‘>>>‘) print(num(ret) #11、检查代码,如有错误请改正(禁止运行代码): # #a. def func(x,y,z): print(x,y,z) func(1,2,3) # # 输出结果为:123 # # # # b. def func(x,z,y=5): print(x,y,z) func(1,3,3) # # 输出结果为:133 # # # # c. def func(x,y,*z): print(x,y,z) func(1,2,3,4,5,6) # # 输出结果为:12(3,4,5,6) # # # # d. def func(x,y,*z): print(x,y,z) func(1,2,3,4,5,6,6) # # 输出结果为:1 2(3,4,5,6,6) # # # # e. def func(x,*z,**y): print(x,y,z) func(1,2,3) # # 输出结果为:1{}(2,3) # # # # f. def func(x,*y,**z): print(x,y,z) func(1,name=2,age=3) # # 输出结果为:1(){"name":2,"age":3} # # # # g. def func(x,*y,**z): print(x,y,z) func(1,2,3,4, name=2,age=3) # # 输出结果为:1(2,3,4){"name":2,"age":3} # # # # h. def func(x=2,*y,**z): print(x,y,z) func(name=2,age=3) # # 输出结果为:2(){‘name‘:2,‘age‘:3} # 13、书写输出结果(禁止运行代码): # a. def func(*y,**z): print(y,z) func(1,2,3,4,5) # # 输出结果为:(1,2,3,4,5){} # # # # # # b. def func(*y,**z): print(y,z) func([1,2,3,4,5]) # # 输出结果为:([1,2,3,4,5]){} # # # # c. def func(*y,**z): print(y,z) func(*[1,2,3,4,5]) # # 输出结果为:(1,2,3,4,5){} # # # # d. def func(*y,**z): print(y,z) func(*[1,2,3,4,5],name="alex",age=19) # # 输出结果为:(1,2,3,4,5){"name":"alex","age":19} # # # # e. def func(*y,**z): print(y,z) func(*[1,2,3,4,5], { "name":"alex","age":19}) # # 输出结果为:(1,2,3,4,5,{ "name":"alex","age":19}){} # # # # f. def func(*y,**z): print(y,z) func(*[1,2,3,4,5], **{ "name":"alex","age":19}) # # 输出结果为:(1,2,3,4,5){ "name":"alex","age":19} # 14、书写执行结果(禁止运行代码) def func1(x=1,*y,**z): print(x,y,z) return y #结束的标志 print(x) def func2(arg): ret = func1(name=arg) print(ret) result = func2("Fuck") print(result) # 输出结果为:1(){"name":"fuck"} () None # 15、书写执行结果(禁止运行代码) def func(arg): arg.append(55) li = [11, 22, 33, 44] func(li) print(li) li = func(li) print(li) # 输出结果为:[11, 22, 33, 44,55] None # 16、书写执行结果(禁止运行代码) def f1(arg): print(arg+100) def f2(arg): ret = f1(arg+1) print(arg) print(ret) ret = f2(7) print(ret) # 输出结果为:108 7 None # 17、简述 Python3 中的 range 函数和 Python2.7 中的 range 函数有什么区别 # 3.x range 不会生成值 只有用的时候才会生成 封装成函数 迭代器 # 2.7 range 会直接生成一个列表,值已经生成 # # 18、书写执行结果(禁止运行代码) a = "oldboy %" print(a) # # 输出结果为:oldboy % b = "oldboy %d %%" % (12,) print(b) # 输出结果为:oldboy 12% # 20、书写执行结果(禁止运行代码) def func(a1): return a1 + 100 func = lambda a1: a1 + 200 ret = func(10) print(ret) # 输出结果为:210 #函数覆盖 #23、将字符串 "老男人" 转换成 UTF-8 编码的字节类型 name=‘老男人‘ print(bytes(name,encoding=‘utf-8‘)) print(name.encode(‘utf-8‘)) #24、利用内置函数将十进制数字 12,分别转换成 二进制、八进制、十六进制 表示的字符串 print(bin(12)) print(oct(12)) print(hex(12)) # 26、利用内置函数 zip(),实现功能 l1 = ["alex",22,33,44,55] l2 = ["is",22,33,44,55] l3 = ["good",22,33,44,55] l4 = ["guy",22,33,44,55] # # 请获取字符串 s = "alex_is_good_guy" print(‘_‘.join((list(zip(l1,l2,l3,l4))[0]))) # 27、判断输出结果是否相同?并可得到什么结论? def f1(arg): print(id(arg)) n = 1111111 print(id(n)) f1(n) # 输出的两个值是否相同: 相同 # 执行函数是调用变量值不是复制 # # 28、书写执行结果(禁止运行代码) # a. NAMELIST = ["alex", "eric"] def func(): NAMELIST = 123 func() print(NAMELIST) # 输出结果为:["alex", "eric"] # b. NAMELIST = ["alex", "eric"] def func(): global NAMELIST NAMELIST = 123 func() print(NAMELIST) # 输出结果为:123 # c. NAMELIST = ["alex", "eric"] def func(): NAMELIST.append("seven") func() print(NAMELIST) # 输出结果为:["alex", "eric","seven"] # d. NAMELIST = ["alex", "eric"] def func(): NAMELIST = 123 global NAMELIST func() print(NAMELIST) # 输出结果为:["alex", "eric"] # 29、书写执行结果(禁止运行代码) # a. name = "root" def func(): name = "seven" def outer(): name = "eric" def inner(): global name name = "蒙逼了吧..." print(name) print(name) ret = func() print(ret) print(name) # 输出结果为:seven None root # b. name = "root" def func(): name = "seven" def outer(): name = "eric" def inner(): global name name = "蒙逼了吧..." print(name) o = outer() print(o) print(name) ret = func() print(ret) print(name) # 输出结果为:eric None seven None root # c. name = "root" def func(): name = "seven" def outer(): name = "eric" def inner(): global name name = " ..." print(name) inner() o = outer() print(o) print(name) ret = func() print(ret) print(name) # 输出结果为:eric None seven None ... # d. name = "root" def func(): name = "seven" def outer(): name = "eric" def inner(): nonlocal name name = "蒙逼了吧..." print(name) inner() o = outer() print(o) print(name) ret = func() print(ret) print(name) # 输出结果为: # 30、书写执行结果并解释每一步操作 # a. name = "苍老师" def outer(func): name = ‘alex‘ func() #结果,就是执行的show def show(): print(name) outer(show) # 结果:苍老师 # # # b. name = "苍老师" def outer(): name = "波多" def inner(): print(name) return inner() ret = outer() print(ret) # 输出结果为:波多 None # c. name = "苍老师" def outer(): name = "波多" def inner(): print(name) return inner ret = outer() ret() print(ret) result = ret() print(result) # 输出结果为:波多 inne的内存地址 None # # # d. name = "苍老师" def outer(): name = "波多" def inner(): print(name) return inner #返回内存地址 ret = outer() print(ret) result = ret()#执行inner函数,打印波多 print(result)#inner无返回值 打印的是None # 输出结果为:inner的内存地址 波多 None # # e. name = "苍老师" def outer(func): def inner(): name = "李杰" func() return inner def show(): print(name)#打印全局的 outer(show)() # 输出结果为:苍老师 # # 31、书写执行结果并解释每一步操作 #a. def outer(func,z,y): func(z) def show(x): return x * x ret = outer(show,9,23) print(ret) # 输出结果为:None # # # c. def outer(func,z,y): return func(z) def show(x): return x * x ret = outer(show,9,23) print(ret) # 输出结果为:81 # def outer(func,z,y): func(z,y) f1 = lambda x,y: x + y ret = outer(f1,11,23) print(ret) # 输出结果为:None # # d. def outer(func,z,y): return func(z,y) f1 = lambda x,y: x + y ret = outer(f1,11,23) print(ret) # 输出结果为:34 # # 32、写输出结果 # a. def f5(arg): arg.append(‘偷到 500 万‘) def f4(arg): arg.append(‘开第四个门‘) f5(arg) arg.append(‘关第四个门‘) def f3(arg): arg.append(‘开第三个门‘) f4(arg) arg.append(‘关第三个门‘) def f2(arg): arg.append(‘开第二个门‘) f3(arg) arg.append(‘关第二个门‘) def f1(arg): arg.append(‘开一个门‘) f2(arg) arg.append(‘关一个门‘) user_list = [] result = f1(user_list) print(user_list) print(result) # # 输出结果为:[‘开一个门‘,‘开第二个门‘,‘开第三个门‘,‘开第四个门‘‘偷到 500 万‘,‘关第四个门‘,‘关第三个门‘,‘关第二个门‘,‘关一个门‘] None # # # # b. def f5(arg): arg = arg + 5 def f4(arg): arg = arg + 4 f5(arg) arg = arg + 4 def f3(arg): arg = arg + 3 f4(arg) arg = arg + 3 def f2(arg): arg = arg + 2 f3(arg) arg = arg + 2 def f1(arg): arg = arg + 1 f2(arg) arg = arg + 1 num = 1 result = f1(num) print(num) print(result) # 输出结果为:1 None # # c. def f5(arg): arg = arg + 5 return arg def f4(arg): arg = arg + 4 f5(arg) arg = arg + 4 return arg def f3(arg): arg = arg + 3 f4(arg) arg = arg + 3 return arg def f2(arg): arg = arg + 2 f3(arg) arg = arg + 2 return arg def f1(arg): arg = arg + 1 f2(arg) # 执行完f2(arg) 因为无法返回则为3 arg = arg + 1 return arg num = 1 result = f1(num) print(num) print(result) # 输出结果为 #33、利用递归实现 1*2*3*4*5*6*7=5040 #法一: from functools import reduce res=reduce(lambda x,y:x*y,[x for x in range(1,8)]) print(res) #法二: def func(x,y): y *= x x += 1 if x == 8: return y return func(x,y) res=func(2,1) print(res) # 34 a. 利用 filter、自定义函数 获取 l1 中元素大于 33 的所有元素 l1 = [11, 22, 33, 44, 55] l1 = [11, 22, 33, 44, 55] res=filter(lambda x:x > 33,l1) print(list(res)) # b.利用 filter、lambda 表达式 获取 l1 中元素小于 33 的所有元素 l1 = [11, 22, 33, 44, 55] l1 = [11, 22, 33, 44, 55] res=filter(lambda x:x < 33,l1) print(list(res)) # c.利用 map、自定义函数 将所有是奇数的元素加 100 l1 = [11, 22, 33, 44, 55] l1 = [11, 22, 33, 44, 55] def jsj(i): if i % 2 ==1: i = i + 100 return i else: return i print(list(map(jsj,l1))) # d.利用 map、lambda 表达式 将所有是偶数的元素加 100 l1 = [11, 22, 33, 44, 55] l1 = [11, 22, 33, 44, 55] res=map(lambda i:i+100 if i%2==0 else i,l1) print(list(res)) #36、第一个列表中的数字无序不重复排列,第二个列表为空列表 # 需求: # 取出第一个列表的最小值 放到第二个列表的首个位置, # 取出第一个列表的最小值(仅大于上一次的最小值) 放到第二个列表的首个位置, # 取出第一个列表的最小值(仅大于上一次的最小值) 放到第二个列表的首个位置, ... # 依此类推,从而获取一个有序的列表 l2,并将其返回给函数调用者。 l1 = [1,5,4,6,8,3,2,9,0] l2 = [] def func(la,lb): if len(la) ==0: return lb lb.insert(0,la.pop(la.index(min(la)))) ret = func(la,lb) return ret func(l1,l2) print(l2) #37、猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾就多吃了一个。第二天早上又将剩下的桃子吃了一半, # 还是不过瘾又多吃 了一个。以后每天都吃前一天剩下的一半再加一个。到第 10 天刚好剩一个。 # 问猴子第一天摘了多少个桃子?1534 n = 1 func =lambda x:(x+1)*2 for i in range(9): n =func(n) print(n)
原文:http://www.cnblogs.com/xyd134/p/6480938.html