The two forms are perfectly equivalent when considering only the output produced, but the readability of the code differs. Namely, the native function input
always returns a value of type string , even if a number is entered. In the first form, it is explicit in the first lines that it is desired to work with integer values, and thus both n1
and n2
receive the return of input
converted to integers. In the second case, both objects will be of type string and since the goal is to add two numbers, it does not make much sense to store them as such. In other words, the first code snippet perfectly translates the program's intent, while the second does not.
Note that int
in Python is a native class and that when calling int()
is not invoking a function with this name, but the initializing method of class int
. That is, you are instantiating the class by passing its value to the initializer.
What to use?
In a very general way, I would prefer to use the first variation, given what I mentioned earlier, but that does not imply that the second form is wrong. Depending on the situation, the second one may make more sense: to quote, a problem where you will necessarily need to store the values as strings .