斐波那契数列
方法一(递归)
python">def f(a):if a==1:return 1elif a==2:return 1else:return f(a-1)+f(a-2)
print(f(3))
方法二(非递归)
python">n=int(input())
lst=[1,1]
for i in range(2,n+1):lst.append(lst[i-1]+lst[i-2])
print(lst[n-1])
列表的复制
这样赋值改变list1也会改变list2,实际上等同于两个指针指向相同的内存地址
python">list1=[1,2,3,4]
list2=list1
print(list2)
list1[1]=1
print(list2)
结果
[1, 2, 3, 4]
[1, 1, 3, 4]
使用copy库里的deepcopy实现深拷贝
python">import copy
list1=[1,2,3,4]
list2=copy.deepcopy(list1)
print(list2)
list1[1]=1
print(list2)
结果
[1, 2, 3, 4]
[1, 2, 3, 4]
暂停后输出
time库里的sleep方法,实现暂停后输出,单位是秒
python">import time
time.sleep(15)
print("hello world")
成绩评级
python">score=int(input())
if score>=90:print("A")
elif 60 <= score <=89:print("B")
else:print("C")
统计字符
python">string=input()
char=0
num=0
space=0
other=0
for i in string:if i.isalpha():char+=1elif i.isdigit():num+=1elif i.isspace():space+=1else:other+=1
print(f"字母有{char}个,数字有{num}个,空格有{space}个,其他字符有{other}个")