[Go to site: main page, start]

0% found this document useful (0 votes)
4 views23 pages

Python Programming Lab Manual

The document contains a series of Python programs that perform various calculations and visualizations, including temperature conversions, student grading, area calculations, Fibonacci series, factorials, and matrix operations. It also includes graphical representations of mathematical functions, pulse vs height graphs, and chemical reaction mass calculations. Each section provides a program followed by example outputs demonstrating the functionality.

Uploaded by

Thowfic Ahamed
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views23 pages

Python Programming Lab Manual

The document contains a series of Python programs that perform various calculations and visualizations, including temperature conversions, student grading, area calculations, Fibonacci series, factorials, and matrix operations. It also includes graphical representations of mathematical functions, pulse vs height graphs, and chemical reaction mass calculations. Each section provides a program followed by example outputs demonstrating the functionality.

Uploaded by

Thowfic Ahamed
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Fahrenheit to Celsius and Vice Versa

Program:
print("Select operation.")

print("[Link] to Celsius")

print("[Link] to Fahrenheit")

choice = input("Enter choice(1/2):")

if choice == 1:

fah = input("Enter Temperature in Fahrenheit: ")

fahrenheit = float(fah)

celsius = (fahrenheit-32)/1.8

print("Temperature in Celsius =%d" %celsius)

elif choice == 2:

cel = input("Enter Temperature in Celsius: ")

celsius = float(cel)

fahrenheit = (1.8*celsius) + 32

print("Temperature in Fahrenheit =%d" %fahrenheit)

else:

print("Invalid input")

1
Output:

Select operation.

[Link] to Celsius

[Link] to Fahrenheit

Enter choice(1/2):1

Enter Temperature in Fahrenheit: 98.5

Temperature in Celsius = 36.94444444444444

Enter choice(1/2):2

Enter Temperature in Celsius: 36.944

Temperature in Fahrenheit = 98.4992

2
2. Student Mark List
Program:
sub1=int(input("Enter marks of the first subject: "))

sub2=int(input("Enter marks of the second subject: "))

sub3=int(input("Enter marks of the third subject: "))

tot=sub1+sub2+sub3

avg=tot/3

if(avg>=80):

print("Grade: A")

elif(avg>=70 and avg<80):

print("Grade: B")

elif(avg>=60 and avg<70):

print("Grade: C")

elif(avg>=40 and avg<60):

print("Grade: D")

else:

print("Grade: F")

Output:

Enter the marks

Enter marks of the first subject: 65

Enter marks of the second subject: 75

Enter marks of the third subject: 55

Grade: C

3
3. Calculate area of Square, Circle, Triangle,
Rectangle

Program:
print("Select operation.")

print("[Link] of Square")

print("[Link] of Circle")

print("[Link] of Triangle")

print("[Link] of Rectangle")

choice = input("Enter choice(1/2/3/4):")

if choice == 1:

side = input("Enter side length of Square: ");

side_length = int(side);

area_square = side_length*side_length;

print("\nArea of Square =%d" %area_square);

elif choice == 2:

rad = input("Enter radius of circle: ");

radius = float(rad);

area = 3.14 * radius * radius;

print("\nArea of Circle = %0.2f" %area);

elif choice == 3:

side1 = input("Enter length of first side: ");

side2 = input("Enter length of second side: ");

side3 = input("Enter length of third side: ");

4
a = float(side1);

b = float(side2);

c = float(side3);

s = (a + b + c)/2;

area = (s*(s-a)*(s-b)*(s-c)) ** 0.5;

print("\nArea of Triangle = %0.2f" %area);

elif choice == 4:

leng = input("Enter length of Rectangle: ");

brea = input("Enter breadth of Rectangle: ");

length = int(leng);

breadth = int(brea);

area = length*breadth;

print("\nArea of Rectangle =%d" %area);

else:

print("Invalid input")

Output:

Select operation.

1. Area of Square

2. Area of Circle

3. Area of Triangle

4. Area of Rectangle

Enter choice (1/2/3/4):1

Enter side length of Square: 6

Area of Square = 36

5
Enter choice (1/2/3/4):2

Enter radius of circle: 7

