Python变量赋值问题
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 = b
和 b = 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不,它们不一样。
当您编写
a,b = b, a+b
时,分配是“同时”完成的。a,b = b, a+b
与(a, b) = (b, a+b)
相同。所以,在a=5和b=8之后。当Python看到这个时
,它首先计算右侧
(b, a+b)
,即(8,13)
,然后将(此元组)分配给左侧,即(a,b)
。当您先有:
a = b
,然后是b = a+b
,这两个操作将依次完成。但对于它们中的每一个:它首先计算右侧
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, aftera=5 and b=8. When Python sees this
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 thenb = a+b
, the two operations are done one after the other. But for every one of them:It first calculates the right side
b
and then assigns (this value) to the left side, toa
. And againIt first calculates the right side
a + b
and then assigns (this value) to the left side, tob
.它们不一样。在第一个示例中,为
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 ofb
and then its new value is added tob
. Effectivelyb + b
.In the second example, a is assigned the current value of
b
andb
is assigned the current value ofa
plus the current value ofb
. It happens before the value ofa
changes.The two assignments happen simultaneously rather than sequentially.
这些说法各有不同。
修改
a
,然后使用修改后的值更改b
。实际上,它总是b = b + b
。b
和a
在同一时刻发生变化,因此b
是使用原始a
值计算的。These statements are different.
modifies
a
, and then uses modified value to changeb
. Actually, it always doesb = b + b
.changes
b
anda
in the same moment, sob
is calculated using originala
value.为此:
首先评估右侧的所有内容,然后分配给左侧。因此,在左侧的 a 赋值更改它之前,您正在使用右侧的
a
值。为此:
在执行第二条语句之前更改
a
的值。所以你的结果是不同的。For this:
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:
the value of
a
is changed before the second statement is executed. So your results are different.第二个示例表示用于执行“同时”分配的元组。
元组是不可变的,这意味着它们的内容一旦设置就无法更改。此外,Python 从右到左处理赋值。因此,当创建右侧的元组时,即使 A & 的值也无法更改。 B 在左侧被分配新值。
The second example represents a tuple being used to perform "simultaneous" assignments.
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.
如何同时完成两个赋值操作:
a,b = b,a+b
:首先在后台,我认为 python 将a+b
赋给一个变量让我们称之为x
并将b
分配给一个变量让我们称之为y
然后,将x
分配给b
和y
到一个
。我认为“同时”的概念从逻辑上来说是不正确的,必须先改变内存中
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 assignsa+b
to a variable let us call itx
and assignsb
to a variable let us call ity
then, assignsx
tob
andy
toa
.I think the concept of "simultaneously" is not true by logic, the value of
a
in memory must be changed first, thenb
or vice versa, so the value ofa
still depends onb
or vice versa unless there is another variables to hold the new values as above.