Integer vs string? | Sololearn: Learn to code for FREE!

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.

3/26/2019 5:18:59 AM

tristach605

4 Answers

New Answer

+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.

+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.

+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)?

+1

Thanks Anna for your very clear explanation👍🏻