1

Let's say, I have this code:

#!/bin/env/python2

def hello():
    print "hello, you!"

input()

case A) If I execute this, and I type as user input

hello()

I get

hello, you!

case B) If I type

int(2.3)

or any other built-in function, I get nothing, as expected.

case C) If I type

print "hello you!"

I get

  Traceback (most recent call last):
File "./test_input.py", line 8, in <module>
  input()
File "<string>", line 1
  print "hello you!"
      ^
SyntaxError: invalid syntax

Why do functions work, but the print statement doesn't?

steffen
  • 8,572
  • 11
  • 52
  • 90
  • 2
    Because it `eval`s the input, to run statements `exec` can be used. – Ashwini Chaudhary Nov 06 '14 at 13:48
  • @AshwiniChaudhary: But why does every other statement work? If I type a single 1 , I get no error and no output as expected. What is so special about print? – steffen Nov 06 '14 at 13:55
  • 1
    `1` is an expression. so is `hello()`. `print` is not an expression. It is a statement. `input` can only evaluate expressions and not statements. – Kevin Nov 06 '14 at 13:55
  • 1
    `1` is not a statement, it is an *expression*. `print` is a statement, *not* an expression. – chepner Nov 06 '14 at 13:55

4 Answers4

2

So, input() will run eval() over whatever you pass to it. eval() expects an expression as the argument.

So, you've given 2 examples of expressions:

hello()
int(2.3)

And one example of a statement:

print "hello world"
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • Can you give an example of a statement that works? http://stackoverflow.com/questions/4728073/what-is-the-difference-between-an-expression-and-a-statement-in-python – Bill Lynch Nov 06 '14 at 13:56
  • 1
    @steffen Other *statements* do not work. You need to understand that "expression" and "statement" are well-defined concepts in Python, and you cannot use the two terms interchangeably. – chepner Nov 06 '14 at 13:56
  • 2
    Can you give an example of a statement that works? Note that `1` and `hello()` are not statements. Things like `print`, `assert`, `raise`, `break`, `continue`, are statements. – Kevin Nov 06 '14 at 13:57
  • @steffen: Function calls are expressions. – Bill Lynch Nov 06 '14 at 13:59
  • @steffen, check out [the Python Language Reference](https://docs.python.org/3/reference/index.html). Section 6 describes expressions, and sections 7 and 8 describe statements. – Kevin Nov 06 '14 at 14:00
  • @Kevin: Nope. Use this link (And section 5): https://docs.python.org/2/reference/expressions.html#calls We're talking about Python 2.x. – Bill Lynch Nov 06 '14 at 14:01
  • @BillLynch: Ok, I have to read about that again. Thank you all for clarifying. – steffen Nov 06 '14 at 14:01
  • 1
    If you look at [the grammar](https://docs.python.org/2/reference/simple_stmts.html), you'll see that a simple statement can consist of many different kinds of statements, one of which is an expression statement. That is, an expression can be used where a more general statement is expected, but the reverse is not true. – chepner Nov 06 '14 at 14:02
1

A print command in Python 2 doesn't have a value that can be evaluated. It is a statement that can be executed. Python distinguishes between statements and other expressions. You can execute it using exec.

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • So the difference between print and other statements is that it cannot be evaluated. Does that account for all statement that are keywords? Or what is the general rule? – steffen Nov 06 '14 at 13:57
  • @steffen There is a list of statements on the page I linked to: https://docs.python.org/2/reference/simple_stmts.html – khelwood Nov 06 '14 at 14:33
0

From python doc:

input (prompt) -- built-in functionAlmost equivalent to eval(raw_input(prompt)). As for raw_input(), the prompt argument is optional. The difference is that a long input expression may be broken over multiple lines using the backslash convention.

So, when you input hello(), it will evaluate 'hello()', which would call your hello function. When you input 'print "hello world"', it evaluate 'print', but print command cannot be evaluate.

coo
  • 114
  • 9
0

input() considering the functions but not the statement as print and assert both are statement def hello(): print "hello, you!" a = input() print type(a)

output:-
>>>
open('foo.txt')
<type 'file'>
>>>
>>> 
assert 23

Traceback (most recent call last):
  File "/home/vishnu/foo.py", line 3, in <module>
    a = input()
  File "<string>", line 1
    assert 23
         ^
SyntaxError: invalid syntax
Vishnu Upadhyay
  • 5,043
  • 1
  • 13
  • 24