1.写函数, 传入函数中多个实参(均为可迭代对象如字符串, 列表, 元祖, 集合等),将每个实参的每个元素依次添加到函数的动态参数args里面.例如传入函数两个参数[1, 2, 3](22, 33)最终args为(1, 2, 3, 22, 33)

 

def func(*args):
    return args
print(func(*[1, 2, 3],*(22, 33),*{10,20,30}))

 

2.写函数, 传入函数中多个实参(实参均为字典), 将每个实参的键值对依次添加到函数的动态参数kwargs里面.

例如传入函数两个参数 {‘name’:’alex’} {‘age’:1000},最终kwargs为{‘name’:’alex’, ‘age’:1000}

 def func(**kwargs):
     return kwargs
 s=func(**{"name":'alex'},**{'age':1000},**{"gender":""})
 print(s)

3.写函数, 接收一个参数(此参数类型必须是可迭代对象), 将可迭代对象的每个元素以’_’相连接, 形成新的字符串, 并返回.

例如传入的可迭代对象为[1, '老男孩', '武sir'] 返回的结果为  '1_老男孩_武sir’
def func(ar):
    s = ""
    for el in ar:        
        s += str(el)+"_"
    return s[:-1]
c=func([1, '老男孩', '武sir'])
print(c)

或者

def func(it):
    for i in range(len(it)):
        it[i] = str(it[i])
    return "_".join(it)   #先把it里面的元素全部转化为str,在用join拼接
print(func([1,"alex","wusiar"]))
print("alex"+213)

4.写函数,传入一个参数n,返回n的阶乘 即7!=7*6*5*4*3*2*1

def factoria(a):
    s=1
    for i in range(1,a+1):
        s *= i
    return s
print(factoria(7))

5.写函数,返回一个扑克牌列表,里面有52项,每一项是一个元组

例如:[(‘红心’,2), (‘草花’,2), …(‘黑桃’,‘A’)]
def poker():
    li=[]
    for i in [2,3,4,5,6,7,8,9,10,"J","Q","K","A"]:
        for j in ["红心","梅花","方块","黑桃"]:
            li.append((j, i))
    return li
poker()

6.相关面试题(先从纸上写好答案,然后在运行):

 (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 关键字参数在后面

      (2).下面代码打印的结果分别是______

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)

  (3). 写代码完成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

 

for i in range(1, 10):
    for j in range(1, i+1):
        print("%s * %s = %s" % (i, j, i*j), end=" ") #print默认以\n结尾,end=" ",相当于修改了默认值,每次打印以空格结束
    print() #换行