Fibonacci sequence using list in PYTHON?

前端 未结 6 1442
清酒与你
清酒与你 2020-12-16 08:48

I have a problem about making a fibonacci sequence to a list, I\'m just new to python someone help me please.

This is my code. I know this is looking wrong or somet

6条回答
  •  难免孤独
    2020-12-16 09:08

    You can use below code, your problem can be solve in just one line. what are we doing is appending the

    fib_nums

    till your given limit i.e.,

    700

    and then adding the

    fib_nums

    with the second list which is of zero length which explicitly we doing, because it is containing

    None

    values, that we don't required.

    #defining variable
    fib_nums = [0, 1] 
    
    #just one line code
    fib_nums = fib_nums + [fib_nums.append(fib_nums[i-1]+fib_nums[i-2]) for i in range(2,700)]*0
    
    #printing the series
    print (fib_nums)
    

提交回复
热议问题