sympy Getting started with sympy Integration and Differentiation

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Sympy is made for symbolic math, so let's have a look at some basic integration and differentiation.

from sympy import symbols, sqrt, exp, diff, integrate, pprint                                                                                                                      

x, y = symbols('x y', real=True)                                                                                                                                                              

pprint(diff(4*x**3+exp(3*x**2*y)+y**2,x))                                                                                                                                          
pprint(diff(4*x**3+exp(3*x**2*y)+y**2,y))                                                                                                                                          
pprint(integrate(exp(x*y**2)+sqrt(x)*y**2,x))                                                                                                                                      
pprint(integrate(exp(x*y**2)+sqrt(x)*y**2,y))

First we import the necessary functions from sympy. Next we define our variables x and y. Note that these are considered complex by default, so we tell sympy that we want a simple example by making them real. Next we differentiate some expression with respect to x and then y. Finally we integrate some expression, again with respect to x and then y. The call of pprint ensures that our functions get written in some nice human readable style.



Got any sympy Question?