[Tutor] difference between expressions and statements

Alan Gauld alan.gauld at btinternet.com
Wed Apr 9 23:01:39 CEST 2014

On 09/04/14 17:49, Jared Nielsen wrote:
> Hi Pythons,
> Could someone explain the difference between expressions and statements?
> I know that expressions are statements that produce a value.

Yep, that's it.

> I'm unclear on functions and especially strings.

Unclear in what way? Both functions and strings are expressions,
in that they produce, or are, values.

> Are any of the following expressions?
> print(42)
> print("spam")
> spam = 42
> print(spam)

Yes, 3 are expressions, and all 4 are statements containing expressions.

> Is the first example producing a value or simply displaying an integer?

It does actually produce a value but its not the integer that is 
displayed. The default value for any function (including print() )
is None... You can prove that by trying:

 >>> print( print(42) )

The 42 is the output displayed by the innermost print()
The None is the value returned by the inner print function.

The Python interpreter normally suppresses the None from a print 
function but because I explicitly told it to print the return from print 
it did it in this case.

> Does a string count as a value?

Yes, certainly.

> Is a variable assignment considered a value?

No, its a statement but not an expression.
(In Python at least, in some other languages the rules are different)

> If I print a variable is that considered production of a value?

Yes, as above. But the value produced is the None returned
by the print function not the value that print displays.


And did I just do your homework? hmmm... I'll give
you the benefit of the doubt.

Alan G
Author of the Learn to Program web site

More information about the Tutor mailing list