Using + in python

What is the difference between + and + =?
In the code below I used + = instead of + and it gave an error;

scaled_grades = [grade + 10 for grade in grades]

print(scaled_grades)

+= is a short-hand, these two lines are the same:

x = x + 3
x += 3

+ is simply plus, like you are used to in math
= is assignment, combined with + it will increase the value of the variable

1 Like

The list comprehension is returning an iterable and in this case is made as List to scaled_grades
If yo want to use the +=, then the code has to be changed as:

for grade in grades:
   scaled_grades += [grade +10]
1 Like

In Python assignments are statements not expressions, which is the case in some languages. As such they cannot be used in list comprehension because it is expecting an expression.

2 Likes