+ 1

Difference between x=input(int()) and x=int(input())

What is the difference between x=input(int()) and x=int(input()). Please Explain this.

14th Jul 2021, 10:06 AM
Ch Ruchitha
Ch Ruchitha - avatar
6 Respostas
+ 2
first one output 0 and wait/get for user input second one wait/get user input (as string) and cast it to int... throwing error if not valid int ^^
14th Jul 2021, 10:19 AM
visph
visph - avatar
+ 1
The future is now thanks to science int() doesn't convert only integer string to int... int() is a class... calling it pass arguments provided to its __init__ method and return the related int if first arg can be converted to int ;)
14th Jul 2021, 10:26 AM
visph
visph - avatar
+ 1
Great example visph . Just tried it now. It also converts True to 1 and False to 0.
14th Jul 2021, 10:37 AM
The future is now thanks to science
The future is now thanks to science - avatar
0
The future is now thanks to science you can pass float number to int (it return truncated value: 4.2 => 4, -4.2 => 4)... however, passing float number as string raise exception (not valid whole number) ^^
14th Jul 2021, 10:33 AM
visph
visph - avatar
0
The future is now thanks to science int() initializer use __int__ magic method of any object... or __nonzero__ in case of no __int__ defined ;)
14th Jul 2021, 10:41 AM
visph
visph - avatar
0
The future is now thanks to science my bad: int argument must be string, byte-like or number, not object ;P
14th Jul 2021, 10:45 AM
visph
visph - avatar