python20181211

16.请问这个函数有多少个参数?

def MyFun((x,y), (a, b)):

      return x * y - a * b

零个参数

17.编写一个函数满足以下功能:

计算打印所有参数的和乘以基数(base=3)的结果

def sumn(*n):
    s = 0
    for i in n:
        s += i
    return s*3
print(sumn(1,2,3))

1.下面代码会输出什么:

def f(x,l=[]):

    for in range(x):
        l.append(i*i)
    print l
 
f(2)
f(3,[3,2,1])
f(3)
[0,1]
[3,2,1,0,1,4]
[0,1,0,1,4]

2.列表的sort方法和sorted的区别是什么?试举例说明?

sort()是直接修改序列
sorted()是排序之后的原本的序列没有变
s = [3,2,1,4,5]
s.sort()
print(s)
[1, 2, 3, 4, 5]

s = [3,2,1,4,5]
print(sorted(s))
print(s)
[1, 2, 3, 4, 5]
[3, 2, 1, 4, 5]

3.尝试用两个方式,分别用一行语句实现去除了中的相同项,将结果存放在l2列表中

l1 = ['b','c','d','b','c','a','a']
l2 = []

l1 = ['b','c','d','b','c','a','a']
l2 = []

l2 = list(set(l1))
print(l2)

n = [l2.append(i) for i in l1 if i not in l2]
print(l2)

5.现有一个由100以内所有偶数组成的列表,将列表中所有能被4和6整除的数去除

s = [0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,
52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98,100]
s = [i for i in s if i % 4 != 0 and i % 6 != 0] print(s)

posted on 2018-12-11 19:02  冰之冥  阅读(136)  评论(0编辑  收藏  举报

导航