0

Integer vs string?

What is the output? x=10 Y=x+2 int(str(y)+"2") print(y) Solution: 12 x equals 10 so: y equals 12 (x+2) Shouldn't "(int(str(y)+"2") change the number 12 into a string? And as such, shouldn't the answer be "12" + "2" or "122". Thanks for any help anyone could provide.

26th Mar 2019, 5:18 AM
tristach605
tristach605 - avatar
4 Réponses
+ 7
The result is 12 because the statement int(str(y) +"2") doesn't do anything. Change it to y = int(str(y) +"2") to store the result of the operation in y and the result will be 122.
26th Mar 2019, 6:05 AM
Anna
Anna - avatar
+ 7
Well, it actually does calculate 122, but that value isn't saved anywhere. See this example: i = 5 i + 9 print(i) #5 The second line tells python to calculate 5+9 and that is what python will do. But the result is immediately discarded because it isn't saved anywhere. If you do this: i = 5 i = i + 9 print(i) #14 , the value is actually assigned to the variable i.
26th Mar 2019, 11:01 PM
Anna
Anna - avatar
+ 2
Thanks Anna! Any idea on why it doesnt do anything? Shouldnt the int(str(y)+"2") then become the new value of y (which is asked to print)?
26th Mar 2019, 10:32 PM
tristach605
tristach605 - avatar
+ 1
Thanks Anna for your very clear explanation👍🏻
27th Mar 2019, 2:04 AM
tristach605
tristach605 - avatar