Area of Circle = 153.86

Enter choice (1/2/3/4):3

Enter length of first side: 6

Enter length of second side: 7

Enter length of third side: 9

Area of Triangle = 20.98

Enter choice (1/2/3/4):4

Enter length of Rectangle: 9

Enter breadth of Rectangle: 7

Area of Rectangle = 63

6
4. Fibonacci Series

Program:
nterms = int(input("How many terms? "));

n1 = 0

n2 = 1

count = 0

if nterms <= 0:

print "Please enter a positive integer"

elif nterms == 1:

print("Fibonacci sequence upto :%d" %nterms);

print n1

else:

print("Fibonacci sequence upto :%d" %nterms);

while count < nterms:

print n1

nth = n1 + n2

n1 = n2

n2 = nth

count += 1

Output:
How many terms? 10

Fibonacci sequence upto: 10

0 1 1 2 3 5 8 13 21 34

7
5. Factorial Number

Program:
num = input("Enter a number to find its factorial: ");

number = int(num);

if number == 0:

print("\nFactorial of 0 is 1");

elif number < 0:

print("\nFactorial of negative numbers doesn't exist..!!");

else:

fact = 1;

print("\nFactorial of %d" %number)

for i in range(1, number+1):

fact = fact*i;

print(fact);

Output:

Enter a number to find its factorial: 5

Factorial of 5

24

120

8
6. Sum of Natural Numbers

Program:
print("Enter '0' for exit.")

num = int(input("Upto which number ? "))

if num == 0:

exit();

elif num < 1:

print("Kindly try to enter a positive number..exiting..")

else:

sum = 0;

while num > 0:

sum += num;

num -= 1;

print("Sum of natural numbers =%d " %sum);

Output:

Enter '0' for exit.

Upto which number ? 10

Sum of natural numbers= 55

9
7. Sum and Product of the Matrices

Program:
X = [[12,7,3],

[4 ,5,6],

[7 ,8,9]]

Y = [[5,8,1],

[6,7,3],

[4,5,9]]

result = [[0,0,0],

[0,0,0],

[0,0,0]]

print("Sum of two matrix");

for i in range(len(X)):

for j in range(len(X[0])):

result[i][j] = X[i][j] + Y[i][j]

for r in result:

print(r)

print("Product of two matrix");

for i in range(len(X)):

for j in range(len(Y[0])):

for k in range(len(Y)):

result[i][j] += X[i][k] * Y[k][j]

for r in result:

print(r)

10
Output:

Sum of two matrix

[17, 15, 4]

[10, 12, 9]

[11, 13, 18]

Product of two matrix

[131, 175, 64]

[84, 109, 82]

[130, 170, 130]

11
8. Mathematical Objects in 3D Images

Program:
from visual import *

print("Select operation.");

print("[Link]");

print("[Link]");

print("[Link]");

print("[Link]");

print("[Link]");

print("[Link]");

choice = input("Enter choice(1/2/3/4/5/6):")

if choice == 1:

fig1 = curve(pos=[(0,0,0), (1,0,0), (2,1,0)], radius=0.05)

elif choice == 2:

fig2 = sphere(pos=(1,2,1), radius=0.5)

elif choice == 3:

fig3 = cone(pos=(5,2,0), axis=(12,0,0),radius=1)

elif choice == 4:

fig4 = arrow(pos=(0,2,1), axis=(5,0,0), shaftwidth=1)

elif choice == 5:

fig5 = ring(pos=(1,1,1), axis=(0,1,0), radius=0.5,


thickness=0.1)

elif choice == 6:

fig6 = cylinder(pos=(0,2,1),axis=(5,0,0), radius=1)

else:

12
print("Invalid input")

Output:

13
9. Histogram for the Numbers

Program:
def histogram( items ):

for n in items:

output = ''

times = n

while( times > 0 ):

output += '*'

times = times - 1

print(output)

histogram([2, 3, 6, 5])

Output:
Histogram for the numbers:

**

***

******

*****

14
10. Show Sine, Cosine, Exponential, Polynomial
Curves

Program:
print("Select Operation")

choice=input("Enter the choice1/2/3:")

if choice == 1:

