Recursive articles(遞歸篇)
Feibo sequence
def fib(number):
if number==0 or number == 1:
return number
else:
return fib(number-1) + fib(number-2)
for i in range(8):
print("fib(%2d) = %2d" % (i,fib(i)))
We can control the time of the function.
def fib(number):
if number==0 or number == 1:
return number
else:
return fib(number-1) + fib(number-2)
while True:
a = int(input("How many do you want?"
"\nThis integer must be > 0"
"Please enter your integer:"))
if a < 0:
print("The integer of you enter is wrong,please enter again!\n")
continue
for i in range(a+1):
print("fib(%d) = %2d" % (i,fib(i)))
print()
List's using methods.
# creating an empty list
list = []
# add new value to the list
for number in range(1,11):
list += [ number ]
print(list)
print()
# len(list) is the method to get the length of the list
print("There are",len(list),"elements in the list")
print()
# print the values in list
i = 0
for item in list:
print("list[%d] is %d"%(i,item))
i += 1
print()
# change the value in list
list[0] = 100
# if the index lower than 0
# it will Count from the right
# this is a logical error that is not serious
# better never use it!!!!!
list[-3] = 999
print(list)
Dictionary
# creating empty dictionary
empty_dictionary={"name":"No"}
# creating another dictionary
grade={"name":"Sabo",
"grade":"Good!",
}
# output the grade
print("grade dictionary is")
print(grade)
print()
# add to our empty dictionary
print("before add grade")
print("empty_dictionary is ")
print(empty_dictionary)
print()
print("after add grade")
print("empty_dictionary is ")
empty_dictionary.update(grade)
print(empty_dictionary)
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
