In python 2.7, if we take an integer as input, which one is more faster and efficient or there is no difference at all:
input() or int(raw_input())
In python 2.7, if we take an integer as input, which one is more faster and efficient or there is no difference at all:
input() or int(raw_input())
From the Python docs
input([prompt])
Equivalent to eval(raw_input(prompt)).
This function does not catch user errors. If the input is not syntactically valid, a SyntaxError will be raised. Other exceptions may be raised if there is an error during evaluation.
If the readline module was loaded, then input() will use it to provide elaborate line editing and history features.
Consider using the raw_input() function for general input from users.
int(raw_input())
will be faster, more secure and produce less confusing results.
Consider:
>>> b = 5
>>> a = input()
[1, 2, 3]
>>> a + b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "int") to list
>>> a = int(raw_input())
[1, 2, 3]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '[1, 2, 3]'
The ValueError
raised when reading input is far more desirable than the TypeError
raised when using the variable.