0% found this document useful (0 votes)
220 views8 pages

Python Lab Programs

The document summarizes 15 Python programs and SQL queries. The Python programs cover topics like recursion, file handling, random number generation, lists, strings, plotting, web scraping, and loans. The SQL queries find aggregate values from tables, count customers by country with group by, and order a table in descending order. The last item describes a Django web server that parses POST requests and writes to a CSV file.

Uploaded by

Prabhu Chris
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
220 views8 pages

Python Lab Programs

The document summarizes 15 Python programs and SQL queries. The Python programs cover topics like recursion, file handling, random number generation, lists, strings, plotting, web scraping, and loans. The SQL queries find aggregate values from tables, count customers by country with group by, and order a table in descending order. The last item describes a Django web server that parses POST requests and writes to a CSV file.

Uploaded by

Prabhu Chris
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

Python Lab Programs

1) Program to find the Factorial of a natural number by using Recursive function.

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.

2) Program to Read a file line by line and print it.


Aim: To write a python program to read a file line by line and print it.
Program:
Text book Pg no: 181 (Code Snippet:5)
Output:
Result: Thus the Python program to read a file line by line is written and executed successfully.

3) Program to create a Random Number Generator


Aim: To write a random number generator python program that randomly generates the numbers
between 1 and 6.
Program:
import random
def startTheGame():
counter = 0
myList = [ ]
while(counter) < 6:
randomNumber = random.randint(1,6)
myList.append(randomNumber)
counter = counter + 1
if (counter)>=6:
pass
else:
return myList
Output:
5
Result: Thus the Python program that generates the random number was written and executed
successfully.

4) Program to find the sum of all elements of a list by using recursion.


Aim: To write a Python program to find the sum of all elements in a list by using Recursive
Function.
Program:
list1 = [11, 5, 17, 18, 23]
def sumOfList(list, size):
if (size == 0):
return 0
else:
return list[size - 1] + sumOfList(list, size - 1)
total = sumOfList(list1, len(list1))
print("Sum of all elements in given list: ", total)
Output:
Sum of all elements in given list:74
Result: Thus the python program to find the sum of all elements of a list by using recursive
function was written and executed successfully.

5) Program to compute the nth Fibonacci number by using Recursive Function.


Aim: To write a Python program to calculate the Fibonacci number by using recursive function.
Program:
def Fibonacci(n):
if n<0:
print("Incorrect input")
elif n==1:
return 0
elif n==2:
return 1
else:
return Fibonacci(n-1)+Fibonacci(n-2)
print(Fibonacci(9))
Output:
Result: Thus the Python program to calculate the nth fibonacci number by using recursive
function was written and executed successfully.
6) Program to implement a stack using a list

Aim: To write a Python program to implement stack operations in a list.


Program:
Text book Pg no: 336 (Pgm:10.6)
Output:
Result: Thus the Python program to implement stack operations using a list was written and
executed successfully.

7) Program to implement a queue using a list

Aim: To Write a Python program to implement queue operations in a list.


Program:
Text book Pg no: 349 (Pgm:10.2)
Output:
Result: Thus the python program to implement stack operations using a list was written and
executed successfully.

8) Program to test a string is palindrome or not.

Aim: To write a Python program to test a given string is palindrome or not.


Program:
def isPalindrome(str):
for i in xrange(0, len(str)/2):
if str[i] != str[len(str)-i-1]:
return False
return true
s = "malayalam"
ans = isPalindrome(s)
if (ans):
print("Yes")
else:
print("No")
Output:
Yes
Result: Thus the python program to test a given string is palindrome or not 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.

10) Opening a webpage using the urllib library.

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.

SQL and WebServer:

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.

Text book Pg no: 502

You might also like