SCL - Lab Qns
SCL - Lab Qns
x=2
y=3
z=x+y
avg=z/2
print("average of",x,"and",y,"is:",avg)
Output:
average of 2 and 3 is: 2.5
2. Write a program to print complex numbers, its real and imaginary part. Also find
the magnitude of complex numbers.
i=complex(0,1)
a=5+2*i
print("Complex number is:",str(a))
print("The imaginary part of a is:"+str(a.imag))
mag=abs(a)
print("The magnitude of a is:"+str(mag))
print("Magnitude square is:",str(mag*mag))
Output:
Complex number is: (5+2j)
The imaginary part of a is: 2.0
The magnitude of a is: 5.385164807134504
Magnitude square is: 28.999999999999996
Output:
Enter radius: 10
Volume of sphere: 4188.790204786391
Output:
The roots are:(-1+1.4142135623730951j)and(-1-1.4142135623730951j)
Output:
The value of cos 45 in radians : 0.7071067811865476
The value of sin 60 in radians : 0.8660254037844386
Output:
Enter your number:5
Logarithm of 5.0 is: 1.6094379124341003
Exponential of 5.0 is: 148.4131591025766
8. Write a program to find sum of first N natural numbers.
sum=0
n=int(input("Enter value for n:"))
for i in range(n):
if i%2==0:
sum=sum+i
print("Sum of even numbers upto",n,":",sum)
Output:
enter value for n: 5
Sum of 5 natural numbers: 15
Output:
Enter value for n:20
Sum of even numbers upto 20 : 90
10. Write a program to find the sum of squares of odd numbers between two
numbers entered.
sum=0
n1=int(input("Enter value of starting:"))
n2=int(input("Enter value of ending:"))
for i in range(n1,n2+1):
if i%2!=0:
sum=sum+i*i
print("Sum of squares of odd numbers between",n1,"and",n2,":",sum)
Output:
Enter value of starting: 45
Enter value of ending: 87
Sum of squares of odd numbers between 45 and 87: 99374
11. Write a program to print numbers divisible by 7 between two values entered by
the user.
n1=int(input("enter value of starting integer:"))
n2=int(input("enter value of ending integer:"))
for i in range(n1,n2+1):
if i%7==0:
print(i)
Output:
enter value of starting integer:45
enter value of ending integer:87
49
56
63
70
77
84
12. Write a program to print numbers divisible by both 7 and 10 between two
numbers entered by user.
n1=int(input("enter value of starting integer:"))
n2=int(input("enter value of ending integer:"))
for i in range(n1,n2+1):
if i%7==0 and i%10==0:
print(i)
Output:
enter value of starting integer:45
enter value of ending integer:87
70
Output:
[13, 26, 39, 52, 65, 78, 91, 104, 117, 130]
14. Write a program to find sum and average of elements in a list and print it.
lst=[0,1,2,3,4,5]
l=len(lst)
sum=0.0
for i in range(l):
sum=sum+lst[i]
avg=sum/l
print("Sum of elements in the list :",sum)
print("Average of elements in the list :",avg)
Output:
Sum of elements in the list : 15.0
Average of elements in the list : 2.5
15. Write a program to search an element in a list, also print where the item was
found .
lst=[10,20,30,40,50]
l=len(lst)
search=int(input("Enter your search element: "))
count=0
for i in range(l):
if search==lst[i]:
print("Search item found at:",(i+1))
break
else:
count=count+1
if count==l:
print("Search item was not found")
Output:
Enter your search element: 40
Search item found at: 4
Enter your search element: 60
Search item was not found
16. Write a program using numpy module to find sum of cubes of elements in an
array and also find square of each element in the array.
import numpy as np
x=np.array([1,2,3,4,5,6,7])
print("The numpy array is:",x)
print("Sum of elements in the array is: ",sum(x))
sqr=x*x
sum_cubes=sum(x*x*x)
print("Sum of cubes: ",sum_cubes)
print("Square of each element in the list: ",sqr)
Output:
The numpy array is: [1 2 3 4 5 6 7]
Sum of elements in the array is: 28
Sum of cubes: 784
Square of each element in the list: [ 1 4 9 16 25 36 49]
17. Write a program using the concept of function to find factorial and square of a
number entered
def square(x):
y=x*x
return y
def factorial(x):
fact=1
if x==0:
return 1
else:
for i in range(1,x+1):
fact=fact*i
return fact
Output:
Enter your number 6
Factorial of the number: 720
Square of the number: 36
18. Write a program to find exponent of a number without using inbuilt functions.
def exp(x,n_terms):
exp_x=0
for i in range(n_terms):
exp_x=exp_x+(x**i/factorial(i))
return exp_x
def factorial(y):
fact=1
if y==0:
return 1
else:
for j in range(1,y+1):
fact=fact*j
return fact
import math as m
x1=int(input("Enter your number: "))
n_terms=10
print("Without using inbuilt function exponent is: ",exp(x1,n_terms))
print("Using inbuilt function exponent is: ",m.exp(x1))
Output:
Enter your number: 1
Without using inbuilt function exponent is: 2.7182815255731922
Using inbuilt function exponent is: 2.718281828459045
Output:
Output:
21. Write a program to plot a bar graph,scatter graph and pie graph in one page.
Output:
22. Write a program to plot graphs of sin(t), cos(t), sinh(t) and cosh(t) in one page.
Output:
23. Write a program to create a 3x10 matrix and reshape it into 6x5.
import numpy as np
a1=[]
a1=np.arange(1,31)
b1=a1.reshape([3,10])
print("The 3X10 matrix is: ")
print(b1)
c1=b1.reshape([6,5])
print("The 6X5 matrix is:")
print(c1)
Output:
The 3X10 matrix is:
[[ 1 2 3 4 5 6 7 8 9 10]
[11 12 13 14 15 16 17 18 19 20]
[21 22 23 24 25 26 27 28 29 30]]
The 6X5 matrix is:
[[ 1 2 3 4 5]
[ 6 7 8 9 10]
[11 12 13 14 15]
[16 17 18 19 20]
[21 22 23 24 25]
[26 27 28 29 30]]
24. Write a program to find rank, inverse, eigen value, eigen vector and transpose of a
matrix.
import numpy as np
from numpy.linalg import eig
a2=np.array([[0,2],[2,3]])
print("The matrix is:")
print(a2)
a2_rev=np.linalg.inv(a2)
print("INVERSE OF MATRIX")
print(a2_rev)
a2_rank=np.linalg.matrix_rank(a2)
print("RANK OF MATRIX:")
print(a2_rank)
a2_trans=np.transpose(a2)
print("TRANSPOSE OF MATRIX")
print(a2_trans)
eig_vector_a2,eig_value_a2=eig(a2)
print("Eigen values:",eig_value_a2)
print("Eigen vectors:",eig_vector_a2)
eig_vector_trans,eig_value_trans=eig(a2_trans)
print("Eigen value of transpose:",eig_value_trans)
print("Eigen vectors of transpose:",eig_vector_trans)
Output:
The matrix is:
[[0 2]
[2 3]]
INVERSE OF MATRIX
[[-0.75 0.5 ]
[ 0.5 0. ]]
RANK OF MATRIX:
2
TRANSPOSE OF MATRIX
[[0 2]
[2 3]]
Eigen values: [[-0.89442719 -0.4472136 ]
[ 0.4472136 -0.89442719]]
Eigen vectors: [-1. 4.]
Eigen value of transpose: [[-0.89442719 -0.4472136 ]
[ 0.4472136 -0.89442719]]
Eigen vectors of transpose: [-1. 4.]
25. Write a program to plot functions of sin(t), cos(t) for the vector t=[0,10] with
increment of 0.1. Also find and plot its 1st and 2nd derivative.
import numpy as np
import matplotlib.pylab as plt
#sin(t)
t=np.arange(0,10,0.1)
plt.subplot(2,3,1)
plt.title("sin(t)")
y=np.sin(t)
plt.plot(y)
#y1=sin'(t)
plt.subplot(2,3,2)
plt.title("sin'(t)")
y1=np.diff(y)
plt.plot(y1)
#y2=sin''(t)
plt.subplot(2,3,3)
plt.title("sin''(t)")
y2=np.diff(y1)
plt.plot(y2)
#cos(t)
plt.subplot(2,3,4)
plt.title("cos(t)")
a=np.cos(t)
plt.plot(a)
#cos'(t)
plt.subplot(2,3,5)
plt.title("cos'(t)")
a1=np.diff(a)
plt.plot(a1)
#cos''(t)
plt.subplot(2,3,6)
plt.title("cos''(t)")
a2=np.diff(a1)
plt.plot(a2)
plt.show()
Output:
26. Write a program to express the function f(t)=x5 and its derivative.
import numpy as np
import sympy as sp
x=sp.Symbol("X")
f=sp.pprint(x**5)
y=sp.diff(x**5)
sp.pprint(y)
Output:
X5
5X4
27. Write a program to express e|t| and also find its integration in the range (-3,3).
import sympy as sp
t=sp.Symbol("e")
f=sp.exp(-t)
z=sp.integrate(f,(t,-3,0))
sp.pprint(z)
f1=sp.exp(t)
y=sp.integrate(f1,(t,0,3))
sp.pprint(y)
x=y+z
sp.pprint(x)
Output:
-1 + e3
-1 + e3
-2 + 2⋅ℯ3
28. Write a program to express 3t4+5 and its 1st and 2nd differential in the range (-3, 3).
import matplotlib.pyplot as plt
import numpy as np
t=np.arange(-3,3,1)
a=(3*t**4)+5
plt.subplot(1,3,1)
plt.plot(a)
plt.title("f(t)")
plt.subplot(1,3,2)
b=np.diff(a)
plt.plot(b)
plt.title("f'(t)")
plt.subplot(1,3,3)
c=np.diff(b)
plt.title("f''(t)")
plt.plot(c)
plt.show()
Output:
29.Write a program to create an empty 10x10 matrix with zeroes as input. Then
change values as :
a[i][j]=i+j+1 where 1<=i<=10 and 1<=j<=10.
import numpy as np
n_row=10
n_column=10
x=np.zeros([n_row,n_column])
print(x)
for i in range(n_row):
for j in range(n_column):
x[i][j]=(i+1)+(j+1)+1
print(x)
Output:
[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
[[ 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.]
[ 4. 5. 6. 7. 8. 9. 10. 11. 12. 13.]
[ 5. 6. 7. 8. 9. 10. 11. 12. 13. 14.]
[ 6. 7. 8. 9. 10. 11. 12. 13. 14. 15.]
[ 7. 8. 9. 10. 11. 12. 13. 14. 15. 16.]
[ 8. 9. 10. 11. 12. 13. 14. 15. 16. 17.]
[ 9. 10. 11. 12. 13. 14. 15. 16. 17. 18.]
[10. 11. 12. 13. 14. 15. 16. 17. 18. 19.]
[11. 12. 13. 14. 15. 16. 17. 18. 19. 20.]
[12. 13. 14. 15. 16. 17. 18. 19. 20. 21.]]
30. Write a program to find determinant of a matrix and perform matrix addition.
import numpy as np
from numpy import linalg
a=np.array([[1,2],[2,4]])
b=np.array([[2,4],[1,2]])
det_a=np.linalg.det(a)
print("Determinant of a:")
print(det_a)
print("MATRIX ADDITION:")
print(a+b)
Output:
Determinant of a:
0.0
MATRIX ADDITION:
[[3 6]
[3 6]]