Python代码不了解。逐步说明此代码
a,b=1,2
a,b=b,a=a,b
print(a,b)
# 2 1
如果有人可以通过行解释此代码,请帮助我
a,b=1,2
a,b=b,a=a,b
print(a,b)
# 2 1
If someone could give me a line by line explanation of this code, help me plz
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为互换会像
a,b = b,a
一样发生。但是,第2行上没有交换。转换行2如下。就像将值分配给变量一样。
它等于a = b = c = 1。
I thought the swap would happen like
a,b=b,a
. but, no swap occurs on line 2. Converting line 2 is as follows.It's just like assigning a value to a variable.
It is equal to a=b=c=1.
当然!因此,第1行:
这基本上创建了两个变量A和B,并在从左到右的相等符号的另一侧分配值。因此,a = 1,b = 2。
第2行基本上交换了周围的所有内容。如我所见,每个数量(例如a,b或a,a)的制造与其他东西相等。例如,如果a,b = 1,2并且您说a,b = b,a基本上是在说a,b = 2,1,那么a变为2,b变为1。当您打印时,这就是您得到。
Sure! So, line 1:
This basically creates two variables, a and b, and assigns them values on the other side of the equal sign from left to right. So, a = 1, and b = 2.
Line 2 basically swaps everything around. As I see it, each quantity (e.g. a,b or b,a) is being made equal to something else. For example if a,b = 1,2 and you say a,b = b,a you're basically saying a,b = 2,1 so a becomes 2, and b becomes 1. When you print it, that's what you get.