```python
from sympy import *
init_printing()
```

# 1. Symbols
Python variables and SymPy Variables


```python
a = 'stuff' # Python variable
```

The `symbols` function creates a SymPy Variable `x` which is assigned to python variable `x`.


```python
x = symbols('x')
x
```

The following works too, but not recommended. The python variable `crazy` is assigned to the SymPy variable `unrelated`. So, its always a good idea to name your variables same as your symbols.


```python
crazy = symbols('unrelated')
crazy
```

# 2. Immutability


```python
x, y = symbols('x y')
a = x + y + 1
a
```


```python
x = 3
```

What is "`a`" now?


```python
a
```

Now, how do I substitute x with 3?


```python
??
```

What is `a` now?


```python
a
```

SymPy expressions are all immutable. They never change in-place. So, whenever you do `subs`, `simplify`, etc it creates a new expression.

# 3. Equals sign
Checking mathematical inequality.


```python
x = symbols('x')
a = (x + 1)**2
a
```


```python
b = x**2 + 2*x + 1
```

Now, lets check if `a` and `b` are equal or not. What do you think will be the output of the following?


```python
a == b
```


```python
simplify(a - b)
```

That's the difference between Structural and Mathematical Equality.

#### Representing a symbolic equality, like say: $$x^2 = y$$


```python
eq = Eq(x**2, y)
```


```python
eq.lhs
```


```python
eq.rhs
```


```python
solveset(eq, x)
```

# 4. Fractions 


```python
# In Python 2
acos(1/2)
```

The arc cosine of Half is π/3, isn't it?


```python
# Python 2
1/2
```


```python
# Python 3
from __future__ import division
1/2
```


```python
acos(1/2)
```

We are looking for symbolic results, right?


```python
Rational(1, 2)
```


```python
acos(Rational(1, 2))
```


```python
from fractions import Fraction
Fraction(1, 2)
```


```python
acos(Fraction(1, 2))
```


```python
type(sympify(Fraction(1, 2)))
```

# 5. Logical Operator (XOR)


```python
x^2
```


```python
sympify('x^2')
```

If you are doing logic, and **don't** want to convert the `XOR` to power operator:


```python
sympify('x^2', convert_xor=False)
```

6bd9b672e23e0dac28ed466a58821237fea373d3
ipynb
Jupyter Notebook
tutorial_exercises/Gotchas.ipynb
gvvynplaine/scipy-2016-tutorial
aa417427a1de2dcab2a9640b631b809d525d7929
BSD-3-Clause
2016-06-21T21:11:02.000Z
2021-02-04T07:51:03.000Z
tutorial_exercises/Gotchas.ipynb
gvvynplaine/scipy-2016-tutorial
aa417427a1de2dcab2a9640b631b809d525d7929
BSD-3-Clause
2016-07-02T20:24:06.000Z
2016-07-11T11:31:44.000Z
tutorial_exercises/Gotchas.ipynb
gvvynplaine/scipy-2016-tutorial
aa417427a1de2dcab2a9640b631b809d525d7929
BSD-3-Clause
2016-06-25T09:04:24.000Z
2021-08-09T06:46:01.000Z
Qwen/Qwen-72B
1. YES
2. YES