Int inputs | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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)

1st Oct 2021, 5:32 PM
Sean Simmons
9 Answers
+ 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.
1st Oct 2021, 5:40 PM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 4
Oh i understand thank you very much
1st Oct 2021, 5:42 PM
Sean Simmons
+ 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.
1st Oct 2021, 5:40 PM
Brian
Brian - avatar
+ 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.
1st Oct 2021, 6:26 PM
Lothar
Lothar - avatar
+ 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.
2nd Oct 2021, 4:52 PM
Tousif HR
+ 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
3rd Oct 2021, 5:34 PM
Sayyam Jain
Sayyam Jain - avatar
+ 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
1st Oct 2021, 5:40 PM
Sean Simmons
0
You can do that
1st Oct 2021, 5:39 PM
Eashan Morajkar
Eashan Morajkar - avatar
0
The int function changes any primitive data type to the integer data type
1st Oct 2021, 5:39 PM
Eashan Morajkar
Eashan Morajkar - avatar