Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiplication Assignment Operator when variable is 0

why the result of the following operation is always 0 in the Python REPL? (got the same results for C and Javascript)

>>> a = 0
>>> a *= 1 + 1
>>> a
0

Shouldn't the multiplication assignment be equivalent to a = a * 1 + 1 in the case above?

>>> a = 0
>>> a = a * 1 + 1
>>> a
1
like image 978
Raphael Philipe Avatar asked Feb 15 '26 20:02

Raphael Philipe


1 Answers

because

a *= 1 + 1

is the same as

a = a *(1 + 1)

since a is 0 then the result is still 0

like image 180
ΦXocę 웃 Пepeúpa ツ Avatar answered Feb 18 '26 10:02

ΦXocę 웃 Пepeúpa ツ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!