Why not 2+3 instead of int(“2”)+int(“3”) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why not 2+3 instead of int(“2”)+int(“3”)

14th Dec 2018, 7:15 AM
Evgeny
Evgeny - avatar
5 Answers
+ 5
I checked your profile and saw that you just started learning python so I'm assuming this question is about python. In python, the input() function always takes a string. That is why you need to cast it to int if you want to add two numbers. Otherwise, python will just concatenate them as strings as opposed to adding them as numbers. (you will get "23" instead of 5)
14th Dec 2018, 7:37 AM
Lambda_Driver
Lambda_Driver - avatar
+ 1
It is generally safer to specify the type, otherwise it could interpret 2+3 as a string or as a concatenation of two strings, whereby 2+3 = 23 instead of int("2") + int("3") = int("5"). I hope this helps! 😊
14th Dec 2018, 7:37 AM
Janning⭐
Janning⭐ - avatar
+ 1
Could you provide more detail and context? Also mention the programming language in your tags (python?), use relevant tags Seems like an example for type casting, so just an educational purpose.
14th Dec 2018, 7:38 AM
Matthias
Matthias - avatar
0
I don't understand about what language do you write but I see that numbers in brackets are strings and we must convert it to int before sum/div/multy and others
14th Dec 2018, 7:36 AM
_yaroslavv [online_everyday]
_yaroslavv [online_everyday] - avatar
0
because if you write "2"+"3" you get "23"
14th Dec 2018, 7:37 AM
_yaroslavv [online_everyday]
_yaroslavv [online_everyday] - avatar