You are on page 1of 55

PRACTICAL - 1

To calculate income tax as per given slabs

# SOURCE CODE :-
print("------Income Tax Slab rate 2021-22------")
a=float(input("Enter the Amount:"))
if a>=0 and a<=250000:
print("Total payable tax is NIL")
elif a>250000 and a<=500000:
b=a-250000
print("Total payable tax is",b*5/100)
elif a>500000 and a<=750000:
b=a-500000
print("Total payable tax is",(b*10/100)+12500)
elif a>750000 and a<=1000000:
b=a-750000
print("Total payable tax is",(b*15/100)+12500+25000)
elif a>1000000 and a<=1250000:
b=a-1000000
print("Total payable tax is",(b*20/100)+12500+25000+37500)
elif a>1250000 and a<=1500000:
b=a-1250000
print("Total payable tax is",(b*25/100)+12500+25000+37500+50000)
elif a>1500000:
b=a-1500000
print("Total payable tax is ",(b*30/100)+12500+25000+37500+50000+62500)

# Output :-
PRACTICAL - 2
To print a pattern of numbers using nested loops

# SOURCE CODE :-
a=int(input("Enter the no of times:"))
for i in range(a):
print("*"*(i+1))

n=int(input("Enter the no of times:"))


for i in range(n):
for j in range(n-i):
print(j+1,end=' ')
print()

