The two forms are perfectly equivalent when considering only the result produced, but the readability of the code differs. Namely, the native function input
always returns a type value string, even if you type in a number. In the first form, it is already explicit in the first lines that the desired is to work with integer values and thus both n1
how much n2
receive the return of input
converted to integers. In the second case, both objects will be of the type string And since the goal is to add up two numbers, it doesn’t make much sense to store them as such. In other words, the first snippet of code perfectly translates the intention of the program, while the second does not.
It is worth remembering that int
in Python is a native class and when called int()
is not calling a function with this name, but the class initializer method int
. That is, you are instantiating the class by passing the respective value to the initializer.
Which to use?
Generally speaking, I would prefer to use the first variation, given what I said earlier, but this does not imply that the second form is wrong. Depending on the situation, the second may make more sense: to quote, a problem where you will necessarily need to store the values like strings.
Thank you, excellent reply!
– Darlley Brito