2

The following code doesn't work in python

x = 11
print(x += 5)

while this code does

x = 11
x += 5
print(x)

why is that?

1
  • Because the language designers don't want it to. See the zen of python for some idea of what to expect from the language: python.org/dev/peps/pep-0020 Commented Feb 25, 2011 at 9:32

3 Answers 3

8

The problem is the due to the difference between a Statement and an Expression. This question has an excellent answer which explains the difference, the key point being:

Expression: Something which evaluates to a value. Example: 1+2/x

Statement: A line of code which does something. Example: GOTO 100

The print statement needs a value to print out. So in the brackets you put an expression which gives it the value to print. So this can be something as simple as x or a more complicated expression like "The value is %d" % x.

x += 5 is a statement which adds 5 to x but it doesn't come back with a value for print to use.

So in Python you can't say

print(x += 5)

any more than you could say:

y = x += 5

However, in some other languages, Statements are also Expressions, i.e. they do something and return a value. For example, this you can do this in Perl:

$x = 5;
$y = $x += 5;
print $y;

Whether you would want to do that is another question.

One of the benefits of enforcing the difference between statements and expressions in Python is that you avoid common bugs where instead of something like:

if (myvar == 1) {
    //do things
}

you have the following by mistake:

if (myvar = 1) {
    //do things
}

In second case, C will set myvar to 1 but Python will fail with a compile error because you have a statement where you should have an expression.

Sign up to request clarification or add additional context in comments.

4 Comments

I think this is known in C/C++ as sequence points, am I right?
Oh, and there's no such thing as a compile error in python, they're all runtime errors :-)
@Phobia: not true. Entire modules are compiled before the first statement is executed. A syntax error anywhere in the file will prevent compilation, so the module won't run at all.
Python (At least the version I'm using) is interpreted, not compiled, I know python can also be compiled, but I don't compile it
8

x += 5 is a statement, not an expression. You can only use expressions as arguments in function calls.

I'm assuming you are used to a C-like language, where x += 5 is an expression, but in Python it's not.

Comments

1

In Python, function calls only accept expressions, not statements.

Anything with an equals in it is a statement.

It's the same reason you can't do:

if x += 5:
    print x

See the Python Language Reference (3.2 version, 2.7 version) for full details.

1 Comment

Also the same reason you can't do print(x=5).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.