[Tutor] How to perform variable assignment and

Oxymoron moron.oxy at gmail.com
Sat Oct 3 12:39:12 CEST 2009

(Did not reply to list earlier, apologies)

On Sat, Oct 3, 2009 at 7:47 PM, Didar Hossain <didar.hossain at gmail.com>wrote:

> >>> There are probably various syntactic tricks to achieve the same,
> however,
> >>> the main reason you can't do it is that assignment in Python is a
> statement
> >>> rather than an expression, i.e. it does not return a value that the if
> >>> statement can evaluate.
> Ok, this is what I understood as the difference between "expression"
> and "statement":
> statement => directive to do something
> expression => check for truth value or "evaluatable" code
> Am I correct?
Expressions do stuff too (e.g. function calls with side effects) and can
direct "doing", so I'm a bit reluctant to agree with your definition, which
gives that privilege only to statements :-). In my view, the crucial thing
to note is what happens post-execution of either a statement or an
expression. A statement will not return any value - in C it's like a void
function, you cannot assign the result to anything else - because well,
there's no result returned. An expression always returns a value which in
turn can be a simple expression, a more complex expression, even None, etc.
In the case of the if statement, if was in fact a statement, however, the
condition part: if <condition>: <body>, is expected to be an expression
that's convertible to a boolean value, true or false. I say convertible,
because the following is legal:

if None: print "bar"

All types have implicit True or False values within the context of

>>> if []: print "bar"
>>> if [1]: print "bar"

In the above example, an empty list is considered as False, but a non-empty
one is True. This in turn allows you to write concise and often reusable
code (though you need to be sure the semantics make sense for your use case)
since you do not need to check explicitly every time. I'll leave you to
further research these implicit conversions of types.

Playing a bit with the interactive interpreter gives you a feel for
statements vs. expressions:

>>> a = 1
>>> a
>>> a + a + a
>>> "foo"
>>> a = "foo"
>>> a

After a = 1, the interpreter had nothing interesting to say - no value
returned, a = "foo" also nothing, but the remaining yielded values, so the
interpreter output those.

There's one caveat though with the above, the use of 'None' - a
null/nil/NULL on (mild?) steroids since it is a real type (hint: dir(None)),
this is definitely a valid value to return, it is assignable to something
else, or returned from a function, and as we saw in the case of 'if None:
...', it certainly can be evaluated, it is as real as "foo". However, in the

>>> None
>>> a = None
>>> a

Hmm, quiet.

It's also implicitly returned by functions.

>>> def foo():
...     return
>>> x = foo()
>>> x
>>> x is None
>>> def foo():
...     pass
>>> x = foo()
>>> x is None

In my view, None should possibly be printed... perhaps it could get
annoying, but it would be explicit - so I put forth my question, if None not
important enough to display? ;-)

-- Kamal

PS. I've heard the phrase "expression statement" used where a function call
returns None (implicitly or explicitly) ... sounds highly oxymoronic to me!

There is more to life than increasing its speed.
-- Mahatma Gandhi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091003/8cae0eaa/attachment.htm>

More information about the Tutor mailing list