I have a question about python.
I'm a beginner, so please take good care of me.
On the code, it says:
I don't understand the meaning, but could you tell me?
I don't understand the last "A,B=B,A" part.
What does this line mean with two variables on each side of an equal?
I look forward to your kind cooperation.
X=40
Y = 30
A=zeros(X,Y), "float64")
B=zeros(X,Y), "float64")
A, B = B, A
A,B=B,A
replaces the contents of the two variables A
,B
.It is called multiple assignments, and can be interpreted as doing B
values in A
and A
values in B
at the same time.Before and after this substitution, A
, B
content print
is easy to understand.
In non-Python languages, it is common to have a third temporary variable ready to replace two variables (so-called swap operations):
tmp=B
B = A
A = tmp
Python allows you to write something similar in one line.
A, B = B, A
More precisely, this substitution is achieved by unpacking the tuple and sequence.In other words, the right side is interpreted as a tuple (B,A)
, and then the unpacking operation is performed, resulting in multiple substitutions.
© 2024 OneMinuteCode. All rights reserved.