Python示例程序

系统 1528 0

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元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论