import numpy, matplotlib

from numpy import sin, cos, pi

from matplotlib import pyplot as plt

x = [Link](-pi,pi,100)

ysin=sin(x)

ycos=cos(x)

def Create_plot(c,v,b,n,m):

[Link](c,v)

[Link](c,b)

[Link]("y")

[Link]("x")

[Link]((n,m))

[Link]('Plot of sin(x) and cos(x) from -pi to pi')

[Link]()

Create_plot(x,ysin,ycos,'sin(x)','cos(x)')

15
elif choice == 2:

import numpy as np

import [Link] as plt

a = 5

b = 2

c = 1

x = [Link](0, 10, 256, endpoint = True)

y = (a * [Link](-b*x)) + c

[Link](x, y, '-r', label=r'$y = 5e^{-2x} + 1$')

axes = [Link]()

axes.set_xlim([[Link](), [Link]()])

axes.set_ylim([[Link](), [Link]()])

[Link]('x')

[Link]('y')

[Link]('Exponential Curve')

[Link](loc='upper left')

[Link]()

elif choice == 3:

import numpy as np

import [Link] as plt

a = 3

b = 4

c = 2

x = [Link](0, 10, 256, endpoint = True)

y = (a * (x * x)) + (b * x) + c

16
[Link](x, y, '-g', label=r'$y = 3x^2 + 4x + 2$')

axes = [Link]()

axes.set_xlim([[Link](), [Link]()])

axes.set_ylim([[Link](), [Link]()])

[Link]('x')

[Link]('y')

[Link]('Polynomial Curve')

[Link](loc='upper left')

[Link]()

Output:

17
11. Calculate the pulse and height rate graph

Program:
import [Link] as inter

import numpy as np

import [Link] as plt

p, h = list(), list()

print("Pulse vs Height Graph:-\n")

n = input("How many records? ")

print("\nEnter the pulse rate values: ")

for i in range(int(n)):

pn = input()

[Link](int(pn))

x = [Link](p)

print("\nEnter the height values: ")

for i in range(int(n)):

hn = input()

[Link](int(hn))

y = [Link](h)

print("\nPulse vs Height graph is generated!")

z = [Link]([Link](), [Link](), 0.01)

s = [Link](x, y)

[Link] (x, y, 'b.')

[Link] (z, s(z), 'g-')

[Link]('Pulse')

18
[Link]('Height')

[Link]('Pulse vs Height Graph')

[Link]()

Output:
Pulse vs Height Graph:-

How many records? 5

Enter the pulse rate values: 1 2 3 4 5

Enter the height values: 15 9 20 12 18

Pulse vs Height graph is generated!

19
12. Calculate mass in a chemical reaction

Program:
import numpy as np

import [Link] as plot

def chemical_reaction(t):

m = 60/(t+2)

[Link] (t, m, 'b.')

[Link](t, m, '-g', label='m=60/(t+2), t>=0')

[Link]("calculate the mass m in a chemical reaction")

[Link](loc='upper left')

[Link]()

print "Wait for few seconds to generate chemical reaction graph"

print "See graphs in new figure windows"

t= [Link](100);

chemical_reaction(t)

20
Output:

21
13. Initial velocity & acceleration and plot
graph

Program:
import [Link] as plt

u=int(input('Enter intial velocity:'))

a=int(input('Enter acceleration:'))

v=[]

t=[1,2,3,4,5,6,7,8,9,10]

for i in t:

[Link](u + (a*i))

[Link](t,v)

[Link]([0,max(t)+2,0,max(v)+2])

[Link]('Time')

[Link]('Velocity')

[Link]()

s=[]

for i in t:

[Link](u*i+(0.5)*a*i*i)

[Link](t,s)

[Link]([0,max(t)+2,0,max(s)+2])

[Link]('Time')

[Link]('Distance')

[Link]()

s=[]

22
for i in v:

[Link]((i*i-u*u)/(2*a))

[Link](v,s)

[Link]([0,max(v)+2,0,max(s)+2])

[Link]('Velocity')

[Link]('Distance')

[Link]()

Output:
Enter intial velocity:60

Enter acceleration:10

23

You might also like