```python
# symbols and expressions
import sympy
```


```python
x*2
```


```python
# we have to say that x is a symbol
x = sympy.var('x')
```


```python
x*x
```




    x**2




```python
from sympy import *
```


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


```python
f = x*x*x
```


```python
print(f)
```

    x**3



```python
# will cover in detail later
diff(f,x)
```




    3*x**2




```python
x, y, z, t = symbols('x y z t')
```


```python
expr = x*x+y*z+t*x*z
```


```python
diff(expr, z)
```




    t*x + y




```python
# caveat - possible to confuse and go mad if not careful
x = symbols('y')
```


```python
x*x
```




    y**2




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


```python
x*x
```




    areyoucrazy**2




```python
# == signs compares the expressions exactly, not symbolically.
# For comparisons use either equals or subtraction
x = symbols('x')
(x+2)**2 == x**2 + 4*x + 4
```




    False




```python
a = (x+2)**2
b = x**2 + 4*x + 4
```


```python
c = a-b
```


```python
print(c)
```

    -x**2 - 4*x + (x + 2)**2 - 4



```python
simplify(c)
```




    0




```python
a.equals(b)
```




    True




```python
# numerals 
x + 1/10
```




    x + 0.1




```python
1/10
```




    0.1




```python
# constructing rational objects explicitly
Rational(1,10)
```




    1/10




```python
x + Rational(1,20)
```




    x + 1/20




```python
expr = x**y
```


```python
expr_x2 = expr.subs(x,2)
```


```python
print(expr_x2)
```

    2**y



```python
expr_yx = expr.subs(y, x)
```


```python
print(expr_yx)
```

    x**x



```python
expr_x2_y3 = expr.subs([(x,2),(y,3)])
```


```python
print(expr_x2_y3)
```

    8



```python
# evaluating expressions

```


```python
expr = sqrt(8)
```


```python
print(expr)
```

    2*sqrt(2)



```python
expr.evalf()
```




    2.82842712474619




```python
pi
```




    pi




```python
print(pi)
```

    pi



```python
pi.evalf(10)
```




    3.141592654




```python
pi.evalf(100)
```




    3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068




```python
# simplifying expression
# by default the expressions entered by the user are not simplified because
# the user might want the expressions displayed in different formats.
# For example, some times it is useful to have the polynomials in terms of factors.

```


```python
(x+2)**2 - 4*x
```




    -4*x + (x + 2)**2




```python
# we can see the get the polynomial form by using expand
expand((x+2)**2)
```


```python
simplify((x+2)**2 - 4*x)
```


```python
# pretty printing for powers, exponentials, integrals, derivatives etc.
# IPython's QTConsole uses LATEX if installed. Otherwise, uses matplotlib engine.
```


```python
# use init_printing() for enabling pretty printing.
# This will load the default printing available
# unicode type printing is used on my laptop
from sympy import init_printing
init_printing()
```


```python
(x+2)**2 - 4*x
```


```python
Integral(sqrt(1/x), x)
```


```python
# symbols and functions
x, y, z = symbols('x y z', integer=True)
```


```python
f, g = symbols('f g', cls=Function)
```


```python
type(x)
```




    sympy.core.symbol.Symbol




```python
type(f)
```




    sympy.core.function.UndefinedFunction




```python
f = 2*x*y**2 + x**t
```


```python
type(f)
```




    sympy.core.add.Add




```python
g = 2*x*y*z**t
```


```python
type(g)
```




    sympy.core.mul.Mul




```python

```

03e8fd33f9e6cf6867a2506ace36273dca1cef1b
ipynb
Jupyter Notebook
2-basics-of-sympy.ipynb
chennachaos/SA2CTechChatSymPy
9f1dbb48655ff5f8bdd6b4ced48b58aed0ba5bf4
MIT
2-basics-of-sympy.ipynb
chennachaos/SA2CTechChatSymPy
9f1dbb48655ff5f8bdd6b4ced48b58aed0ba5bf4
MIT
2-basics-of-sympy.ipynb
chennachaos/SA2CTechChatSymPy
9f1dbb48655ff5f8bdd6b4ced48b58aed0ba5bf4
MIT
Qwen/Qwen-72B
1. YES
2. YES