1.is和==的区别?(回答比较粗暴,建议具体百度,原理还是很重要的)
e.g.:a={1} b={1}
e.g.:c=1 d=1
==:值的比较,a==b
is:对于int/sring来说,与==作用相似
对于list/dict/tuple/set类型来说,a is not b,c is d
2.写一个斐波那契数列递归(我用了最原始的方法,但是不符合递归的题意。)
我的回答:
deffib(n):
a =0
b =1for i inrange(n):
a, b = b, a+b
print(a)
3.写一个99乘法表(就两个for循环)
for i inrange(1,10):for j inrange(1,i+1):print(str(i)+"*"+str(j)+"="+str(i*j))
4.迭代器和生成器
迭代器:一个可以记住遍历位置的对象
a =iter(list)#创建迭代对象 next(a)#用法1
a =iter(list)#创建迭代对象 for x in a:#用法2print(a)
生成器:使用了 yield 的函数被称为生成器?
网上抄的斐波那契数列,使用了生成器和迭代器
deffibonacci(n):# 生成器函数 - 斐波那契
a, b, counter =0,1,0whileTrue:if(counter > n):returnyield a
a, b = b, a + b
counter +=1
f = fibonacci(10)# f 是一个迭代器,由生成器返回生成