what exactly happens internally in this python code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

24th Apr 2018, 1:23 AM
®️ahul Mathews™️
®️ahul Mathews™️ - avatar
8 Answers
+ 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..😂😂.
24th Apr 2018, 1:34 AM
Akib
Akib - avatar
+ 3
Akib Reza I know your code.But what abt my code in python?
24th Apr 2018, 1:36 AM
®️ahul Mathews™️
®️ahul Mathews™️ - avatar
+ 1
Jay Matthews how's it possible.please explain.
24th Apr 2018, 3:19 AM
®️ahul Mathews™️
®️ahul Mathews™️ - avatar
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)
24th Apr 2018, 1:32 AM
<i>TheOfficialOdyssey</i>
<i>TheOfficialOdyssey</i> - avatar
0
@akib Reza if that were the case then wouldn’t a= 12 b = -2 a= 10?
24th Apr 2018, 1:38 AM
<i>TheOfficialOdyssey</i>
<i>TheOfficialOdyssey</i> - avatar
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.
24th Apr 2018, 1:41 AM
Akib
Akib - avatar
0
leon santos a= 12 b= 12-b b= 12- 5 =7 a = 12- b a= 12-7=5
24th Apr 2018, 1:45 AM
Akib
Akib - avatar
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
24th Apr 2018, 1:46 AM
<i>TheOfficialOdyssey</i>
<i>TheOfficialOdyssey</i> - avatar