Python变量赋值问题

发布于 2024-11-17 09:09:08 字数 418 浏览 2 评论 0原文

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

输出:

1
2
4
8
16
32

wheras:

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

输出(正确的斐波那契数列):

1
1
2
3
5
8
13
21
34

它们不是相同的吗?我的意思是 a,b = b, a+b 本质上与单独编写的 a = bb = a+b 相同 -不?

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

outputs:

1
2
4
8
16
32

wheras:

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

outputs (correct fibonacci sequence):

1
1
2
3
5
8
13
21
34

Aren't they the same? I mean a,b = b, a+b is essentially the same as a = b and b = a+b written separately -- no?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

又怨 2024-11-24 09:09:08

不,它们不一样。

当您编写 a,b = b, a+b 时,分配是“同时”完成的。 a,b = b, a+b(a, b) = (b, a+b) 相同。所以,在

a, b = 5, 8

a=5和b=8之后。当Python看到这个时

(a, b) = (b, a+b)

,它首先计算右侧(b, a+b),即(8,13),然后将(此元组)分配给左侧,即(a,b)


当您先有:a = b,然后是b = a+b,这两个操作将依次完成。但对于它们中的每一个:

a = b

首先计算右侧 b 然后将(这个值)分配给左侧一个。再次,

b = a + b

首先计算右侧 a + b 然后将(此值)分配给左侧,即b< /代码>。

No, they are not the same.

When you write a,b = b, a+b , the assignments are done "simultaneously". a,b = b, a+b is same as (a, b) = (b, a+b). So, after

a, b = 5, 8

a=5 and b=8. When Python sees this

(a, b) = (b, a+b)

it first calculates the right side (b, a+b) which is (8,13) and then assigns (this tuple) to the left side, to (a,b).


When you have: a = b and then b = a+b, the two operations are done one after the other. But for every one of them:

a = b

It first calculates the right side b and then assigns (this value) to the left side, to a. And again

b = a + b

It first calculates the right side a + b and then assigns (this value) to the left side, to b.

乖乖 2024-11-24 09:09:08

它们不一样。在第一个示例中,为 a 分配了 b 的值,然后将其新值添加到 b 中。实际上是b + b

在第二个示例中,a 被分配了 b 的当前值,b 被分配了 a 的当前值加上 b 的当前值>b。它发生在 a 值更改之前。

这两个任务同时发生,而不是顺序发生。

They are not the same. In the first example, a is assigned the value of b and then its new value is added to b. Effectively b + b.

In the second example, a is assigned the current value of b and b is assigned the current value of a plus the current value of b. It happens before the value of a changes.

The two assignments happen simultaneously rather than sequentially.

小伙你站住 2024-11-24 09:09:08

这些说法各有不同。

a = b
b = a+b

修改a,然后使用修改后的值更改b。实际上,它总是b = b + b

a,b = b, a+b

ba 在同一时刻发生变化,因此 b 是使用原始 a 值计算的。

These statements are different.

a = b
b = a+b

modifies a, and then uses modified value to change b. Actually, it always does b = b + b.

a,b = b, a+b

changes b and a in the same moment, so b is calculated using original a value.

┊风居住的梦幻卍 2024-11-24 09:09:08

为此:

a,b = b, a+b

首先评估右侧的所有内容,然后分配给左侧。因此,在左侧的 a 赋值更改它之前,您正在使用右侧的 a 值。

为此:

a = b
b = a+b

在执行第二条语句之前更改a的值。所以你的结果是不同的。

For this:

a,b = b, a+b

everything on the right side is evaluated first and then assigned to the left side. So you are using the value of a on the right side before the assignment of a on the left side changes it.

For this:

a = b
b = a+b

the value of ais changed before the second statement is executed. So your results are different.

£噩梦荏苒 2024-11-24 09:09:08

第二个示例表示用于执行“同时”分配的元组。

(A,B)=(B,A+B)

元组是不可变的,这意味着它们的内容一旦设置就无法更改。此外,Python 从右到左处理赋值。因此,当创建右侧的元组时,即使 A & 的值也无法更改。 B 在左侧被分配新值。

The second example represents a tuple being used to perform "simultaneous" assignments.

(A,B)=(B,A+B)

Tuples are immutable, meaning that their contents cannot be changed once set. Also, Python handles assignments from right to left. So when the tuple on the right is created, the values cannot change even though A & B are assigned new values on the left.

十雾 2024-11-24 09:09:08

如何同时完成两个赋值操作:

a,b = b,a+b:首先在后台,我认为 python 将 a+b 赋给一个变量让我们称之为x并将b分配给一个变量让我们称之为y然后,将x分配给by一个

我认为“同时”的概念从逻辑上来说是不正确的,必须先改变内存中a的值,然后改变b,反之亦然,所以<的值code>a 仍然依赖于 b ,反之亦然,除非有另一个变量来保存上面的新值。

How two assignment operations done in the same time:

a,b = b,a+b: first in background, I think python assigns a+b to a variable let us call it x and assigns b to a variable let us call it y then, assigns x to b and y to a.

I think the concept of "simultaneously" is not true by logic, the value of a in memory must be changed first, then b or vice versa, so the value of a still depends on b or vice versa unless there is another variables to hold the new values as above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文