You are on page 1of 22

Task 1. Program to check whether a number is prime or not.

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:

print(n,'is not prime')

Output

Task 2. Program to check whether a number is palindrome or not.

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:

print(k,'is not palindrome')

Output

Task 3. Program to dislplay ascii code of a character and vice versa.


Program code

a= input('enter character:')

ascii_code= ord(a)

character= chr(ascii_code)

print('ascii code of ',character,' is ',ascii_code)

print('value corresponding to ascii code ',ascii_code, ' is ',


character)

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

chr = input('enter a character')

if(chr.isalpha()):

print(chr,'is an alphabet')

elif(chr.isdigit()):

print(chr,'is digit')

else:

print(chr,'is a special symbol')

Output

Task 5. Program to calculate factorial of an integer using recursion.


Program code

def fact(n):

if(n==1):

return 1

else:

return(n*fact(n-1))
n= int(input('enter number'))

factorial=fact(n)

print('factorial of',n, 'is', factorial)

Output

Task 6.Program to perform binary search.


Program code

n= int(input('enter number of elements to be entered in the


list'))

L=[]

for i in range(n):

m= int(input('enter element '+str(i+1)+"- "))

L.append(m)

num=int(input('enter number to be searched'))

beg=0

last= len(L)-1

while (beg<=last):

mid=int((last+beg)/2)

if(num==L[mid]):

print(number,’ found at position ', mid+1)

break
elif(num>L[mid]):

beg= mid+1

else:

last=mid-1

else:

print('number not found')

Output

Task 7. Program to count the number of vowels in a text file.


Program code

f1= open('C:\\Users\\HP\\Desktop\\shavik\\myfile.txt', 'r')

m= f1.read()

count=0

for i in m:

if(i in'aeiou'):

count=count+1

print('number of vowels in myfile:',count)

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

from math import factorial as fact

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

n= int(input('enter number of terms'))

x= int(input('enter value of x'))

result=sin(x,n)

print('the result is',result)


Output

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)

user_number= int(input('enter your number'))

print('lucky number is:', n)

if(user_number==n):

print('congrats you won a lottery')

else:

print('oops ,next time')

Output

Task 11. Program to create a library in python and import it in a program.


Program code

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]

import matplotlib.pyplot as plt

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

n= int(input('enter number of values for x'))

x = []

for i in range(n):

x.append(int(input('enter value'+str(i+1)+':')))

x = np.array(x)

y=x**2

import matplotlib.pyplot as plt

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]

areas=['drinking','washing utensil', 'washing clothes', 'making


food','bathing']

import matplotlib.pyplot as plt

plt.pie(consumption, labels=areas,autopct='%2i%%')

plt.show()

Output
Task 15. Program for linear search.
Program code

n= int(input('enter number of elements'))

L=[]

for i in range(n):

m= int(input('enter element '+str(i+1)+"- "))

L.append(m)

num=int(input('enter number to be searched'))

length= len(L)

for i in range(length):

if (L[i]==num):
print(num,'found at position', i)

break

else:

print(num, 'not found')

Output

Task 16. Program for bubble sort.


Program code

n=int(input("enter no of elements in the list"))

L=[]

for i in range(n):

x = int(input("enter number to be entered"))

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( '1. push')


print('2.pop')

print('3.peek')

print('4.display stack')

print('5.exit')

ch= int(input('enter your choice (1-5):'))

if(ch==1):

item= int(input('enter item'))

push(stack,item)

elif(ch==2):

item= pop(stack)

if(item=='underflow'):

print('underflow! stack is empty')

else:

print('popped item is', item)

elif(ch==3):

item= peek(stack)

if(item=='underflow'):

print('underflow! stack is empty')

else:

print('topmost item is', item)

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')

ch= int(input('enter your choice(1-5):'))

if(ch==1):

item= int(input('enter item'))

enqueue(queue,item)
elif(ch==2):

item= dequeue(queue)

if(item=='underflow'):

print('queue is empty')

else:

print('dequeue-ed item is', item)

elif(ch==3):

item= peek(queue)

if(item=='underflow'):

print('queue is empty')

else:

print('frontmost item is', item)

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

Task 20. Program to calculate EMI for a loan using numpy.


Program code

Output

Task 21. Program to write a django based web application and write the data to a
csv file.
Program code
Output

You might also like