斐波那契数列python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18009817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:42:38  来源:igfitidea点击:

Fibonacci sequence python

pythonloopspython-3.xiteratoriterable-unpacking

提问by eLg

I am trying to understand Python, but I still don't get it. I am new to the language, and wants to understand it properly. This is a line from a Fibonacci sequence using loops. Please explain the meaning of this code. I am trying to get the pattern by hand. I got the pattern up to 3, but after 3 I am not getting the answer.

我正在尝试了解 Python,但我仍然不明白。我是这门语言的新手,想正确理解它。这是使用循环的斐波那契数列中的一条线。请解释这段代码的含义。我正在尝试手动获取模式。我得到了最多 3 个模式,但是在 3 个之后我没有得到答案。

a, b = 0, 1
while b < 50:
    print(b)
    a, b = b, a + b

采纳答案by paxdiablo

a, b = b, a + b

This is known as multiple assignment. It's basically an atomicversion of:

这称为多重分配。它基本上是一个原子版本:

a = b
b = a + b

By atomic, I mean everything on the right is calculated beforepacing it into the variables on the left. So abecomes band bbecomes the oldversion of aplus b, equivalent to the non-atomic:

通过原子,我的意思是右边的所有内容都是在将其调入左边的变量之前计算出来的。所以a变成bb变成了plus的版本,相当于非原子的:ab

old_a = a
a = b
b = old_a + b

So, in terms of what you see:

所以,就你所看到的而言:

        a                        b               output
================    =========================    ======
(initial values)        (initial values)
        0                        1                  1
(becomes prev b)    (becomes sum of prev a,b)
        1                        1                  1
        1                        2                  2
        2                        3                  3
        3                        5                  5
        5                        8                  8

That exact code (along with the explanation of multiple assignment) can be found herein the tutorial.

这确切的代码(有多种布局的解释和说明),可以发现这里的教程。

回答by falsetru

It's multiple assigment (or tuple unpacking).

这是多重分配(或元组解包)。

According to Python Tutorial:

根据Python 教程

>>> # Fibonacci series:
... # the sum of two elements defines the next
... a, b = 0, 1
>>> while b < 10:
...     print(b)
...     a, b = b, a+b
...
1
1
2
3
5
8

This example introduces several new features.

The first line contains a multiple assignment: the variables a and b simultaneously get the new values 0 and 1. On the last line this is used again, demonstrating that the expressions on the right-hand side are all evaluated first before any of the assignments take place. The right-hand side expressions are evaluated from the left to the right.

此示例介绍了几个新功能。

第一行包含一个多重赋值:变量 a 和 b 同时获得新值 0 和 1。在最后一行再次使用它,证明右侧的表达式在任何赋值之前都首先被评估发生。右边的表达式是从左到右计算的。

回答by the_marcelo_r

How about multiple answers?

多个答案怎么样?

def fib(num):
    a = 0
    b = 1
    while b <= num:
        prev_a = a
        a = b
        b = prev_a +b
        #print b                                                                                                          
    return a

print fib(13)

def pythonic_fib(num):
    a,b = 0,1
    while b <= num:
        a,b = b, a+b
    return a

print pythonic_fib(13)

def recursive_fib(num, a, b):
    if (b >= num):
        return b
    else:
        return recursive_fib(num, b, a+b)

print recursive_fib(13, 0, 1)

回答by Hamza Akbar

#The easy way to generate Fibonacci series in python is 
user = input('Please enter the integer range for Fibonacci series: '); # take input from user form the range of Fibonacci series.
try:# to ignore wrong inputs and be aware from error we use try and except block
    d=int(user);# converts the user input to type integer.
    a=0; #initialization``
    b=1; #initialization
    print(a,b,end=' '); # print initial value of a and b
    for c in range(0,d): # here c is iterable variable and in range o is the starting point and d is the ending range which we get from user
        c=a+b;
        a=b;
        b=c;
        print(c,end=' ');

except Exception as e:
    print(e); # if any error occurred in the input here it shows which error occurs

回答by sethuramalingam

a= int(input("Enter the first number:"));
Enter the first number:0
b= int(input("enter the second number:"));
enter the second number:1
n= int (input("enter the number of terms:"));
enter the number of terms:10
print(a,b);
0 1
while(n>2):
      c=a+b;
      a=b;
      b=c;
      print(c);
      n=n-1;


1
2
3
5
8
13
21
34

回答by samman

I know this is an old question, but just thought I'd through in my 2-cents since a lot of these seem a bit overly complicated for just the fibonacci sequence (outside the given answer), in case someone was still looking. You could do this:

我知道这是一个老问题,但只是想我已经用了 2 美分,因为其中很多对于斐波那契数列(在给定答案之外)来说似乎有点过于复杂,以防有人仍在寻找。你可以这样做:

a=1
b=0
while b<400:
    a=a+b
    b=a+b
    print(a)
    print(b)

This will give the desired output of the sequence(to whatever you set b less than).

这将提供所需的序列输出(对于您设置的 b 小于)。