习题

题目1:

# 用递归函数实现10的阶乘:10*9*8*7....*1
def fun(n):
    if n==1:
        return 1
    else:
        return n*fun(n-1)
print(fun(10))

 

题目2:

#F(0)=0,F(1)=1, 斐波纳契数列F(n)=F(n - 1)+F(n - 2)(n ≥ 2,n ∈ N*)
#0、1、1、2、3、5、8、13、21、34
def Fun(x):
   return  x if x <=1 else Fun(x-1) + Fun(x-2)
print(Fun(3))

 

 

 

题目2:

#1至100质数求和
list=[x for x in range(2, 101) if all([x%y!=0 for y in range(2, x)])]
print(sum(list))

 

 题目3:

#惰性函数
list =[m(2) for m in [lambda x : i * x for i in range(4) if i%2!=0]]
print(list)

 

题目4:

#函数filter过滤元素
li = [1,1,2,3,4,5,6,7,8,9]
li=list(filter(lambda x:x%2==1,li))

#函数remove删除元素
li = [1,1,2,3,4,5,6,7,8,9]
for i in li:
    if i%2!=0:
        li.remove(i)
print(li)

#注释
li = [1,1,2,3,4,5,6,7,8,9]
j=0
for i in li:
    print(li)
    print("取下标%d,值:%d"%(j,i))
    j = j+1
    if i%2!=0:
        print("删除:%d"%i)
        li.remove(i)
        print("================")
    else:
        print("不删除:%d"%i)
        print("================")
print("结果:",li)

out:
[1, 2, 4, 6, 8]
[1, 1, 2, 3, 4, 5, 6, 7, 8, 9]
取下标0,值:1
删除:1
================
[1, 2, 3, 4, 5, 6, 7, 8, 9]
取下标1,值:2
不删除:2
================
[1, 2, 3, 4, 5, 6, 7, 8, 9]
取下标2,值:3
删除:3
================
[1, 2, 4, 5, 6, 7, 8, 9]
取下标3,值:5
删除:5
================
[1, 2, 4, 6, 7, 8, 9]
取下标4,值:7
删除:7
================
[1, 2, 4, 6, 8, 9]
取下标5,值:9
删除:9
================
结果: [1, 2, 4, 6, 8]

 

题目5:

#try语句,先执行try再执行finally
#finally语句,不管是否return都会执行
#return语句,若异常体内已return则不执行,否则执行异常体外语句
#try和finally中都有return语句,return都执行只取最后一个return

import pysnooper
@pysnooper.snoop()
def getNum (a=0):
    try:
        print("try:",a)
        return a
    except:
        pass
    finally:
        a+=1
        print("finally:",a)
        return a
    print("!try:", a)
getNum()

 

 

题目6:

list = [(x-10 if x in [12,13] else x) for x in [1,12,13]]
print(list)

out:
[1, 2, 3]

 

题目7:

#[('*'*i).center(9,' ') for i in [1, 3, 5, 7, 9, 7, 5, 3, 1]]
list1 = [('*'*i).center(9,' ') for i in [i for i in range(1,10,2)]+[i for i in range(7,0,-2)]]

a = 'abecdefg'
b = 'ef'
c = {i:a[i:i+len(b)] for i in range(len(a)) if a[i:i+len(b)]==b}

 

 

 题目8:

def test():
    global a   #全局变量a,但为未定义
    a = 10     #定义

def test1():
    a = 20     #重新定义的a
    print(a)

def test2():
    print(a)   #全局变量a

if __name__ == '__main__':
    test()    #定义方法一定要执行
    test1()
    test2()

out :
    20
    10

 

 题目9:

a=10
b=20
c = a if a > b else b
e = a > b and a or b
f = [a,b][a>b]   #lis=[a,b]  lis[0]
print(c,e,f,[a,b][0])
print([a if a > 2 else 2 for a in range(11)])  #列表生成式

print(a and b)  #b  取决于b
print(0 and b)  #0  取决于0
print(a or b)   #a  取决于a
print(0 or b)   #b  取决于b

 

题目10:

#批量+10
dict1 = {"a":1,"b":2,"c":3}
print({k:v+10 for k,v in dict1.items()})

#按key排序
dict2 = {"a":1,"d":0,"c":3,"b":2}
print({k:dict2[k] for k in sorted(dict2)})

#按velue排序
list1 = [{"name":"t1","value":15},{"name":"t2","value":8},{"name":"t3","value":9},{"name":"t4","value":30},{"name":"t5","value":2}]
print(sorted(list1,key=lambda x: x["value"]))

 

题目11:

#判断key是否在这个字典
dict = {'key':None}

if 'key' in dict:
    print(123)

if dict.get('key'):
    print(456)

if dict['key1']:
    print(789)

 

posted @ 2019-07-20 10:46  南方的墙  阅读(193)  评论(0编辑  收藏  举报