What does "&" mean in SymPy?
Question:
What does "&" exactly mean in SymPy results?
Does it always mean "and"?
For example:
solve(Eq(216, abs(y**3)))
Output: (6 < y) | (y < -6) | ((-6 < y) & (y < 6))
So I can display it to the user like this:
print(str(solve(Eq(216, abs(y**3)))).replace("&", "and").replace("|", "or"))
Output: (6 < y) or (y < -6) or ((-6 < y) and (y < 6))
Answers:
It is the Python "and" operator; the "|" is the "or" operator. Python also has "and" and "or" keywords. If you type in the "so it means" expression you will get an error because "and" and "or" are expecting things with bool(thing) == True or False.
SymPy knows that you probably don’t want (x<1) and (x>2)
to evaluate to True so it raises that error for you (while, for things like x and y
, SymPy uses the normal Python semantics and returns y
) To get the unevaluated expression you would write (x<1) & (x>2)
or And(x<1, x>2)
. When you simplify that expression it will simplify to false: And(x<1, x>2).simplify() -> false
. The expression And(x,y)
will not simplify to anything.
What does "&" exactly mean in SymPy results?
Does it always mean "and"?
For example:
solve(Eq(216, abs(y**3)))
Output: (6 < y) | (y < -6) | ((-6 < y) & (y < 6))
So I can display it to the user like this:
print(str(solve(Eq(216, abs(y**3)))).replace("&", "and").replace("|", "or"))
Output: (6 < y) or (y < -6) or ((-6 < y) and (y < 6))
It is the Python "and" operator; the "|" is the "or" operator. Python also has "and" and "or" keywords. If you type in the "so it means" expression you will get an error because "and" and "or" are expecting things with bool(thing) == True or False.
SymPy knows that you probably don’t want (x<1) and (x>2)
to evaluate to True so it raises that error for you (while, for things like x and y
, SymPy uses the normal Python semantics and returns y
) To get the unevaluated expression you would write (x<1) & (x>2)
or And(x<1, x>2)
. When you simplify that expression it will simplify to false: And(x<1, x>2).simplify() -> false
. The expression And(x,y)
will not simplify to anything.