-1

This is more of a curiosity question than a coding question but it helps me understand how Python works. So my question is, why can't I just define a variable and add my int value? example:

pancake = 9
pancake + 1
print(pancake)

I'm interested in why Python doesn't add 1 to 9 because technically I am saying 9 + 1 right?

  • 1
    But you can (it's `+=` instead of `+`) – wim May 30 '18 at 00:29
  • 3
    `pancake + 1` doesn't assign anything to `pancake`. In order to assign the result to `pancake`, it should be `pancake += 1`. – hcheung May 30 '18 at 00:32
  • 1
    Let's see if there will be an interesting answer (nothing yet). It probably will contain the terms expression and assignment, but i'm also lost on the terminology in terms of programming paradigms. An interesting question would be: in which programming language would the value of this variable be mutated given that code? – sascha May 30 '18 at 00:37
  • 1
    `pancake + 1` is an expression and does return `10`, e.g. `print(pancake+1)` will print `10`. But you need to assign it to something, e.g. `pancake = pancake + 1`, which can be shortcut to `pancake += 1`. – AChampion May 30 '18 at 00:38
  • 3
    `pancake + 1` means `10`. You can assign that to another variable (`more_pancakes = pancake + 1`), or pass it to a function, or store it in a list, or do anything else you can do with a value. Meanwhile, `pancake` still means `9`, which means you can write `fewer_pancakes = pancake - 1` later and get `8`. Hopefully you can see why it's useful to still have access to the value `9` even after creating the value `10`? – abarnert May 30 '18 at 00:38
  • 2
    @sascha There are some CPUs that have a default target register for every operation, so, e.g., `ADD src1, src2, dest` can be abbreviated to `ADD src1, src2` meaning `ADD src1, src2, R1`. If you wrote a macro-assembler for such a language, `x + y` would presumably turn into `ADD x, y`, so, e.g., `R1 + R4` would turn into `ADD R1, R4`, which means `ADD R1, R4, R1`, which "mutates" R1. Close enough? :) – abarnert May 30 '18 at 00:42

3 Answers3

1

You need to use the += operator (instead of +) to modify the variable on the left. Otherwise, the resulting sum is not assigned to the variable you are trying to print.

See this thread for more information on the += operator.

What exactly does += do in python?

Basically, += will add the value to the right and assign the result to the variable on the left. Using a + by itself does not modify the variable on the left.

Grokify
  • 15,092
  • 6
  • 60
  • 81
0

You can, but you should use += operator to re-assign the value to the pancake variable

pancake = 9
pancake += 1
print(pancake)
0

that's because when you do that pancake + 1 you are calling the __add__ method of the pancake object which is an instance of the class int, this method returns the sum of the value of this object "self" plus the value that is to the right of the operator +. that is why the value of the pancake object is not updated, you could better understand this case if you investigate a bit about the magic methods of python

juancarlos
  • 593
  • 3
  • 9