Primitive type int() in Python

Asked

Viewed 807 times

2

I am beginner(1 semester of TADS), the maximum programming I saw was programming logic and now I’ve started to study Python.

In the course of Python Video Course he taught that the primitive type int() should be written as:

n1 = int(input('Digite um valor: '))
n2 = int(input('Digite outro valor: '))
s = n1 + n2
print(s)

The same day I saw a solution that wrote differently, so:

n1 = input('Digite um valor: ')
n2 = input('Digite outro valor: ')
s = int(n1) + int(n2)
print(s)

One between the two can make some kind of mistake in the future or I can use whatever I want?

2 answers

7


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!

6

If someone said the guy int should be written as it is already teaching wrong. The function int() serves to convert some value of another type into an integer, if possible.

Neither of them has problems, it’s the same thing. It’s equal math, writing the same formula differently doesn’t change the result. But I’d do it a different way.

For a beginner the second form seems to be the easiest to understand. She’s taking two values typed by the user and then she’s converting each of them into an integer to add up. She is conceptually wrong if she considers that n1 and n2 seem to want to store numbers, and is saving texts. but does not change the result. I like the conceptually right because it takes you the right way.

The first form also gives to understand well and in a certain way can even be easier to understand. It is more correct in my point of view because n1 and n2 already stores numbers because the conversion is done just after the input of the data.

Could avoid variables:

print(int(input('Digite um valor: ') + int(input('Digite outro valor: '))

I put in the Github for future reference.

  • Thank you! I left no more information on the question so the mistake was mine: The teacher of the course, Gustavo Guanabara, did not impose a unique way of writing, in fact he did not show the other way, I discovered on his own responding an exercise. Therefore, what he did was to show a form of solution, but it was not in his intentions to delimit only that. Alias, great response!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.