Basics of numpy and sympy
From fractions
import Fraction
print(2+3j)
print(type(2+3j))
print((4+4j)
+(3+2j))
print((4+4j)-
(3+2j))
print((4+4j)*(3+2j
)) print((4+4j)**2)
print((2+3j).real)
print((2+3j).imag)
print((2+3j).conjugate())
print(abs(2+3j))
print(Fraction(3,4))
print(Fraction(3,4)+1+1.5)
print(Fraction(3,4)+1+Fraction(3,4))
from random import *
num = randint(1,10) guess =
eval(input("Enter your guess:
"))
if guess==num:
print("You got it.")
else: print("Sorry
you lost it.")
#Practice Problems:
#A function is a block of code that performs a specific
task.
#Types of functions:
#*Standard Library(in-built functions) -
print(),sqrt(),pow()
#S*User defined - we can make our functions
x=min(3,1,2)
y=max(5,2,6)
print(x) print(y)
z=pow(2,8)
print(z) import
math
l=[Link](1.4
m=[Link](
1.4)
print(l)
print(m)
sq_rt=[Link](4)
print("The square root of
4 is:",sq_rt)
power=pow(2,3)
print("2 to the power 3 is:",power)
r=[Link]
print(r)
def greet():
q="I am
local."
print("Local
1:",q) greet()
q1="I am global."
def teerg():
print("Global 1:",q1)
teerg()
def deef():
print("I am
error:",q) deef()
#Arc length: To compute, the length of the curve on
the inteval [a,b] we compute the integral of sqrt(1+
(f'(x))**2)dx
#Q. find the arc length of the function f(x)=x**3/2 on
[0,2] from sympy import *
x=symbols("x")
f=diff(x**(3/2),x)
a=integrate(sqrt(1+f**2),
(x,0,2)) print(a)
#Find arc length of
f(x)=x on[1,2] from
sympy import *
x,y=symbols('x,y')
f=diff(x,x,1)
a=integrate(sqrt(1+f**2),(x,1,2))
print(a)
#Find arc length of
f(x)=(1/3)*(x**2+2)**(3/2) on [0,1] from
sympy import * x,y=symbols('x,y')
f=diff((1/3)*(x**2+2)**(3/2),x,1)
a=integrate(sqrt(1+f**2),(x,0,1)) print(a)
from sympy import *
x,y,z=symbols("x,y,z")
a=integrate(x*y*z,(z,0,x+y),(y,2,x),
(x,0,3)) print(a)
#Matrices: Matrix is a two-dimensional array where
numbers, symbols or expressions are arranged into
rows and columns. Using the concepts of lists, one can
easily define a matrix in Python #We define two
matrices A and B.
A=[[1,2,3],[4,5,6],[7,8,9]
B=[[9,8,7],[6,5,4],[3,2,1]
print(A)
print(B)
a=A[0][0]
print(a)
A[0].append(10)
print(A)
#Working with matrices can be made easier in Python
using NumPy package
#NumPy is a library consisting of multidimensional
array objects and package of functions for processing
those arrays. NumPy stands for Numerical Python. The
following operations can be performed using NumPy.
#1. Mathematical and logical operations on
arrays. #2. Fourier transforms and routines for
shape manipulation #3. Operations related to
linear algebra.
#We can import the NumPy package using one of the
following syntax-
#import numpy
#import numpy as np (preferred)
#from numpy import *
#NumPy provides the function array() and matrix() to
define a matrix
import numpy as np
M1=[Link]([[1,2,3],[3,4,5],[5,6,7]
]) print(M1) print([Link])
M2=[Link]([[1,2,3],[3,4,5],[5,6,7],[1,2,34]
])
print(M2)
print([Link][0])
print([Link][1])
print([Link])
M3=[Link]((4,4))
M4=[Link]((4,4),dtype=in
t)
print(M3)
print(M4)
#Matrix Operations:
D=[Link]([[1,2,3],[3,4,5],
[5,6,7]]) print("The matrix D \
n",D) E=[Link]([[1,2,3],
[1,9,5],[4,1,3]]) print("The
matrix E \n",E)
F=[Link]([[1,7,0],[2,4,1],
[5,5,7]]) print("The matrix F \
n",F) #Addition: print(D+E)
print([Link](D,F))
#Subtraction
print(D-F) print([Link](D,F))
#Multiplication
print([Link](D,F))
print([Link](D,F)) print(D*F)
#Division
print([Link](D,F)) print(D/F)