1.怎么理解递归函数?
2.什么是栈帧空间?
3.怎么理解递归函数的执行过程?
4.如何使用递归函数实现求任意数n的阶乘?
5.如何使用递归函数实现斐波那契?
6.什么是尾递归?
7.math模块的几种常用方法都是什么效果?
ceil()\floor()\pow()\sqrt()\fabs()\modf()\copysign()\fsum()\pi
8.random模块的几种常用方法都是什么效果?
random()\randrange()\randint()\uniform()\choice()\sample()\shuffle()
9.如何自定义choice函数?
10.如何实现随机验证码小程序?
1.递归函数就是自己调用自己的函数,有去有回。
2.函数运行时在内存里开辟的空间,递归过程就是开辟和释放栈帧空间的过程,但空间始终有限,注意溢出。
3.类似于俄罗斯套娃吧,想在最里层的套娃拿东西的过程。
4.实现阶乘:
def jiecheng(n):
if n <= 1:
return 1
return n * jiecheng(n-1)
res = jiecheng(5)
print(res)
5.实现斐波那契:
def feb(n):
if n ==1 or n == 2:
return 1
return feb(n-1) + feb(n-2)
res = feb(5)
print(res)
6.尾递归就是无论调用多少次函数,都只占一份空间,感觉像是把那些小套娃都放在大套娃的壳里,始终只占一份空间。
7.math模块
res = math.ceil(3.01)
print(res)
res = math.floor(4.1)
print(res)
res = math.pow(2,3)
print(res)
res = math.sqrt(9)
print(res)
res = math.fabs(-9)
print(res)
res = math.modf(-19.8)
print(res)
res = math.copysign(-90,-18)
print(res)
lst = [1,2,3,4]
res = math.fsum(lst)
print(res)
res = math.pi
print(res)
8.random模块
res = random.random()
print(res)
res = random.randrange(3)
res = random.randrange(3,7)
res = random.randrange(3,10,2)
print(res)
res = random.randint(1,3)
print(res)
res = random.uniform(1,3)
res = random.uniform(3,1)
print(res)
lst = ["周杰伦","蔡徐坤","易烊千玺","神秘boy"]
res = random.choice(lst)
print(res)
def mychoice(lst):
num = random.randrange(len(lst))
return lst[num]
print(mychoice(lst))
mychoice = lambda lst : lst[random.randrange(len(lst))]
print(mychoice(lst))
lst = ["周杰伦","蔡徐坤","易烊千玺","神秘boy"]
res = random.sample(lst,2)
print(res)
lst = ["周杰伦","蔡徐坤","易烊千玺","神秘boy"]
random.shuffle(lst)
print(lst)
10.随机验证码小程序
def yanzhengma():
strvar = ""
for i in range(4):
b_word = chr( random.randrange(65,91) )
s_word = chr( random.randrange(97,123) )
num = str( random.randrange(10) )
lst = [b_word,s_word,num]
strvar += random.choice(lst)
return strvar
res = yanzhengma()
print(res)