PYTHON LAB A Report Submitted to Theni College of Arts & Science (Affiliated to Madurai Kamarajar University) In partial fulfillment of Department of computer science Department of computer science Theni College of Arts & Science Veerapandi. Theni College of Arts and Science, Veerapandi (Affiliated to Madurai Kamaraj University) Department of computer science Bonafide Certificate University Register no certified that this is the Bonafide record of Practical work done by the student Subject Class (Subject Code: Head of the department ) in the computer lab of this college held on Lecturer Incharge Submitted to the University Practical Examination of Madurai Kamaraj University in Theni College of Arts and Science. Internal Examiner External Examiner INDEX S.N DATE o CONTENTS 1 2 3 4 5 6 Temperature conversion Check the student grade Area of shapes Fibonacci series Finding factorial Sum of the series for 1-2/2!+3/3! 7 8 Matrix of sum and product Create mathematical 3D objects 9 Histogram 10 11 12 Sine and cos waves Pulserate verses height Graph for time and velocity 13 Grape for time and distance 14 15 Grape for velocity and distance Wire frame PAGE SIGN No Ex.No.1 Date: TEMPERATURE CONVERSION Aim: To write a python program to calculate the temperature conversion. To convert Fahrenheit to Celsius and Celsius to Fahrenheit Program: print("\t\t*****MENU*****") print("1.From Fahrenheit to Celsius") print("2.From Celsius to Fahrenheit ") print("3.Exit") choice = int(input("Enter choice(1/2/3):")) if (choice == 1): F = int(input("what is the Fahrenheit? ")) C=(F - 32) * 5/9 print("Celsius is = ",C) elif (choice == 2): C = int(input("what is the Celsius? ")) F= (C * 9/5) + 32 print("Fahrenheit is = ",F) elif (choice == 3): exit() else: print("Invalid input") Output: *****MENU***** 1.From Fahrenheit to Celsius 2.From Celsius to Fahrenheit 3.Exit Enter choice(1/2/3):1 what is the Fahrenheit? 3 Celsius is = -16.11111111111111 *****MENU***** 1.From Fahrenheit to Celsius 2.From Celsius to Fahrenheit 3.Exit Enter choice(1/2/3):2 what is the Celsius? -15 Fahrenheit is = 5.0 Result: The above program has been executed successfully Ex.No.2 Date: CHECK THE STUDENT GRADE Aim: Write a python program to check our student grade for the given marks 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: ")) avg=(sub1+sub2+sub3)/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: E") Output: Enter marks of the first subject: 80 Enter marks of the second subject: 90 Enter marks of the third subject: 70 Grade: A Result: The above program has been executed successfully Ex.No.3 AREA OF SHAPES Date: Aim: Write a python program to find out and display the area calculation for the given shapes Program: def square(length): area_square = length ** 2 print ("the area of your square equals %s" % (area_square)) def rectangle(length, width): area = length * width print ("The area of your rectangle equals %s" %(area)) def triangle(height, base): area_triangle = height * base * 0.5 print ("the area of your triangle equals %s" % (area_triangle)) def circle(pi,radius): area_circle = pi * (radius ** 2) print (" the area of your circle equals %s" % (area_circle)) print ("\t Area calculator") print (" def calculation(): ") print (" \t MENU") print (" ") print ("1.Square, 2.Rectangle, 3.Triangle, 4.Circle 5.Exit") print ("Enter your choice") user_shape = int(input()) if user_shape == 1: square(length = float(input("please type the length of the square"))) elif user_shape == 2: rectangle(length = float(input("please type the length of the rectangle")), width = float(input("please type the width of the rectangle"))) elif user_shape == 3: triangle(height = float(input("please type the height of the triangle")), base = float(input("please type the base length of the triangle"))) elif user_shape == 4: circle(3.14, radius = float(input("please type the radius of thecircle"))) elif user_shape == 5: exit() else: print ("That's not in the choices!, Try again") calculation() calculation() choice = input("Would you like to calculate the area of a different shape?(yes/no)") while choice == "yes": print (" calculation() ") Output: MENU 1.Square, 2.Rectangle, 3.Triangle, 4.Circle 5.Exit Enter your choice 1 please type the length of the square5 the area of your square equals 25.0 Would you like to calculate the area of a different shape?(yes/no)yes MENU 1.Square, 2.Rectangle, 3.Triangle, 4.Circle 5.Exit Enter your choice 2 please type the length of the rectangle5 please type the width of the rectangle6 The area of your rectangle equals 30.0 MENU 1.Square, 2.Rectangle, 3.Triangle, 4.Circle 5.Exit Enter your choice 3 please type the height of the triangle10 please type the base length of the triangle2 the area of your triangle equals 10.0 MENU 1.Square, 2.Rectangle, 3.Triangle, 4.Circle 5.Exit Enter your choice 4 please type the radius of the circle5 the area of your circle equals 78.5 Result: The above program has been executed successfully Ex.No.4 Date: FIBONACCI SERIES Aim: Write a python program to display the Fibonacci series for the given times Program: n = int(input("Please Enter the Range Number: ")) i=0 first = 0 second = 1 while(i < n): if(i <= 1): Next = i else: Next = first + second first = second second = Next print(Next) i=i+1 Output: Please Enter the Range Number: 5 0 1 1 2 3 Result: The above program has been executed successfully Ex.No.5 Date: FINDING FACTORIAL Aim: To write a python program to find and display the factorial value for the given number Program: n=int(input("Enter number:")) fact=1 while(n>0): fact=fact*n n=n-1 print("Factorial of the number is: ") print(fact) Output: Enter number:5 Factorial of the number is: 120 Result: The above program has been executed successfully Ex.No.6 Date: SUM OF THE SERIES FOR 1 – 2/2! + 3/3! --------- n/n! Aim: Write a python program to to find sum of the following series for n terms: 1 – 2/2! + 3/3! - - - - - n/n! Program: def sumofseries(num): res = 0 fact = 1 for i in range(1, num+1): fact *= i if(i%2==0): res = res - (i/ fact) else: res = res + (i/ fact) return res n =int(input("Enter the range")) print("Sum: ", sumofseries(n)) Output: Enter the range 4 Sum: 0.33333333333333337 Result: The above program has been executed successfully Ex.No.7 Date: MATRIX OF SUM AND PRODUCT Aim: Write a python program to calculate the matrix of sum and product for the given two matrices and display it Program: X = [[1,2,3], [4,5,6], [7,8,9]] Y = [[1,2,3], [4,5,6], [7,8,9]] result = [[0,0,0], [0,0,0], [0,0,0]] print("Matrix Addition") for i in range(len(X)): for j in range(len(Y[0])): result[i][j] += X[i][j] + Y[i][j] for r in result: print(r) print("Matrix Multiplication") 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) Output: Matrix Addition [2, 4, 6] [8, 10, 12] [14, 16, 18] Matrix Multiplication [32, 40, 48] [74, 91, 108] [116, 142, 168] Result: The above program has been executed successfully Ex.No.8 Date: CREATE MATHEMATICAL 3D OBJECTS Aim: Write a python program and to create mathematical 3D objects in visual python Program: import matplotlib.pyplot as plt from matplotlib.patches import Ellipse plt.axes() print ("MENU") print ("1-Circle") print ("2-rectangle") print ("3-Arrow") print ("4-Ellipse") print ("5-Polygon") ch=int(input("enter Choice = ")) if ch==1: circle = plt.Circle((0, 0), radius=0.75, fc='y') plt.gca().add_patch(circle) elif ch==2: rectangle = plt.Rectangle((10, 10), 100, 100, fc='r') plt.gca().add_patch(rectangle) elif ch==3: arrow = plt.Arrow(1, 1, 0.5, 0.5) plt.gca().add_patch(arrow) elif ch==4: ellipse = Ellipse((3, 3), 0.4, 0,2) plt.gca().add_patch(ellipse) elif ch==5: points = [[2, 1], [6, 1], [8, 4]] line = plt.Polygon(points,facecolor='g', edgecolor='r', linewidth=3.0) plt.gca().add_patch(line) else: print ("Invalid Choice") plt.axis('scaled') plt.show() Output: MENU 1- Circle 2- Rectangle 3-Arrow 4-Ellipse 5-Polygon Enter choice = 1 Enter choice = 2 Enter choice = 4 Result: The above program has been executed successfully Enter choice = 3 Enter choice = 5 Ex.No.9 Date: HISTOGRAM Aim: Write a python program and display the histogram in visual python Program: import numpy as np import matplotlib.pyplot as plt data = [1,11,21,31,41] plt.hist([1,11,21,31,41,51], bins=[0,10,20,30,40,50,60], weights=[10,1,40,33,6,8], edgecolor="red") plt.show() Output: Result: The above program has been executed successfully Ex.No.10 Date: SINE AND COS WAVES Aim: Write a python program to display the sine and cos waves in the visual python Program: import matplotlib.pyplot as plt import numpy as np st=np.arange(0,10,0.1) ct=np.arange(0,10,0.1) sine=np.sin(st) cos=np.cos(ct) plt.plot(st,sine) plt.plot(ct,cos) plt.title("Sine, Cos, Polynomial and Exponential Waves") plt.xlabel("Time") plt.ylabel("Sin and Cos (Time)") plt.show() Output: Result: The above program has been executed successfully Ex.No.11 Date: PULSERATE VERSES HEIGHT Aim: Write a python program to display the graph for pulse rate and height variance Program: import numpy as np import matplotlib.pyplot as plt x=[ ] y=[ ] for n in range(6): p = int(input('Enter pulse rate')); h = int(input('Enter the height')); x.append(p) y.append(h) plt.plot(x,y) plt.title("Pulse rate vs Height") plt.xlabel("Pulse rate"); plt.ylabel("Height"); plt.show() Output: Enter pulse rate110 Enter the height170 Enter pulse rate100 Enter the height160 Enter pulse rate95 Enter the height155 Enter pulse rate90 Enter the height150 Enter pulse rate85 Enter the height140 Enter pulse rate80 Enter the height130 Result: The above program has been executed successfully GRAPH FOR TIME AND VELOCITY Ex.No.12 Date: Aim: Write a python program and to display the graph for time and velocity variance in velocity acceleration Program: import numpy as np import matplotlib.pyplot as plt x=[ ] y=[ ] t=0 et =int(input('Enter end time')); a = int(input('Enter the Acceleration')); u = int(input('Enter the Initial Velocity')); while (t<=et): v=u+a*t x.append(t) y.append(v) t=t+1 plt.plot(x,y) plt.title("Velocity acceleration") plt.xlabel("Time"); plt.ylabel("Velocity"); plt.margins(0) plt.show() Output: Ex.No.13 Date: GRAPH FOR TIME AND DISTANCE Result: The above program has been executed successfully Aim: Write a python program and to display the graph for time and distance variance in velocity acceleration Program: import numpy as np import matplotlib.pyplot as plt x=[ ] y=[ ] t=0 et =int( input('Enter end time')); a = int(input('Enter the Acceleration')); u =int( input('Enter the Initial Velocity')); while t<=et: v=u+a*t d = v * t + 0.5 * a * t ** 2 x.append(t) y.append(d) t=t+1 plt.plot(x,y) plt.title("Velocity acceleration") plt.xlabel("Time"); plt.ylabel("Distance"); plt.margins(0) plt.show() Output: Ex.No.14 Date: GRAPH FOR VELOCITY AND DISTANCE Result: The above program has been executed successfully Aim: Write a python program and to display the graph for velocity and distance variance in velocity acceleration Program: import numpy as np import matplotlib.pyplot as plt x=[ ] y=[ ] t=0 et = input('Enter end time'); a = input('Enter the Acceleration'); u = input('Enter the Initial Velocity'); while t<=et: v=u+a*t s = (v * v - u * u) / 2 * a x.append(v) y.append(s) t=t+1 plt.plot(x,y) plt.title("Velocity acceleration") plt.xlabel("Velocity"); plt.ylabel("Distance"); plt.margins(0) plt.show() Output: Ex.No.(extra) Date: WIRE FRAME Result: The above program has been executed successfully Aim: Write a python program to display the wire frame in the visual python Program: from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt import numpy as np fig = plt.figure() ax = fig.add_subplot(111, projection='3d') u, v = np.mgrid[0:2*np.pi:200j, 0:np.pi:100j] x=np.cos(u)*np.sin(v) y=np.sin(u)*np.sin(v) z=np.cos(v) plt.title("Wireframe Example") ax.set_xlabel("x-Axis") ax.set_ylabel("y-Axis") ax.set_zlabel("z-Axis") ax.plot_wireframe(x, y, z, rstride = 5, cstride = 5, linewidth = 1) plt.show() Output: Result: The above program has been executed successfully