[Tutor] eazy python question involving functions and parameters
wesley chun
wescpy at gmail.com
Mon Sep 21 21:47:32 CEST 2009
>> an expression is something ... that evaluates to *some* Python object
>> :
>> in contrast, a statement is something that has no intrinsic value
>
> Umm.....you just completely confused me. What does it do?
ok, now *i*'m the one confused... what does *what* do?
both expressions and statements represent executable Python code,
however, a snippet of code like "4*5" has a value... it evaluates to
20, which you can optionally save or (re)use. similarly, a function,
like this one that does nothing: "def foo(): pass", also has a return
value (None) that you can save.
however, calls to print (in Python 2), pass, if, while, for, try,
etc., do NOT have any return value, because they are statements, not
expressions. in fact, if you try to save a value when executing those
commands, you get an error:
In [1]: x = print 'hi'
-------------------------------------
File "<ipython console>", line 1
x = print 'hi'
^
SyntaxError: invalid syntax
In [2]:
of course, since print is a function in Python 3, it changes from
being a statement (in Python 2) to an expression (in Python 3).
again, the easiest way to differentiate b/w the 2 is whether or not it
results in some Python object. if it doesn, it's an expression; if
not, it's a statement.
clear as mud? hope this somewhat helps...
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
http://corepython.com
wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
More information about the Tutor
mailing list