a=int(input("Enter the number of elements you


want:"))
for i in range (65,65+a):
for j in range(65,i+1):
print(chr(j),end="")
print()

# Output :-
PRACTICAL - 3
Sum of n natural numbers using recursion

# SOURCE CODE :-
def s(x):
if x<=1:
return 1
elif x>1:
return x+s(x-1)

a=int(input("Enter the number for summ of natural numbers:"))


y=s(a)
print(y)

# Output :-
PRACTICAL - 4
Calculate factorial using recursion

# SOURCE CODE :-
def fac(n):
if n<=1:
return n
elif n>1:
return n*fac(n-1)
x=int(input("Enter the number for factorial:"))
y=fac(x)
print("Factorial of",x,":-",y)

# Output :-
PRACTICAL - 5
Print fibonnaci series of n numbers using recursion

# SOURCE CODE :-
def fib(x):
if x<= 1:
return x
elif x>1:
return(fib(x-1) + fib(x-2))
a=int(input("Enter a number:"))
for i in range(a):
print(fib(i))

# Output :-
PRACTICAL - 6
Menu driven program to run the following operaters
on a text file

# SOURCE CODE :-
print("---menu driven program for text file---")
print("1] count uppercase")
print("2] count lowercase")
print("3] count vowels")
print("4] occurrences of a specific word")
print("5] count of 4 lettered words")
print("")
z=int(input("Enter the operation to run:"))
if z==1:
x=input("Enter the text file location:")
a=open(x,'r')
b=a.read()
n=0
for i in b:
if i.isupper():
n=n+1
print("Total number of uppercase:-",n)
a.close()
if z==2:
x=input("Enter the text file location:")
a=open(x,'r')
b=a.read()
n=0
for i in b:
if i.islower():
n=n+1
print("Total number of lowercase:-",n)
a.close()
if z==3:
x=input("Enter the text file location:")
a=open(x,'r')
b=a.read()
n=0
for i in b:
if i in ('a','e','i','o','u','A','E','I','O','U'):
n=n+1
print("Total number of vowels:-",n)
a.close()
if z==4:
x=input("Enter the text file location:")
a=open(x,'r')
b=a.read()
c=b.split()
n=0
word=input("Enter a word to find:")
for i in c:
if i==word:
n=n+1
print("Total number of occurrance of '",word,"'"":-
",n)
a.close()
if z==5:
x=input("Enter the text file location:")
a=open(x,'r')
b=a.read()
c=b.split()
n=0
for i in c:
if len(i)==4:
n=n+1
print("Total number of 4 lettered word:-",n)
a.close()

# Text File :-
# Output :-
PRACTICAL - 7
To display the longest and smallest line from the text file

# SOURCE CODE :-
z=input("Enter the location of the file:")
a=open(z,'r')
b=a.readlines()
c=0
for i in b:
if len(i)>c:
c=len(i)
for j in b:
if len(j)==c:
print("longest line:-",j)
d=c
for k in b:
if len(k)<d :
d=len(k)

for l in b:
if len(l)==d:
print("Smallest line:-",l)

# Text file :-
*Same as practical-6

# Output :-
PRACTICAL - 8
To read lines from text file and write line beginning with
‘T’ in another file
# SOURCE CODE :-
a=input("Enter the file location to read:")
b=input("Enter the file location to write:")
z=open(a,'r')
x=open(b,'w+')
c=z.readlines()
for l in c:
if l[0]=='T':
x.write(l)
z.close()
x.close()

# Text file :-

*Same as practical-6

# Output :-
PRACTICAL - 9
To add multiple lines in a given text file (with keyword)

# SOURCE CODE :-
a=input("Enter the file location:")
with open(a,'a+') as f:
n=int(input("Enter the number of lines to be
entered:"))
for i in range(n):
z=input("Enter the line:")
f.write("\n")
f.write(z)

# Text file :-
*Same as practical-6

# Output :-
PRACTICAL - 10
Create binary file with roll no.,name &marks search for
roll no., and display the record
# SOURCE CODE :-
import pickle as p
rec={}
while True:
print()
print("1]Add record")
print("2]search for a record by roll no.")
print("3]display all records")
print()
z=int(input("Enter your choice:"))
if z==1:
a=int(input("Enter the roll no.:"))
b=input("Enter the name :")
c=float(input("Enter the marks:"))
rec={"Rollno.":a,"Name":b,"Marks":c}
f=open("student.dat",'ab')
p.dump(rec,f)
f.close()

if z==2:
roll=int(input("Enter the roll no.:"))
f=open("student.dat",'rb')
while True:
try:
rec=p.load(f)
if rec["Rollno."]==roll:
print()
print("Roll number:",rec["Rollno."])
print("Name:",rec["Name"])
print("Marks:",rec["Marks"])
print()
except EOFError:
break
if z==3:
f=open("student.dat",'rb')
while True:
try:
rec=p.load(f)
print()
print("Roll number:",rec["Rollno."])
print("Name:",rec["Name"])
print("Marks:",rec["Marks"])
print()
except EOFError:
break
# Output :-

# Text file (output) :-


PRACTICAL - 11
Menu driven program to add, update delete records from
binary file
# SOURCE CODE :-
def addrecord():
f=open("electronics.dat",'rb')
D=p.load(f)
It=input("Enter the item name:")
Pr=input("Enter the Price:")
Q=float(input("Enter the Quantity:"))
D[It]=[Pr,Q]
f.close()
f=open("electronics.dat",'wb')
p.dump(D,f)
f.close()
def updaterecord():
f=open("electronics.dat",'rb')
I=input("Enter the record name you want to update:")
NPr=input("Enter the new Price:")
NQ=float(input("Enter the new Quantity:"))
D=p.load(f)
if I in D:
del D[I]
D[I]=[NPr,NQ]
else:
print("Invalid Item Name.")
f.close()
f=open("electronics.dat",'wb')
p.dump(D,f)
f.close()
def deleterecord():
f=open("electronics.dat",'rb')
I=input("Enter the record name you want to delete:")
D=p.load(f)
if I in D:
del D[I]
f.close()
f=open("electronics.dat",'wb')
p.dump(D,f)
f.close()
def display():
f=open("electronics.dat",'rb')
D=p.load(f)
for key, value in D.items() :
print (key, value)
import pickle as p
while True:
print("-------- Electronic store Record--------")
print("1. Add record. ")
print("2. Update a Record.")
print("3. Delete a Record.")
print("4. print all Records.")
print()
ch=int(input("Enter your choice:"))
print()
if ch==1:
addrecord()
elif ch==2:
updaterecord()
elif ch==3:
deleterecord()
elif ch==4:
display()
else:
break
print()

# Text file after running:-


# OUTPUT:-
PRACTICAL - 12
Create CSV file by entering user-id and password, read &
search the password for given user-id

# SOURCE CODE :-
import csv
with open("ids.csv", "a") as cv:
cv = csv.writer(cv)
while True:
user_id= input("Enter id:")
password= input("Enter password:")
data=[user_id, password]
cv.writerow(data)
z=input("do you want to continue:")
if z =="n":
break
elif z=="y":
continue
with open("ids.csv", "r") as cv:
cv= csv.reader(cv)
x=input("Enter the user_id to find:")
for i in cv:
next(cv)
if i[0] == x:
print(i[1])
break

# Output :-

# CSV file (output) :-


PRACTICAL - 13
Write a random number generator that generates random
numbers between 1 and 6 (dice)

# SOURCE CODE :-
import random
while True:
i=input("Do you want to roll a Dice:")
if i=='y':
z= random.randint(1,6)
print("number is ---->",z)
else:
print(" 'Thank you for playing'")
break

# Output :-
PRACTICAL - 14
Binary search using recursion

# SOURCE CODE :-
def bi(x,low,high,a):
if high >= low:
mid = (high + low) // 2
if x[mid] == a:
return mid
elif x[mid] > a:
return bi(x, low, mid - 1, a)
else:
return bi(x, mid + 1, high, a)
else:
return ("No element found")
x=[]
z=int(input("Enter no of elements to add in list:"))
for i in range(z):
n=int(input("Enter number:"))
x.append(n)
a=int(input("Enter element to search:"))
f = bi(x,0,len(x)-1,a)
print("Index:",f)

# Output :-
PRACTICAL - 15
Selection sort

# SOURCE CODE :-
a=eval(input("Enter a list:-"))
for i in range(len(a)):
b=i
for j in range(i+1, len(a)):
if a[b] > a[j]:
b=j
a[i], a[b] = a[b], a[i]
print(a)

# Output :-
PRACTICAL - 16
Bubble sort

# SOURCE CODE :-
a=eval(input("Enter a list:-"))
b=len(a)
print()
print("Before sorting",a)
for i in range(b-1):
for j in range(b-1-i):
if a[j]>a[j+1]:
a[j],a[j+1]=a[j+1],a[j]
print("After sorting",a)

# Output :-
PRACTICAL - 17
Insertion sort

# SOURCE CODE :-
a=eval(input("Enter a list:-"))
for i in range(1,len(a)):
key=a[i]
j=i-1
while j>=0 and key<a[j]:
a[j+1]=a[j]
j=j-1
else:
a[j+1]=key
print(a)

# Output :-
PRACTICAL - 18
Implementation of Stacks using lists

# SOURCE CODE :-
def push(lst,a):
lst.append(a)
top=len(lst)-1

def Pop(lst):
if len(lst)!=0:
i=lst.pop()
top=len(lst)-1
print(i,"is poped out of stack")
else:
print("stack is empty")

def peek(lst):
if len(lst)!=0:
i=lst[len(lst)-1]
print("given stack has last element",i)
else:
print("stack is empty")

def display(lst):
if len(lst)!=0:
top=len(lst)-1
for i in range(len(lst)):
print(lst[top])
top=top-1
else:
print("stack is empty")

top=-1
lst=[]
while True:
print("")
print(" ---STACK---")
print('1.push new element in stack')
print('2.pop an element from stack')
print('3.peek the toppest element from the stack')
print('4.display whole stack')
print('5.EXIT')
print()
ch=int(input("Enter your choice:-"))
if ch==1:
a=input("Enter the element to add in the stack:-")
push(lst,a)

if ch==2:
Pop(lst)

if ch==3:
peek(lst)

if ch==4:
display(lst)

if ch==5:
break

# Output :-
PRACTICAL - 19
Display unique vowels in a string using stacks

# SOURCE CODE :-
Stack = []
vowels =['a','e','i','o','u']
word = input("Enter the word to search for vowels :")
for letter in word:
if letter in vowels:
if letter not in Stack:
Stack.append(letter)
print(Stack)

# Output :-
PRACTICAL - 20
Implementation of queue using a list

# SOURCE CODE :-
def enqueue(lst,ele):
lst.append(ele)
f=0
r=len(lst)-1

def dequeue(lst):
if len(lst)!=0:
i=lst.pop(0)
if len(lst)==0:
f=r=None
else:
print("Queue is empty")

def peek(lst):
if len(lst)!=0:
f=0
print(lst[0])
else:
print("Queue is empty")

def display(lst):
f=0
r=len(lst)-1
if len(lst)!=0:
print(lst[0],"----front")
for i in range(1,r):
print(lst[i])
print(lst[r],"----rear")
else:
print("Queue is empty")
lst=[]
f=r=None
while True:
print("")
print(" ---Queue---")
print('1.push new element in Queue')
print('2.pop an element from Queue')
print('3.peek in the Queue')
print('4.display whole Queue')
print('5.EXIT')
print()
ch=int(input("Enter your choice:-"))
if ch==1:
ele=input("Enter the element to add in the Queue:-")
enqueue(lst,ele)

if ch==2:
dequeue(lst)

if ch==3:
peek(lst)

if ch==4:
display(lst)

if ch==5:
break

# Output :-
PRACTICAL - 21
MYSQL- LOAN TABLE






PRACTICAL - 22
MYSQL- HOSPITAL TABLE





❑ Pow(4,3) mod(37,7)
64 2

Round(1473.1457,2) Round(1473.1457,-1) Round(1473.1457)



1473.15 1470 1473

❑ Upper(Mid(name,3,2)) Length(name)
UN 4

❑ Instr(‘Maharaja Aggarsein’,’a’)
2

❑ name age
Zarina 22

❑ department Avg(charges) Min(charges)


Cardiology 283.3333 250
ENT 450.0000 200
Eyes 300 300

❑ department Min(age) Max(age)


Cardiology 30 62
ENT 16 32

❑ DateofAdm Month(DateofAdm)
1998-01-12 01
1998-01-13 01

❑ Max(DateofAdm) Min(DateofAdm)
1998-02-24 1997-12-12

Date(now()) Day(curdate())

2022-08-14 14
PRACTICAL - 23
SQL join table: flight-fares

Fl_no No_flight airlines



AM501 1 Jet airways
ic302 8 Indian airlines

❑ airlines Sum(fare) Avg(tax)


Jet airways 13400 8.0000
Indian airline 18800 7.5000
Indian airlines 6500 10.0000
Deccan airlines 7200 10.0000
PRACTICAL - 24
SQL join table: Store-supplier
item rate*qty
H. Ball pen 480

eraser 480
I. Empty set
big
Pencil 520
star
Gel pen 520
sharp

sname Avg(qty)
J. Tetra supply 94.0000
K. Max(lastbuy)

2015-03-15
Plastics 150.0000

Classic india 41.0000

Doms ltd 110.0000

Count(distinct scode) distinct scode


L. 5
M.
20

1 22

1 23

2 24

1 28

lastbuy Year(lastbuy) Month(lastbuy)


N.
2013-05-14 2013 05
PRACTICAL - 25
To create new databse from python and show all databases

# SOURCE CODE :-
import MySQLdb
db=MySQLdb.connect("localhost","root","Rishabh___21")
cursor=db.cursor()
sql1="create database new"
sql2="show databases"
try:
cursor.execute(sql1)
cursor.execute(sql2)
for x in cursor:
print(x)
db.commit()
except:
db.rollback()
print("ERROR")
db.close()

# Output :-
PRACTICAL - 26
To create a table in MySQL from python

# SOURCE CODE :-
import MySQLdb
db=MySQLdb.connect("localhost","root","Rishabh___21","try")
cursor=db.cursor()
sql="create table class(sno int primary key,name varchar(20),subject varchar(15),gender char(1)
check(gender in('M','F','O')))"
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
print("ERROR")
db.close()

# Output :-
PRACTICAL - 27
To add new col in MySQL table from python

# SOURCE CODE :-
import MySQLdb
db=MySQLdb.connect("localhost","root","Rishabh___21","try")
cursor=db.cursor()
sql="alter table class add marks float(5,2)"
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
print("Error")
db.close()

# Output :-
PRACTICAL - 28
To insert records in MySQL from python

# SOURCE CODE :-
import MySQLdb
db=MySQLdb.connect("localhost","root","Rishabh___21","try")
cursor=db.cursor()
sql="insert into class values(1,'abc','maths','M',87.2)"
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
print("Error")
db.close()

# Output :-
PRACTICAL - 29
To update a record in MySQL from python

# SOURCE CODE :-
import MySQLdb
db=MySQLdb.connect("localhost","root","Rishabh___21","try")
cursor=db.cursor()
sql="update class set name='xyz' where sno=1"
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
print("ERROR")
db.close()

# Output :-
PRACTICAL - 30
To delete record in MySQL from python

# SOURCE CODE :-
import MySQLdb
db=MySQLdb.connect("localhost","root","Rishabh___21","try")
cursor=db.cursor()
sql="delete from class where name='xyz'"
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
print("ERROR")
db.close()

# Output :-

You might also like