1
Learning practice for python
SymPy
SymPy is a Python library used for symbolic mathematics.
It helps in solving algebraic expressions, calculus problems, equations, matrices, and
symbolic integration.
Important Functions
symbols() → creates variables
solve() → solves equations
diff() → differentiation
integrate() → integration
expand() → expands expressions
factor() → factorization
Example
Factorization (not in syllabus but for example )
from sympy import *
x = symbols('x')
expr = x**2 + 2*x + 1
print(factor(expr))
Output:
(x + 1)**2
NumPy
NumPy is a Python library used for numerical computations and arrays.
It performs fast mathematical and array operations.
Important Functions
array() → creates arrays
zeros() → creates zero matrix
ones() → creates ones matrix
arange() → creates range array
linspace() → creates equally spaced values
2
sum() → calculates sum
mean() → calculates average
max() / min() → maximum or minimum value
SciPy
SciPy is a Python library used for scientific and advanced mathematical computing.
It is built on NumPy and provides tools for optimization, integration, statistics, signal
processing, and linear algebra.
Important Functions
quad() → integration
odeint() → solves differential equations
minimize() → optimization
fft() → Fourier transform
[Link]() → determinant calculation
Example
from [Link] import quad
result, error = quad(lambda x: x**2, 0, 2)
print(result)
Output:
2.666666666666667
Python Function
A function in Python is a block of code that performs a specific task.
Functions are used to make programs shorter, organized, and reusable.
General Form of Python Function
def function_name(parameters):
# code
return value
Explanation
def → keyword used to define a function
3
function_name → name of the function
parameters → input values
return → sends result back
Example
def add(a, b):
return a + b
result = add(3, 4)
print(result)
Output:
User Defined Function
A user defined function is created by the programmer using the def keyword.
Example
def add(a, b):
return a + b
print(add(3, 4))
Output:
Calling Function
Calling a function means executing the function by writing its name with parentheses.
Example
def greet():
print("Hello")
greet()
Output:
Hello
4
Built-in Function
Built-in functions are already available in Python.
Examples
print()
len()
type()
max()
min()
Example
numbers = [1, 2, 3, 4]
print(len(numbers))
Output:
Integration
Integration is used to find area, accumulation, and antiderivatives.
Simple Integral
∫ 𝑥 2 𝑑𝑥
Result:
𝑥3
+𝑐
3
Python Example
from sympy import *
x = symbols('x')
result = integrate(x**2, x)
print(result)
Output:
5
x**3/3
Double Integral
A double integral is used for functions with two variables.
∫ ∫(𝑥 + 𝑦)𝑑𝑥𝑑𝑦
Python Example
from sympy import *
x, y = symbols('x y')
result = integrate(x + y, (x,0,1), (y,0,1))
print(result)
Triple Integral
A triple integral is used for functions with three variables.
∫ ∫ ∫(𝑥 + 𝑦 + 𝑧)𝑑𝑥𝑑𝑦𝑑𝑧
Python Example
from sympy import *
x, y, z = symbols('x y z')
result = integrate(x+y+z, (x,0,1), (y,0,1), (z,0,1))
print(result)
Definite Integral
A definite integral has upper and lower limits.
2
∫ 𝑥 2 𝑑𝑥
0
Result:
6
8
3
Python Example
from sympy import *
x = symbols('x')
result = integrate(x**2, (x,0,2))
print(result)
Output:
8/3
Differentiation
Differentiation is used to find the rate of change or derivative.
𝑑
(𝑥 2 + 3𝑥 + 2)
𝑑𝑥
Result:
2x + 3
Python Example
from sympy import *
x = symbols('x')
result = diff(x**2 + 3*x + 2, x)
print(result)
Output:
2*x + 3