-3
def add_10(x):
    tenplus=x+10
    return tenplus
add_30=add_10(30)
add_90=add_10(90)

If I swap the last two lines such that I get:

def add_10(x):
    tenplus=x+10
    return tenplus
add_10(30)=add_30
add_10(30)=add_90

There is an error. How does a computer read the second version?

Vishal Jain
  • 443
  • 4
  • 17
  • 1
    I don't think that would work in any language. How can you make an assignment to a function call? It's not clear to me why you expect that to work – roganjosh Jun 08 '19 at 13:20
  • you assign by `x=value` not `value=x` and for comparing use `x==value` – anky Jun 08 '19 at 13:21
  • 1
    I think you're thinking too much in terms of commutative examples from mathematics. In programming (in Python at least), `=` is an assignment and separate. Other languages vary in the syntax, but I don't think they vary in the ordering of the assignment. – roganjosh Jun 08 '19 at 13:23
  • Many programming languages use `=` for assignment even though this is mathematically confusing and/or annoying. To avoid this, e.g. Pascal uses `:=` instead; this is [being made available as a synonym in a forthcoming version of Python](/a/50071110/874188), too, although the canonical `=` assignment is not going away any time soon. (I believe `:=` was chosen as an ASCII approximation of ⇐ back in the dark ages.) – tripleee Jun 08 '19 at 13:39
  • (Looking at PEP572 in more detail, you won't be able to use `:=` as a direct replacement for `=` even in 3.8.) – tripleee Jun 08 '19 at 13:47

2 Answers2

3

They're not equations; they're assignments. Don't confuse = for a mathematical equality symbol. It's an assignment operator. It assigns the RHS to the LHS.

Imagine that the assignment operator were instead of =. Then this would assign the result of add_10(30) to a variable named add_30:

add_30 ← add_10(30)

And this would... assign add_30 to the function call add_10(30)? What the heck does that mean? It doesn't exactly make sense, does it?

add_10(30) ← add_30     # huh?

Indeed, I too was confused about = when I first started programming. We programmers are all quite used to = nowadays, but it wasn't necessarily the best choice back in the 70's when C was invented. In fact, Pascal—which was designed as a teaching language—deliberately invented a new, non-symmetric assignment operator so as not to confuse math students. = was the equality operator rather than assignment.

add_30 := add_10(30)
if add_30 = 40 then ...

Too bad it didn't catch on. C won the influence wars and most modern languages base their syntax on C, so = for assignment it is.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
1

Here, "=" is an assignment operator. In the first case, you assign a known value to an unknown variable - this makes sense. In the second case, I am not sure what you are trying to achieve.

Yulia V
  • 3,507
  • 10
  • 31
  • 64