Professional Documents
Culture Documents
Class 12 Practical File
Class 12 Practical File
Program code
n= int(input('enter number'))
count=0
for i in range(1,n+1):
if(n%i==0):
count=count+1
if(count==2):
print(n,'is prime')
else:
Output
Program code
n= int(input('enter number'))
n1=n
k=0
while(n>0):
r=n%10
k=k*10+r
n=n//10
if(k==n1):
print(k,'is palindrome')
else:
Output
a= input('enter character:')
ascii_code= ord(a)
character= chr(ascii_code)
Output
Task 4. Program to input a character and check whether the given character is an
alphabet, or digit or any other character.
Program code
if(chr.isalpha()):
print(chr,'is an alphabet')
elif(chr.isdigit()):
print(chr,'is digit')
else:
Output
def fact(n):
if(n==1):
return 1
else:
return(n*fact(n-1))
n= int(input('enter number'))
factorial=fact(n)
Output
L=[]
for i in range(n):
L.append(m)
beg=0
last= len(L)-1
while (beg<=last):
mid=int((last+beg)/2)
if(num==L[mid]):
break
elif(num>L[mid]):
beg= mid+1
else:
last=mid-1
else:
Output
m= f1.read()
count=0
for i in m:
if(i in'aeiou'):
count=count+1
Output
Task 8. Program to write those lines which have the character ‘p’ from one text file
to another text file.
Program code
Output
Task 9. Program to write a function sin(x,n) to calculate the value of sin x using its
taylor series expansion upto n terms.
Program code
def sin(x,n):
i=1
k=1
sum=0
for j in range(n):
sum=sum+((-1)**(k+1))*((x**i)/(fact(i)))
i=i+2
k=k+1
return sum
result=sin(x,n)
Task 10. Program to generate random numbers between 1 to 6 and check whether a
user won a lottery or not.
Program code
import random as rn
n= rn.randint(1,6)
if(user_number==n):
else:
Output
def func():
for i in range(1,5):
print(i*i,'',end='')
import mylib as mb
mb.func()
Output
Task 12. Program to plot a bar chart to display the result of a school for five
consecutive years.
Program code
years=[1995,1996,1997,1998,1999]
result=[80,95,90,95,100]
plt.bar(years,result)
plt.xlabel('years')
plt.ylabel('result')
plt.show()
Output
Task 13. Program to plot a graph of the function y= x^2.
Program code
import numpy as np
x = []
for i in range(n):
x.append(int(input('enter value'+str(i+1)+':')))
x = np.array(x)
y=x**2
plt.plot(x,y)
plt.show()
Output
Task 14. Program to plot a pie chart on consumption of water in daily life.
Program code
consumption=[30,12,20,25,23]
plt.pie(consumption, labels=areas,autopct='%2i%%')
plt.show()
Output
Task 15. Program for linear search.
Program code
L=[]
for i in range(n):
L.append(m)
length= len(L)
for i in range(length):
if (L[i]==num):
print(num,'found at position', i)
break
else:
Output
L=[]
for i in range(n):
L.append(x)
n= len(L)
for i in range(n):
for j in range(0,n-i-1):
if(L[j]>L[j+1]):
L[j],L[j+1]=L[j+1],L[j]
print(L)
Output
Task 17. Menu based program to perform the operation on stack in python.
Program code
def isEmpty(stk):
if (stk==[]):
return True
else:
return False
def push(stk,item):
stk.append(item)
top= len(stk)-1
def pop(stk):
if (isEmpty(stk)):
return 'underflow'
else:
item=stk.pop()
if(len(stk)==0):
top=none
else:
top=len(stk)-1
return item
def peek(stk):
if (isEmpty(stk)):
return 'underflow'
else:
top= len(stk)-1
return stk[top]
def display(stk):
if(isEmpty(stk)):
print('stack empty')
else:
top= len(stk)-1
print(stk[top],'<-top')
for i in range(top-1,-1,-1):
print(stk[i])
stack=[]
top= None
while True:
print('stack operations:')
print('3.peek')
print('4.display stack')
print('5.exit')
if(ch==1):
push(stack,item)
elif(ch==2):
item= pop(stack)
if(item=='underflow'):
else:
elif(ch==3):
item= peek(stack)
if(item=='underflow'):
else:
elif(ch==4):
display(stack)
elif(ch==5):
break
else:
print('invalid choice')
Output
Task 18. Menu driven program to perform the operations on queue in python.
Program code
def isempty(Qu):
if(Qu==[]):
return True
else:
return False
def enqueue(Qu,item):
Qu.append(item)
if(len(Qu)==1):
front=rear=0
else:
rear= len(Qu)-1
def dequeue(Qu):
if (isempty(Qu)):
return 'underflow'
else:
item= Qu.pop(0)
if(len(Qu)==0):
front=rear=None
return item
def peek(Qu):
if(isempty(Qu)):
return 'underflow'
else:
front=0
return Qu[front]
def display(Qu):
if(isempty(Qu)):
print('queue empty')
elif(len(Qu)==1):
print(Qu[0],'<-front,rear')
else:
front=0
rear=len(Qu)-1
print(Qu[front],'<-front')
for i in range(1,rear):
print(Qu[i])
print(Qu[len(Qu)-1],'<-rear')
queue=[]
front= None
while True:
print('queue operations')
print('1.enqueue')
print('2. dequeue')
print('3.peek')
print('4,display queue')
print('5.exit')
if(ch==1):
enqueue(queue,item)
elif(ch==2):
item= dequeue(queue)
if(item=='underflow'):
print('queue is empty')
else:
elif(ch==3):
item= peek(queue)
if(item=='underflow'):
print('queue is empty')
else:
elif(ch==4):
display(queue)
elif(ch==5):
break
else:
print('invalid choice')
Output
Task 19. Program to open a webpage using urllib library.
Program code
Output
Output
Task 21. Program to write a django based web application and write the data to a
csv file.
Program code
Output