Python Lab Programs
Python Lab Programs
Aim: To write a Python program to find the factorial of a natural number by using recursive
function.
Program:
def recur_factorial(n):
if n = = 1:
return n
else:
return n*recur_factorial(n-1)
num = int(input("Enter a number: "))
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num = = 0:
print("The factorial of 0 is 1")
else:
print("The factorial of",num,"is",recur_factorial(num))
Output:
Enter a number: 4
The factorial of 4 is 24
Result: Thus the Python program to find the factorial of a natural number is written and executed
successfully.
9) Python program to plot the function y = x^2 using pyplot or matplotlib libraries.
Aim: To write a Python program to plot the function y=x^2 using pyplot or matplotlib libraries.
Program:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange( -10 , 10 )
y = np.square( x )
plt.plot( x , y )
plt.show()
Output:
Result: Thus the Python program to plot the function y=x^2 using pyplot or matplotlib libraries.
Aim: To write a Python program to open a webpage using the urllib library.
Program:
Text book Pg no:157 (Pgm:4.4)
Output:
Result: Thus the python program to open a webpage using the urllib library was written and
executed successfully.
11) Program to Compute EMIs for a loan using the numpy or scipy libraries.
Aim: To create a python program to compute EMIs for a loan using the numpy or scipy libraries.
Program:
import numpy as np
interestRate = 0.07
numberOfMonths = 25*12;
principalBorrowed = 3500000
principal2Pay = np.ppmt(interestRate/12, 1, numberOfMonths, principalBorrowed);
interest2Pay = np.ipmt(interestRate/12, 1, numberOfMonths, principalBorrowed);
print("Loan amount:%7.2f"%principalBorrowed);
print("Loan duration in months:%d"%numberOfMonths);
print("Annual Interest Rate in percent:%2.2f"%(interestRate*100));
print("Principal to be paid:%5.2f"%abs(principal2Pay));
print("Interest to be paid:%5.2f"%abs(interest2Pay));
print("Principal+Interest, to be paid:%5.2f"%abs(principal2Pay+interest2Pay));
Output:
Loan amount:3500000.00
Loan duration in months:300
Annual Interest Rate in percent:7.00
Principal to be paid:4320.61
Interest to be paid:20416.67
Principal+Interest, to be paid:24737.27
Result: Thus the Python program to calculate the EMIs for a loan using the numpy or scipy
libraries was written and executes successfully.
12) Find the min, max, sum, and average of the marks in a student marks table.
Aim: To write a query to find the minimum ,maximum and average of the marks in a student
marks table.
Queries:
Output:
Result: Thus the SQL query to find the maximum,minimum, sum and average was written and
executed sucessfully.
13) Find the total number of customers from each country in the table (customer ID,
customer name, country) using group by.
Aim: To write a query to count the total number of customers from each country in the table by
using group by.
Queries:
Output:
Result: Thus the SQL query to count the total number of customers from each country in the
table by using group by was written and executed sucessfully.
14) Write a SQL query to order the (student ID, marks) table in descending order of
the marks.
Aim: To write a query to order the table in descending order.
Queries:
Result: Thus the SQL query to order the table in descending order was written and executed
sucessfully.
15) Write a Django based web server to parse a user request (POST), and write it to
a CSV file.