Practical1-12 Sybcs
Practical1-12 Sybcs
Practical1-12 Sybcs
Name:
Roll No:
Class:
Div:
Batch:
Subject:
Subject Code:
INDEX
Sr.
List of Practical Date Remark Signature
No.
1 Introduction to Python Data Types - I
5 Matrices
7 System of Equation
10 Roots of Equations
11 Numerical Integration - I
12 Numerical Integration - II
Practical – 01
Introduction to Python Data Types - I
Name of Student:
Roll No:
Batch:
2. A=3+2j
type(A)
Output: <class 'complex'>
3. A=3.14
type(A)
Output: <class 'float'>
4. A='python'
type(A)
Output: <class 'str'>
5. A=[1,2,3,4,5]
type(A)
Output: <class 'list'>
2. B='Mathematics'
print(B)
Output: Mathematics
2. B=3*10//3+10%3
print(B)
Output: 11
Q.4) Compare the following expression.
1. A=21
B=15
print(A==B)
Output: False
2. A=4
B=5
print(A!=B)
Output: True
3. A=10
B=15
print(A<=B)
Output: True
4. A=4
B=8
print(A<B)
Output: True
Q.5) Print the following terms by importing math and cmath module.
1. log(10) with base 10
import math
print(math.log10(10))
Output: 1.0
2. sin(30)
import math
print(math.sin(30))
Output: -0.9880316240928618
3. Value of pi
import math
print(math.pi)
Output: 3.141592653589793
4. tan(45)
import math
print(math.tan(45))
Output: 1.6197751905438615
5. 3+5j
a = 3+5j
print(a)
Output: (3+5j)
Q.6) Accept the different value by using input() statement and print it from the following
code.
1. A=input("A:")
A:30
print(A)
Output: 30
len(y)
Output: 6
x[-1]
Output: 'y'
y[2]
Output: ' '
y[4]
Output: 'l'
3*x
Output: 'Math is EasyMath is EasyMath is Easy'
Q.2) Write a python program of concatenation and repetition of string and list.
Ans:
1. Concatenation of string:
x = "hello "
y = "I am in sybsc(cs)"
x+y
Output: 'hello I am in sybsc(cs)'
2. Repetition of string:
x = "hello "
y = "I am in sybsc(cs)"
x*2
Output: 'hellohello'
y*2
Output: 'I am in sybsc(cs)I am in sybsc(cs)'
3. Concatenation of list:
L1 = ['mango','apple','banana',45,78,'kiwi']
L2 = ['bottle','book',66,90,'pen']
L1 + L2
Output: ['mango', 'apple', 'banana', 45, 78, 'kiwi', 'bottle', 'book', 66, 90, 'pen']
4. Repetition of list:
L1 = ['mango','apple','banana',45,78,'kiwi']
L2 = ['bottle','book',66,90,'pen']
L1 * 2
Output: ['mango', 'apple', 'banana', 45, 78, 'kiwi', 'mango', 'apple', 'banana', 45, 78,
'kiwi']
L2 * 2
Output: ['bottle', 'book', 66, 90, 'pen', 'bottle', 'book', 66, 90, 'pen']
Q.3) Write a python program to create a nested list & display its element.
Ans:
L = ['a', 'b', ['cc', 'dd', ['eee', 'fff']], 'g', 'h']
print(L)
Output: ['a', 'b', ['cc', 'dd', ['eee', 'fff']], 'g', 'h']
print(L[2])
Output: ['cc', 'dd', ['eee', 'fff']]
Q.4) Write a python program to create list using range function.
Ans:
My_list = [*range(10, 20, 1)]
print(My_list)
Output: [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
Q.5) Write a python program using membership operator ('not in' and 'is' operator) for
list.
Use: x=[1,2,3,'Rose','Pink','math','easy']
Ans:
'in' operator:
L1=[1,2,3,'Rose','Pink','math','easy']
L2=[2,3,4,'red']
if L1 in L2:
print("True")
else:
print("False")
Output: False
Q.6) Define tuple with single element & verify its type.
Ans:
tuple1 = (5,)
type(tuple1)
Output: <class 'tuple'>
Q.7) Write a python program to write a function that return area and circumference of
Circle.
Ans:
from math import *
def f(r):
c = 2 * pi * r
a = pi * r * r
return c, a
print(f(5))
Output: (31.41592653589793, 78.53981633974483)
Q.8) Write a python program to find area of rectangle by passing arguments.
Ans:
def rect_area(l, b):
return l * b
print(rect_area(5, 7))
Output: 35
Q.9) Write a python program to find maximum and minimum value from list of numbers.
Ans:
L1 = [5, 8, 7, 3, 12]
print("Maximum value from list is", max(L1))
print("Minimum value from list is", min(L1))
Output:
Maximum value from list is 12
Minimum value from list is 3
Practical – 03
Control Statements in Python - I
Name of Student:
Roll No:
Batch:
a) range(10)
L1 = range(10)
for i in L1:
print(i, end=' ')
print()
Output: 0 1 2 3 4 5 6 7 8 9
b) range(1,5)
L1 = range(1, 5)
for i in L1:
print(i, end=' ')
print()
Output: 1 2 3 4
c) range(1, 10, 2)
L1 = range(1, 10, 2)
for i in L1:
print(i, end=' ')
print()
Output: 1 3 5 7 9
d) range(5,2)
L1 = range(5,2)
for i in L1:
print(i, end=' ')
print()
Output: 2 3 4 5
Q.5) Write a python program to find first 10 terms of Fibonacci sequence using for loop.
Ans:
a=0
b=1
for i in range(10):
a, b = b, a + b
print(a, end=' ')
Output: 1 1 2 3 5 8 13 21 34 55
Q.6) Write a python program to check if the student is pass, fail, or distinction.
Ans:
name = input("Name of the student: ")
marks = int(input('Marks = '))
if marks > 39:
if marks > 75:
print("First class with distinction")
elif 60 <= marks <= 75:
print("First class")
else:
print("Second class")
else:
print("Fail")
Output:
Name of the student: Nisha
Marks = 71
First class
Practical – 04
Control Statements in Python - II
Name of Student:
Roll No:
Batch:
Q.1) Write a program to find divisors of any number using input function.
Ans:
n = int(input("Divisor of: "))
for i in range(1, n + 1):
if n % i == 0:
print(i)
Output:
Divisor of: 10
1
2
5
10
Q.2) Write a python program to print the absolute value of a given number.
Ans:
n = float(input("Enter the value: "))
absolute = abs(n)
print("\nThe absolute value of the number is: ->", absolute)
Output:
Enter the value: 5.23
The absolute value of the number is: -> 5.23
Q.3) Write a python program to print the table of a given number.
Ans:
i=1
n = int(input("Table of: "))
while i <= 10:
print(n * i)
i=i+1
Output:
Table of: 5
5
10
15
20
25
30
35
40
45
50
Q.4) Define a function that prints all integers between 1 to n that are relatively prime.
Ans:
import math
def phi(n):
for x in range(1, n):
if math.gcd(n, x) == 1:
print(x, end=' ')
phi(10)
Output: 1 3 7 9
Q.5) Define Euler’s phi function in python.
Ans:
import math
def phi(n):
i=0
for i in range(1, n):
if math.gcd(n, i) == 1:
i=i+1
print(i, end=' ')
Output: phi(10)
4
Q.6) Write a program to check if a person is eligible for voting or not and if they are a
senior citizen or not.
Ans:
a = int(input("Enter your age: "))
if a >= 60:
print("You are eligible for voting and you are a senior citizen.")
elif 18 <= a < 60:
print("You are eligible for voting and you are not a senior citizen.")
else:
print("You are not eligible for voting.")
Output:
Enter your age: 5
You are not eligible for voting.
Enter your age: 55
You are eligible for voting and you are not a senior citizen.
Enter your age: 70
You are eligible for voting and you are a senior citizen.
Practical – 05
Matrices
Name of Student:
Roll No:
Batch:
a) Find U + V
from sympy import Matrix
U = Matrix([[5], [0]])
V = Matrix([[1], [-2]])
result = U + V
print(result)
Output:
[ 6]
[-2]
b) Find 3V
from sympy import Matrix
V = Matrix([[1], [-2]])
result = 3 * V
print(result)
Output:
[ 3]
[-6]
c) Find 2U + 3V
from sympy import Matrix
U = Matrix([[5], [0]])
V = Matrix([[1], [-2]])
result = 2 * U + 3 * V
print(result)
Output:
[ 7]
[-6]
Q.2) Construct the following matrices using Python:
a) Find A + B
from sympy import Matrix
A = Matrix([[4, 2, 4], [4, -1, 1], [2, 4, 2]])
B = Matrix([[5, 2, 3], [3, -7, 5], [3, 1, -1]])
result = A + B
print(result)
Output:
[9 4 7]
[7 -8 6]
[5 5 1]
b) Find A - B
from sympy import Matrix
A = Matrix([[4, 2, 4], [4, -1, 1], [2, 4, 2]])
B = Matrix([[5, 2, 3], [3, -7, 5], [3, 1, -1]])
result = A - B
print(result)
Output:
[-1 0 1]
[ 1 6 -4]
[-1 3 3]
c) Find A^(-1)
from sympy import Matrix
A = Matrix([[4, 2, 4], [4, -1, 1], [2, 4, 2]])
inverse_A = A.inv()
print(inverse_A)
Output:
[-1/6 1/3 1/6]
[-1/6 0 1/3]
[ 1/2 -1/3 -1/3]
d) Find B * A
from sympy import Matrix
A = Matrix([[4, 2, 4], [4, -1, 1], [2, 4, 2]])
B = Matrix([[5, 2, 3], [3, -7, 5], [3, 1, -1]])
result = B * A
print(result)
Output:
[34 20 28]
[-6 33 15]
[14 1 11]
e) Find B^(-1) * A * B
from sympy import Matrix
A = Matrix([[4, 2, 4], [4, -1, 1], [2, 4, 2]])
B = Matrix([[5, 2, 3], [3, -7, 5], [3, 1, -1]])
result = B.inv() * A * B
print(result)
Output:
[ 522/59 -303/59 405/59]
[ -22/59 46/59 -72/59]
[-108/59 435/59 -273/59]
f) Find A^4
from sympy import Matrix
A = Matrix([[4, 2, 4], [4, -1, 1], [2, 4, 2]])
result = A**4
print(result)
Output:
[2060 1198 1622]
[1106 613 857]
[1456 848 1120]
Program:
from sympy import *
x, y, z = symbols("x, y, z")
A = Matrix([[3, 2, -1], [2, -2, 4], [2, -1, 2]])
B = Matrix([[3], [6], [9]])
print(linsolve((A, B), [x, y, z]))
OUTPUT:
{(6, -11, -7)}
Program:
from sympy import *
x, y, z = symbols("x, y, z")
A = Matrix([[7, 6, -8], [7, -2, 2], [6, -1, 2]])
B = Matrix([[3], [0], [9]])
sol, param = A.gauss_jordan_solve(B)
OUTPUT:
Matrix([ [7/11],
[106/11],
[163/22]])
Q.4) Find the solution by Gauss elimination method and Gauss Jordan method where
A = (2 1
1 2) and b = (5
7)
1) by Gauss elimination method Program:
from sympy import *
x, y = symbols("x, y")
A = Matrix([[2, 1], [1, 2]])
B = Matrix([[5], [7]])
print(linsolve((A, B), [x, y]))
OUTPUT:
{(1, 3)}
Q.8) Solve the system by using Gauss elimination method, Gauss Jordan method and LU
decomposition method.
(1 2 3 (x (6
a) 2 1 4 y = 7
2 5 1) z) 8
1) By Gauss Elimination Method Program:
from sympy import *
x, y, z = symbols("x, y, z")
A = Matrix([[1, 2, 3], [2, 1, 4], [2, 5, 1]])
B = Matrix([[6], [7], [8]])
print(linsolve((A, B), [x, y, z]))
OUTPUT:
{(1, 1, 1)}
2) By Gauss Jordan Method Program:
from sympy import *
x, y, z = symbols("x, y, z")
A = Matrix([[1, 2, 3], [2, 1, 4], [2, 5, 1]])
B = Matrix([[6], [7], [8]])
sol, param = A.gauss_jordan_solve(B)
OUTPUT:
Matrix([ [1],
[1],
[1]])
3) By LU Decomposition Program:
from sympy import *
x, y, z = symbols("x, y, z")
AB = Matrix([[1, 2, 3, 6], [2, 1, 4, 7], [2, 5, 1, 8]])
print(solve_linear_system_LU(AB, [x, y, z]))
OUTPUT:
{x: 1, y: 1, z: 1}
Q.9) – x + 2y + 2z = -1
x + y + 2z = 2
5x + 2z = 8
1) By Gauss Elimination Method Program:
from sympy import *
x, y, z = symbols("x, y, z")
A = Matrix([[-1, 2, 2], [1, 1, 2], [5, 0, 2]])
B = Matrix([[-1], [2], [8]])
print(linsolve((A, B), [x, y, z]))
OUTPUT:
{(3/2, 0, 1/4)}
Q.1) Using Sympy module of python, find the eigen values and the corresponding eigen
vectors of the matrix A = (4 2 2
242
2 2 4).
Ans-
from sympy import *
A = Matrix([[4,2,2],[2,4,2],[2,2,4]])
>>> A.eigenvals()
Output:
{8: 1, 2: 2}
>>> A.eigenvects()
O/p:
[(2, 2, [Matrix([
[-1],
[ 1],
[ 0]]), Matrix([
[-1],
[ 0],
[ 1]])]), (8, 1, [Matrix([
[1],
[1],
[1]])])]
Q.2) Using Sympy module of python, find the eigen values and the corresponding eigen
vectors of the matrix A = (1 2
2 1).
Ans-
from sympy import *
A = Matrix([[1, 2], [2, 1]])
>>>A.eigenvals()
Output:
{-1: 1, 3: 1}
>>>A.eigenvects()
Output:
[(3, 1, [Matrix([
[1],
[1]])]), (-1, 1, [Matrix([
[-1],
[1]])])]
Q.3) Using Sympy module of python, find the eigen values and the corresponding eigen
vectors of the matrix A = (2 3 1
321
1 1 2).
Ans-
from sympy import *
A = Matrix([[2, 3, 1], [3, 2, 1], [1, 1, 2]])
>>> A.eigenvals()
Output:
{4: 1, 1: 2}
>>> A.eigenvects()
Output:
[(4, 1, [Matrix([
[1],
[1],
[1]])]), (1, 2, [Matrix([
[-1],
[1],
[0]]), Matrix([
[0],
[1],
[-1]])])]
Q.4) Using Sympy module of python, find the eigen values and the corresponding eigen
vectors of the matrix A = (3 1 1
131
1 1 3).
Ans-
from sympy import *
A = Matrix([[3, 1, 1], [1, 3, 1], [1, 1, 3]])
>>> A.eigenvals()
Output:
{4: 1, 2: 2}
>>> A.eigenvects()
Output:
[(4, 1, [Matrix([
[1],
[1],
[1]])]), (2, 2, [Matrix([
[-1],
[1],
[0]]), Matrix([
[0],
[1],
[-1]])])
Q.5) Using Sympy module of python, find the eigen values and the corresponding
eigenvectors of the matrix A = (1 0 0
000
0 0 1).
Ans-
from sympy import *
A = Matrix([[1, 0, 0], [0, 0, 0], [0, 0, 1]])
>>> A.eigenvals()
Output:
{1: 1, 0: 2}
>>> A.eigenvects()
Output:
[(1, 1, [Matrix([
[1],
[0],
[0]])]), (0, 2, [Matrix([
[0],
[1],
[0]]), Matrix([
[0],
[0],
[1]])])]
Practical – 09
Determinants, System of Linear Equations
Name of Student:
Roll No:
Batch:
Q.1) Write a python program to diagonalizes the matrix [ 3 -2 ] and find matrix P and D.
Program:
from sympy import *
A = Matrix([[3, -2], [6, -4]])
P, D = A.diagonalize()
Output:
P = Matrix([ [1, 2],
[2, 3]])
D = Matrix([ [-1, 0],
[0, 0]])
Q.3) Find the eigenvalues of the following matrix and hence check whether it is
diagonalizable or not.
Program:
from sympy import *
A = Matrix([[1, 2, 2], [2, 1, 2], [2, 2, 1]])
Output:
A.eigenvals() -> {5: 1, -1: 2}
A.is_diagonalizable() -> True
def f(x):
return x**3 - 4*x**2 - 11*x + 30
def g(x):
return 3*x**2 - 8*x - 11
def f(x):
return x**3 + 2*x
def g(x):
return 3*x**2 + 2
newtonraphson(f, g, 3, 0.0001)
Output:
Iteration = 1 x1 = 1.35 and f(x1) = 5.160375
Iteration = 2 x1 = 0.839071782178218 and f(x1) = 2.2688848832741244
Iteration = 3 x1 = 0.5165318379707146 and f(x1) = 1.1708770250983709
Iteration = 4 x1 = 0.286911684752419 and f(x1) = 0.5974414559037061
Iteration = 5 x1 = 0.12637258636310406 and f(x1) = 0.25476334679541873
Iteration = 6 x1 = 0.03400797677253169 and f(x1) = 0.06805528521500116
Iteration = 7 x1 = 0.0031305686951099317 and f(x1) = 0.006261168071234248
Iteration = 8 x1 = 2.911262335528837e-05 and f(x1) = 5.822524673525099e-05
Iteration = 9 x1 = 2.5424001309411746e-09 and f(x1) = 5.084800261882349e-09
Required root is : 0.0
Q3: Using python find the correct root of the function 𝑒𝑥 − sin 𝑥, in [0, 1] using
Newton Raphson method correct upto 3 decimal places. Take 𝑥0 = 0.4.
from math import exp, sin, cos
def newtonraphson(f, g, x0, e):
step = 1
condition = True
while condition:
if g(x0) == 0.0:
print('Divided by zero error!')
break
x1 = x0 - (f(x0) / g(x0))
print('Iteration =', step, 'x1 =', x1, 'and f(x1) =', f(x1))
x0 = x1
step += 1
condition = abs(f(x1)) > e
print('Required root is :', round(x1, 3))
def f(x):
return exp(x) - sin(x)
def g(x):
return exp(x) - cos(x)
Q4: Using python find the correct root of the function 𝑒 𝑥 , in [-1, 1] using Regula
Falsi method correct upto 4 decimal places.
def falseposition(f, x0, x1, e):
if f(x0) * f(x1) > 0.0:
print('Given guess values do not break the root. Try again with different guess
. values.')
else:
step = 1
condition = True
while condition:
x2 = ((x0 * f(x1)) - (x1 * f(x0))) / (f(x1) - f(x0))
print('Iteration =', step, 'x2 =', x2, 'and f(x2) =', f(x2))
if f(x0) * f(x2) < 0:
x1 = x2
else:
x0 = x2
step += 1
condition = abs(f(x2)) > e
print('Required root is :', round(x2, 4))
def f(x):
return exp(x)
def f(x):
return sin(x) - 8 * x
def f(x):
return x**6 - x**4 - x**3 - 1
falseposition(f, 1, 2, 0.001)
Output:
Iteration = 1 x2 = 1.048780487804878 and f(x2) = -2.0326812065849147
……
Iteration = 43 x2 = 1.4035942752871293 and f(x2) = -0.00012335744923852587
Iteration = 44 x2 = 1.4035961617193395 and f(x2) = -9.371235608179873e-05
Required root is : 1.404
Practical – 11
Numerical Integration - I
Name of Student:
Roll No:
Batch:
𝟏𝟎
Q1: Estimate the value of the integral ∫𝟎 (𝟏 + 𝒙)𝒅𝒙 dx using the trapezoidal rule (h=0.2)
def trapezoidal(f, a, b, n):
h = float(b - a) / n
result = f(a) + f(b)
for i in range(1, n):
result += 2 * f(a + i * h)
result = (h / 2) * result
print("Integration by trap is:", result)
return result
def f(x):
return 1 + x
trapezoidal(f, 0, 1, 5)
Output:
Integration by trap is: 1.5
𝟏𝟎 𝟏
Q2: Find the value of ∫𝟎 𝟏+𝒙𝟐 𝒅𝒙 using the trapezoidal rule (h=0.5)
def trapezoidal(f, a, b, n):
h = float(b - a) / n
result = f(a) + f(b)
for i in range(1, n):
result += 2 * f(a + i * h)
result = (h / 2) * result
print("Integration by trap is:", result)
return result
def f(x):
return 1 / (1 + x**2)
trapezoidal(f, 0, 5, 10)
Output:
Integration by trap is: 1.5707963267948966
𝟏𝟎
Q3: Solve the integral ∫𝟎 (𝟏 + 𝒙)𝟑 𝒅𝒙 dx using the trapezoidal rule (h=1)
def trapezoidal(f, a, b, n):
h = float(b - a) / n
result = f(a) + f(b)
for i in range(1, n):
result += 2 * f(a + i * h)
result = (h / 2) * result
print("Integration by trap is:", result)
return result
def f(x):
return (x + 1)**3
𝟏,𝟒
Q4: Solve the integral ∫𝟎,𝟐 (𝒔𝒊𝒏𝒙 − 𝒍𝒐𝒈𝒙 + 𝒆𝒙 )𝒅𝒙 using the trapezoidal rule (h=0.2)
from math import sin, log, exp
def trapezoidal(f, a, b, n):
h = float(b - a) / n
result = f(a) + f(b)
for i in range(1, n):
result += 2 * f(a + i * h)
result = (h / 2) * result
print("Integration by trap is:", result)
return result
def f(x):
return sin(x) - log(x) + exp(x)
𝟐.𝟓
Q5: Evaluate∫𝟎 𝒆𝒙 𝒅𝒙 using the trapezoidal rule (h=0.5)
from math import exp
def trapezoidal(f, a, b, n):
h = float(b - a) / n
result = f(a) + f(b)
for i in range(1, n):
result += 2 * f(a + i * h)
result = (h / 2) * result
print("Integration by trap is:", result)
return result
def f(x):
return exp(x)
trapezoidal(f, 0, 2.5, 5)
Output:
Integration by trap is: 5.625
Practical – 12
Numerical Integration - II
Name of Student:
Roll No:
Batch:
𝟏𝟎
Q1: Evaluate ∫𝟎 (𝟏 + 𝒙𝟐 )𝒅𝒙 using Simpson’s (1/3)rd rule (h=0.25)
def simpsons13(f, a, b, n):
h = float(b - a) / n
result = f(a) + f(b)
result = (h / 3) * result
print("Simpson 1/3rd Result:", result)
return result
def f(x):
return sqrt(1 + x**2)
𝟓
Q2: Evaluate ∫𝟎 𝒆𝒙 𝒅𝒙 using Simpson’s (1/3)rd rule (8 equal intervals)
def simpsons13(f, a, b, n):
h = float(b - a) / n
result = f(a) + f(b)
result = (h / 3) * result
print("Simpson 1/3rd Result:", result)
return result
def f(x):
return exp(x)
simpsons13(f, 0, 5, 8)
Output:
Simpson 1/3rd Result: 32.684613110896976
𝟏𝟎
Q3: Evaluate ∫𝟎 √(𝟏 + 𝒙𝟐 ) 𝒅𝒙 using Simpson’s (3/8)th rule (h=0.2)
def simpsons38(f, a, b, n):
h = float(b - a) / n
result = f(a) + f(b)
result = (3 * h / 8) * result
print("Simpson 3/8th Result:", result)
return result
def f(x):
return sqrt(1 + x**2)
simpsons38(f, 0, 10, 5)
Output:
Simpson 3/8th Result: 13.318559665215194
𝟐
Q4: Estimate ∫𝟎 (𝐱² + 𝟐𝐱 − 𝟖) 𝐝𝐱 using Simpson’s (1/3)rd rule (h=0.25)
def simpsons13(f, a, b, n):
h = float(b - a) / n
result = f(a) + f(b)
result = (h / 3) * result
print("Simpson 1/3rd Result:", result)
return result
def f(x):
return x**2 + 2*x - 8
simpsons13(f, 0, 2, 8)
Output:
Simpson 1/3rd Result: -3.145833333333333
𝟓
Q5: Estimate ∫𝟎 𝐜𝐨𝐬(𝐱) 𝐝𝐱 using Simpson’s (3/8)th rule (h=0.5)
def simpsons38(f, a, b, n):
h = float(b - a) / n
result = f(a) + f(b)
result = (3 * h / 8) * result
print("Simpson 3/8th Result:", result)
return result
def f(x):
return cos(x)
simpsons38(f, 0, 5, 10)
Output:
Simpson 3/8th Result: 0.7343268508376896