+ 1
what exactly happens internally in this python code?
The code is a=7 b=5 a,b=b,a print(a,b) I know the above code swaps the numbers.But what is happening behind the operation internallly?
8 odpowiedzi
+ 3
a=a+b
b=a-b
a=a-b
does that make any sense?
idk if thats what you want to know. i saw something like that in a meme..😂😂.
+ 1
Jay Matthews how's it possible.please explain.
0
7,5 = 5,7
And I don’t know if a,b is a proper way to write that because I don’t program in python but yeah the simple answer to this is 7,5 = 5,7 (a,b = b,a)
0
@akib Reza if that were the case then wouldn’t
a= 12
b = -2
a= 10?
0
Its just swapping integers what the problem in that?
In c there is pointers which points to the memory address of a variable. In you example values in the memory address is swapped.
its just saying
set a= the value of b
and set b = the value of a
you need to store the value in a temp variable or do what i did in the previous answer.
0
leon santos
a= 12
b= 12-b
b= 12- 5 =7
a = 12- b
a= 12-7=5
0
I wasn’t saying it was a problem I just didn’t understand it because In js it replaces previous variables for example if a=2
B= 2+ a
Then a = b
the value of b would be 4 because (a)2+ 2(a) = 4 that’s why I thought it was 10