Ask Your Question

"=" vs "==" - when to use what?

asked 2019-02-14 09:46:42 +0100

stockh0lm gravatar image

i am confused as to when to use "=" and when "==" is correct. My most recent model has been that "=" means "is defined as", while "==" means "evaluates to the same as".

But that does not make sense either, since for example stuff like

plot(f.subs(a == 3), (x, -1, +1)) is clearly a definition, not an evaluation.

So please someone explain this to me.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2019-02-14 22:18:51 +0100

eric_g gravatar image

updated 2019-02-14 22:20:24 +0100

I agree that f.subs(a == 3) is bad syntax and should probably be deprecated. For substitutions, it is much clearer to use a Python dictionary:

sage: a, b = var('a b')
sage: f = a*x + b
sage: f.subs({a: 3})
b + 3*x
sage: f.subs({a: 3, b: -2})
3*x - 2
edit flag offensive delete link more

answered 2019-02-14 09:50:28 +0100

tmonteil gravatar image

updated 2019-02-14 18:34:52 +0100

Actually, in Python = has another meaning when calling a function or a method: it is used to pass a value to a parameter, that is, if a function f has two parameters m and n, and you want to evaluate it with $m=1$ and $n=4$, you can do:

f(m=1, n=4)

I would say that f(m==1) is a attempt to be nice with the newcomer, but it would not mean anything outside the symbolic ring (that keeps m==1 as a symbolic expression with two operands), since in general this will just reduce to f(True) or f(False). There is nothing interesting to understand here, this is not Pythonic, and reserved to a dark part of Sage.

edit flag offensive delete link more



that makes the scope of my question bigger, and i would love to hear a good answer.

stockh0lm gravatar imagestockh0lm ( 2019-02-14 13:20:50 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2019-02-14 09:46:42 +0100

Seen: 235 times

Last updated: Feb 14 '19