Professional Documents
Culture Documents
LAB RECORD PYTHON - Merged
LAB RECORD PYTHON - Merged
2022-23
LAB 1
19 July 2022
In [3]:
Enter temperature: 66
In [4]:
* * * *
* * *
* *
In [5]:
1 #Construction of diamond
2 rows = int(input("Enter the number of rows: "))
3 # It is used to print the space
4 k = 2 * rows - 2
5 # Outer loop to print number of rows
6 for i in range(0, rows):
7 # Inner loop is used to print number of space
8 for j in range(0, k):
9 print(end=" ")
10 # Decrement in k after each iteration
11 k = k - 1
12 # This inner loop is used to print stars
13 for j in range(0, i + 1):
14 print("* ", end="")
15 print("")
16
17 #Downward triangle Pyramid
18 # It is used to print the space
19 k = rows - 2
20 # Output for downward triangle pyramid
21 for i in range(rows, -1, -1):
22 # inner loop will print the spaces
23 for j in range(k, 0, -1):
24 print(end=" ")
25 # Increment in k after each iteration
26 k = k + 1
27 # This inner loop will print number of stars
28 for j in range(0, i + 1):
29 print("* ", end="")
30 print("")
31
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
In [6]:
1 #factorial of a number
2 num= int(input("Enter the number: "))
3 factorial= 1
4 if num<0:
5 print("Factorial cant be computed")
6 elif num==0:
7 print("Factorial is 1")
8 else:
9 for i in range(1, num + 1):
10 factorial= factorial*i
11 print("Factorial of", num, "is" ,factorial)
Factorial of 6 is 720
LAB 2
26 July 2022
Aim: to learn and understand the operations on matrices and determinants like
addition and multiplication
In [1]:
First Matrix...
1 2
3 4
Second Matrix...
2 5
6 7
3 7
9 11
2 10
18 28
LAB 3
27 July 2022
Aim: to learn and understand the operations on matrices and determinants like
transpose
In [1]:
1 2
3 4
1 3
2 4
In [1]:
1 2 3
4 5 6
1 4
2 5
3 6
LAB 4
2 August 2022
Aim: to learn and understand the operations on matrices like determinant and
inverse
localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 7/41
22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook
In [4]:
4 6
3 2
-10.000000000000002
In [1]:
1 #Inverse of a matrix
2 import numpy as np
3 a= int(input("Enter the number of rows: "))
4 b= int(input("Enter the number of columns: "))
5 matrix= []
6 for i in range(a):
7 c= []
8 for j in range(b):
9 j= int(input("Enter the elements of matrix: "))
10 c.append(j)
11 matrix.append(c)
12 print("Your Matrix is: ")
13 for i in range(a):
14 for j in range(b):
15 print(matrix[i][j], end=" ")
16 print()
17 print("Inverse of your matrix is: ")
18 Det1= np.linalg.inv(matrix)
19 print(Det1)
2 3
4 1
[[-0.1 0.3]
[ 0.4 -0.2]]
LAB 5
3 August 2022
In [3]:
1 1 2
3 4 8
-2 2 1
LAB 6
10 August 2022
In [1]:
1 #Write a python program to find the rank of a given matrix (square matrix)
2 import numpy as np
3 a= int(input("Enter the number of rows: "))
4 b= int(input("Enter the number of columns: "))
5 matrix= []
6 for i in range(a):
7 c= []
8 for j in range(b):
9 j= int(input("Enter the elements of matrix: "))
10 c.append(j)
11 matrix.append(c)
12 print("Your Matrix is: ")
13 for i in range(a):
14 for j in range(b):
15 print(matrix[i][j], end=" ")
16 print()
17 print("Determinate of your matrix is: ")
18 Det1= np.linalg.det(matrix)
19 print(float(Det1))
20 print("Rank of your matrix is: ")
21 rank= np.linalg.matrix_rank(matrix)
22 print(rank)
1 2 3 0
2 4 3 2
3 2 1 3
6 8 7 5
2.664535259100386e-15
LAB 7
localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 12/41
22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook
16 August 2022
In [4]:
1 #Write a python program to find the rank of a given matrix (non square matrix)
2 import numpy as np
3 a= int(input("Enter the number of rows: "))
4 b= int(input("Enter the number of columns: "))
5 matrix= []
6 for i in range(a):
7 c= []
8 for j in range(b):
9 j= int(input("Enter the elements of matrix: "))
10 c.append(j)
11 matrix.append(c)
12 print("Your Matrix is: ")
13 for i in range(a):
14 for j in range(b):
15 print(matrix[i][j], end=" ")
16 print()
17 print("Rank of your matrix is: ")
18 rank= np.linalg.matrix_rank(matrix)
19 print(rank)
1 -7 15
2 3 -4
3 -4 11
5 -1 7
LAB 8
17 August 2022
Note:
In [3]:
1 2 -1
3 -1 2
2 -2 3
-1
[-1. 4. 4.]
LAB 9
23 August 2022
In [4]:
1 #Write a python program to find the eigenvalue and eigenvector of a matrix [A-lambda(I)
2 import numpy as np
3 a= int(input("Enter the number of rows: "))
4 b= int(input("Enter the number of columns: "))
5 matrix= []
6 for i in range(a):
7 c= []
8 for j in range(b):
9 j= int(input("Enter the elements of matrix: "))
10 c.append(j)
11 matrix.append(c)
12 print("Your Matrix is: ")
13 for i in range(a):
14 for j in range(b):
15 print(matrix[i][j], end=" ")
16 print()
17 print("Determinate of your matrix is: ")
18 Det1= np.linalg.det(matrix)
19 print(float(Det1))
20 result= [[0 for i in range(a)]for j in range(b)]
21 Inv1=np.linalg.inv(matrix)
22 result.append(Inv1)
23 print("Inverse matrix is: ")
24 for i in range(a):
25 for j in range(b):
26 print(float(Inv1[i][j]), end=" ")
27 print()
28 print("Eigenvalues: ")
29 Eigen1=np.linalg.eigvals(matrix)
30 print(Eigen1)
31
4 1
-1 2
8.999999999999998
0.2222222222222222 -0.1111111111111111
0.1111111111111111 0.4444444444444444
Eigenvalues:
[3.00000001 2.99999999]
LAB 10
30 August 2022
In [1]:
1 import numpy as np
2
3 import matplotlib.pyplot as plt
4
5 %matplotlib inline
6
7 from scipy.optimize import fsolve
In [2]:
[2.23606798] [-1.77635684e-15]
In [3]:
1 def f(xyz):
2
3 x=xyz[0]
4
5 y=xyz[1]
6
7 z=xyz[2]
8
9
10
11 f0=x**2+y**2-1
12
13 f1=x*y+y*z+1.1
14
15 f2=y**2+z**2-2
16
17
18
19 return np.array([f0,f1,f2])
20
21
22
23 xyz0=np.array([2.0,2,2])
24
25
26
27 xyz=fsolve(f,xyz0)
28
29 x=xyz[0]
30
31 y=xyz[1]
32
33 z=xyz[2]
34
35
36
37 print(x,y,z,f(xyz))
LAB 11
6 September 2022
In [11]:
1 import numpy as np
2 import matplotlib.pyplot as plt
3 from scipy.optimize import fsolve
4 def f(x):
5 y=2.0* x**2 + 3.0*x-10.0
6 return y
7 x= np.linspace(-5, 3)
8 plt.plot(x, f(x))
9 plt.plot(x, np.zeros(len(x)))
10 plt.show
Out[11]:
1 def myFunction(z):
2 x=z[0]
3 y=z[1]
4 w=z[2]
5
6 F=np.empty((3))
7 F[0]=x**2+y**2-20
8 F[1]= y - x**2
9 F[2]= w+ 5 - x*y
10 return F
11 zGuess = np.array([1,1,1])
12 z = fsolve(myFunction, zGuess)
13 print(z)
[2. 4. 3.]
In [15]:
1 import numpy as np
2 from scipy.optimize import fsolve
3 def myFunction(xyz):
4 x=xyz[0]
5 y=xyz[1]
6 w=xyz[2]
7
8 F=np.empty((3))
9 F[0]=x**2+y**2-1
10 F[1]= xy + y**2 + 1.1
11 F[2]= y**2 + z**2 -2
12 return np.array([f0, f1, f2])
13 xyz0= np.array([2.0,2,2])
14 xyz = fsolve(f, xyz0)
15
16 print(xyz, f(xyz))
In [14]:
1 import numpy as np
2 from scipy.optimize import *
3 def myFunction(xyz):
4 x=xyz[0]
5 y=xyz[1]
6 w=xyz[2]
7
8 F=np.empty((3))
9 F[0]=x**2+y**2-1
10 F[1]= xy + y**2 + 1.1
11 F[2]= y**2 + z**2 -2
12 return np.array([f0, f1, f2])
13 xyz0= np.array([2.0,2,2])
14 xyz = fsolve(f, xyz0)
15 x= xyz[0]
16 y= xyz[1]
17 w= xyz[2]
18 print(xyz, f(xyz))
LAB 12
7 September 2022
Series
A series is a sum of a sequence of terms. That is, a series is a list of numbers with addition operations between
them.
∞ 𝑥𝑛
1.
∑
𝑛=1 𝑛
In [2]:
∞ 1
2.
∑
𝑛=1 𝑛 − 1
2
In [4]:
Out[4]:
0.7489994994995
Printing a Series
𝑥 + 𝑥2 + 𝑥3 + 𝑥4 + 𝑥5 +.....+ 𝑥𝑛
2 3 4 5 𝑛
3.
In [9]:
1 import sympy as sp
2 from sympy import pprint
3 x= sp.Symbol('x')
4 series=x #we create the label, series and set its initial value as x
5 n= int(input("Enter the number of terms you want in the series: "))
6 #we define a for loop that will iterate
7 #over the integers from 2 to n
8 for i in range(2, n+1):
9 series= series + (x**i)/i #Each time the loop iterates, it adds each term to series
10 pprint(series)
5 4 3 2
x x x x
── + ── + ── + ── + x
5 4 3 2
LAB 13
13 September 2022
In [2]:
2 3 4 5
x x x x
x + ── + ── + ── + ──
2 3 4 5
The print series() function accepts an integer n as a parameter that is the number of terms in the series that will
be printed.
In [1]:
2 3 4 5
x x x x
x + ── + ── + ── + ──
2 3 4 5
LAB 14
14 September 2022
1 + 2 + 3 + 4+....+𝑛
In [19]:
13 + 23 + 33 +...+𝑛3
In [18]:
2 + 4 + 6 + 8... + 𝑛
In [17]:
9 + 99 + 999+....+𝑛
In [2]:
1 + (1 + 3) + (1 + 3 + 5)+...+𝑛
localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 25/41
22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook
In [5]:
14
LAB 15
20 September 2022
In [3]:
a[0][0]=1
a[0][1]=-1
a[0][2]=2
a[0][3]=3
a[1][0]=1
a[1][1]=2
a[1][2]=3
a[1][3]=5
a[2][0]=3
a[2][1]=-4
a[2][2]=-5
a[2][3]=-13
LAB 16
21 September 2022
Aim:to understand and learn about the gauss jordan method on equations
In [2]:
a[0][0]=2
a[0][1]=1
a[0][2]=1
a[0][3]=10
a[1][0]=3
a[1][1]=2
a[1][2]=3
a[1][3]=18
a[2][0]=1
a[2][1]=4
a[2][2]=9
a[2][3]=16
LAB 17
18 October 2022
Write a program to reduce the user defined matrix to an echelon form( lesding entry may not be unity) using
elementary row operations and without using in built function echelon_form
In [6]:
1 import numpy as np
2 from sympy import Matrix, pprint
3 A= np.mat(input("Enter a matrix: "))
4 (r,c)= A.shape
5 A=Matrix(A)
6 print("The given matrix is: ")
7 pprint(A)
8 b=0
9 for j in range(c):
10 for i in range(b+1, r):
11 x= 0
12 Flag= False
13 if (A[b,j]==0):
14 for k in range(b+1, r):
15 if (A[k,j] !=0):
16 A= A.elementary_row_op(op="n<->m", row1=k, row2=b )
17 x=1
18 break
19 if (A[b,j]==0 and x==0):
20 Flag= True
21 break
22 A= A.elementary_row_op(op= "n->n+km", row=i, k=-(A[i,j]/A[b,j]), row1=i, row2=b
23 if Flag:
24 continue
25 else:
26 b+=1
27 print("The echelon form of the matrix is: ")
28 pprint(A)
Enter a matrix: 2 3 4; 5 4 6; 7 9 8
⎡2 3 4⎤
⎢ ⎥
⎢5 4 6⎥
⎢ ⎥
⎣7 9 8⎦
⎡2 3 4 ⎤
⎢ ⎥
⎢0 -7/2 -4 ⎥
⎢ ⎥
⎣0 0 -30/7⎦
In [8]:
1 import numpy as np
2 from sympy import Matrix, pprint
3 A= np.mat(input("Enter a matrix: "))
4 (r,c)= A.shape
5 A=Matrix(A)
6 print("The given matrix is: ")
7 pprint(A)
8 b=0
9 for j in range(c):
10 for i in range(b+1, r):
11 x= 0
12 Flag= False
13 if (A[b,j]==0):
14 for k in range(b+1, r):
15 if (A[k,j] !=0):
16 A= A.elementary_row_op(op="n<->m", row1=k, row2=b )
17 x=1
18 break
19 if (A[b,j]==0 and x==0):
20 Flag= True
21 break
22 A= A.elementary_row_op(op= "n->n+km", row=i, k=-(A[i,j]/A[b,j]), row1=i, row2=b
23 if Flag:
24 continue
25 else:
26 b+=1
27 print("The echelon form of the matrix is: ")
28 pprint(A)
Enter a matrix: 2 1 2; 3 4 5; 6 7 8
⎡2 1 2⎤
⎢ ⎥
⎢3 4 5⎥
⎢ ⎥
⎣6 7 8⎦
⎡2 1 2 ⎤
⎢ ⎥
⎢0 5/2 2 ⎥
⎢ ⎥
⎣0 0 -6/5⎦
LAB 18
2 November 2022
A complex number is the sum of a real and imaginary number. A complex number is of the form a+ib and is
usually represented by z. Heere both a and b are real numbers. The value is called the real part denoted by
Re(z) and b is called the imaginary part im(z). Also, ib is the imaginary number.
cmath: This module provides access to mathematical functions for complex numbers.
1. exp() :- This function returns the exponent of the complex number mentioned in its
argument.
2. log(x,b) :- This function returns the logarithmic value of x with the base b, both mentioned in its
arguments.
If base is not specified, natural log of x is returned.
3. log10() :- This function returns the log base 10 of a complex number.
4. sqrt() :- This computes the square root of a complex number.
In [1]:
1 v= 2+3j
2 v
Out[1]:
(2+3j)
In [2]:
1 u= 3j+6
2 u
Out[2]:
(6+3j)
In [3]:
1 p=3
2 q=5
3 r=p+1j*q
4 r
Out[3]:
(3+5j)
In [4]:
1 #Python converts the real numbers x and y into complex numbers using function complex(x
2 x=2
3 y=6
4 z= complex(x,y)
5 z
Out[4]:
(2+6j)
In [5]:
1 m=1+2j
2 n=5+6j
3 w= m+n
4 w
Out[5]:
(6+8j)
In [6]:
1 a= 2+3j
2 b= 5+6j
3 a+b
Out[6]:
(7+9j)
In [7]:
1 a-b
Out[7]:
(-3-3j)
In [8]:
1 a*b
Out[8]:
(-8+27j)
In [9]:
1 u*v
Out[9]:
(3+24j)
In [10]:
1 a/b
Out[10]:
(0.4590163934426229+0.04918032786885245j)
In [12]:
1 h=9-5j
2 h.real
Out[12]:
9.0
In [13]:
1 h.imag
Out[13]:
-5.0
Complex Conjugate
In [14]:
1 h.conjugate()
Out[14]:
(9+5j)
In [16]:
1 w=-6-8j
2 w.conjugate()
Out[16]:
(-6+8j)
Write a program to enter two real number and convert them into complex number. Also find the real and
imaginary part of the complex number.
localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 35/41
22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook
In [21]:
In [20]:
Out[20]:
(9+0j)
LAB 19
8 November 2022
In [1]:
Trignometric functions
sin(): This function returns the sine of complex number passed in argument
cos(): This fuction returns the cosine function of complex number passed in argument
tan(): This function returns the tangent function of complex number passed in argument
In [2]:
LAB 20
9 November 2022
Aim: to understand and learn the working of isnan(), isinif() and isfinite() in
python
isfinite() :- Returns true if both real and imaginary part of complex number are finite, else returns false.
isinf() :- Returns true if either real or imaginary part of complex number is/are infinite, else returns false.
isnan() :- Returns true if either real or imaginary part of complex number is NaN , else returns false.
math.inf- A floating-point positive infinity. (For negative infinity, use -math.inf.) Equivalent to the output of
float('inf').
math.nan- A floating-point “not a number” (NaN) value. Equivalent to the output of float('nan')
In [5]:
In [3]:
1 a= math.inf
2 b= math.nan
3 c= -math.inf
4 print("a:", a)
5 print("b:", b)
6 print("c:", c)
a: inf
b: nan
c: -inf
LAB 21
localhost:8888/notebooks/Semester 3 Python.ipynb#Topic: 39/41
LAB 21
22/11/2022, 22:19 Semester 3 Python - Jupyter Notebook
15 November 2022
In [1]:
In [2]:
1 r1=sin(1+8j)
2 r1
Out[2]:
(1254.1949676545178+805.3091464217314j)
In [3]:
1 r2=cos(2+4j)
2 r2
Out[3]:
(-11.36423470640106-24.814651485634187j)
In [4]:
1 #finding root -1
2 s=sqrt(-1)
3 s
Out[4]:
1j
Note:
𝑒𝑖𝜃 = 𝑐𝑜𝑠𝜃 + 𝑖𝑠𝑖𝑛𝜃
In [5]:
1 #i=1j
2 f1=3
3 exp(1j*f1)
Out[5]:
(-0.9899924966004454+0.1411200080598672j)
In [6]:
1 cos(f1)+1j*sin(f1)
Out[6]:
(-0.9899924966004454+0.1411200080598672j)
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
2 ⎯
derivable, then the formula for calculating the length of the arc of the curve between and is as follows:
𝑏 𝑑𝑦
∫𝑎 √1 + ( 𝑑𝑥 ) 𝑑𝑥
In [7]:
In [ ]: