0
Help me there..... It seems difficult to me.How does it work? Anyone explain it easily please. Here is the code below:
a = 5 b = 7 print('a, b', a, b) # swap these two a = a + b b = a - b a = a - b print('a, b', a, b)
3 odpowiedzi
+ 6
See I will try to na u understand likewise:
1) firstly u gave 5 as a value to (a).
2) Then u gave 7 as a value to (b).
3) Then u used print statement to get the output as (a,b,5,7).
Here as first a and b are in the form of string so they will be printed as they are but after that u wrote (a,b) this will output the value which u gave them.
4) This is the comment.
5) Now u gave a as the value of a + b ( means 5+7) . Because of which now the value of a will be 12.
6) Now u gave b as the value of a - b ( means 12-7) . Because of which now the value of a will be 5.
7) Now u gave a as the value of a - b ( means 12-5) . Because of which now the value of a will be 7.
8)Then u used print statement to get the output as (a,b,7,5).
Now u can see that the values of a and b are interchanged.
Maybe this will help u out.
+ 1
Do the math line by line.
There is nothing to be surprised.
Use the previous line value in the next line.
+ 1
For swapping values:
a,b = b,a