1.写函数,传入n个数,返回字典{‘max’:最大值,’min’:最小值}
例如:min_max(2,5,7,8,4) 返回:{‘max’:8,’min’:2}(此题用到max(),min()内置函数)
def MaxMin(*args):
dic = dict()
dic['max']=max(args)
dic['min']=min(args)
return dic
MaxMin(1,2,3,4,5,6,7)
2.写函数,传入一个参数n,返回n的阶乘
例如:cal(7) 计算7*6*5*4*3*2*1
def call(num):
result = 1
for n in range(num,0,-1):
result *= n
return result
call(10)
3.写函数,返回一个扑克牌列表,里面有52项,每一项是一个元组
例如:[(‘红心’,2),(‘草花’,2), …(‘黑桃’,‘A’)]
def Poker():
kind = ['黑桃','红桃','梅花','方块']
Num = ['A',2,3,4,5,6,7,8,9,10,'J','Q','K']
poker = list()
for a in Num:
for b in kind:
poker.append((b,a))
return poker
print(Poker())
print(len(Poker()))
4. 相关面试题(先从纸上写好答案,然后在运行):
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__
下面代码打印的结果分别是
__list1=[10,'a']__,__list2=[123]__,__list3=[10,'a']__.
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)
写代码完成99乘法表.(升级题)
1 * 1 = 1
2 * 1 = 2 2 * 2 = 4
3 * 1 = 3 3 * 2 = 6 3 * 3 = 9
......
9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81
'''
1 * 1 = 1
2 * 1 = 2 2 * 2 = 4
3 * 1 = 3 3 * 2 = 6 3 * 3 = 9
4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16
'''
for n in range(1,10):
for i in range(1,n+1):
print(f'{n} * {i} = {n*i}',end=' ')
if i == n : print('\n')
作业写完了的同学继续完善昨天的作业。
明日默写内容:
- 什么是闭包。
- 迭代器的特性。
- 用while循环模拟for循环(写具体代码)。