Int inputs | Sololearn: Learn to code for FREE!

0

# Int inputs

So I have a question if I’m doing inputs let’s say of age why can’t I just do age = input() than do print(age) instead of age = int(input()) print(age)

10/1/2021 5:32:51 PM

Sean Simmons

9 Answers

New Answer

+4

You can absolutely do so, but if you want to do some sort of mathematical operations with age variable in between of input and output, you won't be able to do so, as if you don't use int() function, your variable age will be of type string.

+4

Oh i understand thank you very much

+3

There is no problem with keeping the input as a string if you only use it as a string. In fact, that way is more efficient. However, if you need to perform calculations with the numeric value, then that is when you must convert it to a numeric type.

+3

Sean Simmons , not only for mathematical operations we need e.g. integer values, also using relational operators with "string numbers" can lead to suspicious results: print('17' < '21') # result is True because 17 is less than 21 print('117' < '21') # result is True but 117 is not less than 21 in both cases the comparison is done by a lexicographical comparison by using the ascii or unicode values.

+3

If you write just input(), your input will be a string. That is why you have to write int(input()). If you write int(input()) then your input will be an integer.Or you can use input=int(input). THANKS.

+2

When you just give input() python consider it as string think python like you are talking to python(person) by giving commands like that it will be more simple to understand so Age is here in numbers form (integer) so you have to tell python that it should be converted to integer form (Numbers) so by doing it int(input()) Python will convert string to integer

+1

Is there any difference though because i was just redoing everything making notes and saw that on severel briefs it says the same thing but to do it a different way

0

You can do that

0

The int function changes any primitive data type to the integer data type