What's the difference between these 2 codes? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What's the difference between these 2 codes?

https://code.sololearn.com/cIs6vu8nkVyu/?ref=app

2nd Apr 2018, 9:35 PM
Wasiul Islam Sakif
Wasiul Islam Sakif - avatar
3 Answers
+ 1
The first line: print(int("2") + int("3")) converts a string "2" and another string "3" both to integers, these are then added together to give an integer of 5. The print() function then converts the integer 5 to a string "5". Second line: print(2+3) The 2 and 3 are integer types. They are added together to give an integer of value 5. The print() function then converts the integer 5 to a string "5".
2nd Apr 2018, 9:56 PM
Emma
+ 9
the int method returned an integer object from the given number or string,  Internally, int() method calls an object's __int__() method. So, even if an object isn't a number, you can convert the object into an integer object. so the first line print the sum of two integer and result as 5 and second line is simply add of two number which return 5 so there is no difference
2nd Apr 2018, 9:52 PM
GAWEN STEASY
GAWEN STEASY - avatar
+ 1
On the first line you have 2 and 3 as Strings. Stuff in between quotes or double quotes are of the String data type. So in order to add them as integer you have to convert to int them using int(...) On the second line 2 and 3 are already integer so no need for any conversion. On line 1, if you added them as they were, meaning "2" + "3", you would have ended up with 23 instead of 5.
2nd Apr 2018, 9:55 PM
cyk
cyk - avatar