+ 5
What is the difference between input() raw_input() and eval(raw_input())
I'm using python 2.7 so i kinda know the last 2 but I just want to see the difference in 3
2 Respostas
+ 4
In Python 2, raw_input() returns a string and input() tries to run the input as a Python expression, eval(raw_input()) is the same as input().
In Python 3, there is no raw_input() function, input() returns a string and eval(input()) tries to run the input as a Python expression
>>> from sys import version
>>> version
'2.7.2 (default, Jun 24 2011, 12:21:10) [MSC v.1500 32 bit (Intel)]'
>>> raw_input()
2+2
'2+2'
>>> input()
2+2
4
>>> eval(raw_input())
2+2
4
>>> from sys import version
>>> version
'3.2.2 (default, Sep 8 2011, 10:56:38) [MSC v.1500 32 bit (Intel)]'
>>> input()
2+2
'2+2'
>>> eval(input())
2+2
4
>>> raw_input
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
raw_input
NameError: name 'raw_input' is not defined
+ 2
I know that raw_input() is used in the older Version of Python (2.x.x) and Input() is used in 3.x.x, but it does the same. I don't know what eval(raw_input()) does