Python变量赋值问题 [英] Python variable assignment question

查看:52
本文介绍了Python变量赋值问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

输出:

1
2
4
8
16
32

英雄:

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

他们不一样吗?我的意思是 a,b = b,a + b 与分别编写的 a = b b = a + b 相同-不?

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?

推荐答案

不,它们不相同.

当您编写 a,b = b,a + b 时,分配是同时"完成的. a,b = b,a + b (a,b)=(b,a + b)相同.所以,

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且b = 8.当Python看到这个

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

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

首先计算右侧 (b,a + b),即(8,13) ,然后分配(该元组)向左,移至(a,b).

当您具有: a = b 然后是 b = 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

首先计算右侧 b ,然后将(此值)分配给左侧,分配给 a .再来一次

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

b = a + b

首先计算右侧 a + b ,然后将(此值)分配给左侧,分配给 b.

这篇关于Python变量赋值问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