You are on page 1of 24

INDEX

1. Write a program to input value of x and n and print the series along with its
sum.
2. Write a function DigitSum() that takes a number and returns its digit sum.
3. Write a program to input a list of number and search for a given number using
linear search.
4. Write a menu based program to demonstrate operation on a stack
5. Write a program to print a string and the number of vowels present in it.
6. Write a program to input a year and check whether the year is leap year or
not.
7. Write a function Div 3and5 () that takes a 10 elements numeric tuple and
return the sum of element which are divisible by 3 and 5.
8. Write a Recursive recurfactorial(n) in python to calculate and return the
factorial of number n passed to the parameter.
9. Write a program to make a pyramid.
10 Write a menu based program to demonstrate operation on queue.
.
11 Connect mysql from python and alter the table.
.
12 Fetch the data from database to python.
.
13 Program to write rollno, name and marks of a student in a data file Marks.dat.
.
14 Program to capitalize first letter of each word.
.
15 Write a program to input 3 numbers and print the greatest number using
. nested if.
16 Write a program to print Fibonacci series up to n terms and find the sum of
. series.
17 Write a program to read a file book.txt print the content of file along with
. numbers of words and frequency of word computer in it.
18 Write a program to input a number and check whether it is prime number or
. not.
19 Write a program to enter a number and find the sum of its digit.
.
20 Write a program to check phone number (valid/not valid).
.
21 Write a program to input a list and arrange it in ascending order using Bubble
. sorting.
22 Write a program to plot a bar graph.
.
23 Interface MySQL with Python (Alter and Update commands).
.
24 Write a program to find the area of circle,square and rectangle.
.
25 Write a program to check phone number (valid/not valid).
.

1. Write a program to input value of x and n and print the series along with its sum.
x=float(input(“Enter the value of x= “))
n=float(input(“Enter the value of n= “))
i=1
s=0
while i<n:
y=x**i
print(y,”+”,end=” “)
s=s+y
i+=1
print(x**n)
s=s+(x**n)
print(“Sum of series= “,s)

OUTPUT:

2. Write a function DigitSum() that takes a number and returns its digit sum.
def DigitSum(n):
s=0
n=str(n)
for i in n:
s=s+int(i)
return s
n=int(input(“Enter the number= “))
print(“Sum of digits= “,DigitSum(n))

OUTPUT:
3. Write a program to input a list of number and search for a given number using linear
search.
l=eval(input(“Enter the list of numbers= “))
x=int(input(“Enter the number :”))
for i in l:
if i==x:
print(“Element present”)
break
else:
print(“Element not found”)

OUTPUT:

4. Write a menu based program to demonstrate operation on a stack.


#Functions
def isEmpty(stk):
if len(stk)==0:
return True
else:
return False
def push(stk,n):
stk.append(n)
def pop(stk):
if isEmpty(stk):
print(“UNDERFLOW CONDITIOM”)
else:
print(“Deleted element : “,stk.pop())
def peek(stk):
return stk[-1]
def display(stk):
if isEmpty(stk):
print(“No element present”)
else:
for i in range(-1,-len(stk)-1,-1):
if i==-1:
print(“TOP”,stk[i])
else:
print(“ “,stk[i])

#main
stk=[]
while True:
print(“ Stack operations”)
print(“ 1.PUSH 2.POP”)
print(“ 3.PEEK 4.DISPLAYSTACK”)
print(“ 5.EXIT”)
ch=int(input(“ Enter the choice= “))
if ch==1:
n=input(“Enter the element to PUSH= ”)
push(stk,n)
print(“Element pushed”)
elifch==2:
pop(stk)
elifch==3:
if isEmpty(stk):
print(“UNDERFLOW CONDITION”)
else:
print(peek(stk))
elifch==4:
display(stk)
elifch==5:
break
else:
print(“INVALID CHOICE ENTERED”)
print(“THANKS FOR USING MY SERVICES”)

OUTPUT:
5. Write a program to print a string and the number of vowels present in it.
str=input(“Enter the string= “)
print(“Entered string= “,str)
str=str.lower()
c=0
v=[‘a’,’e’,’i’,’o’,’u’]
for i in str:
if i in v:
c+=1
print(“Number of vowels= “,c)
OUTPUT:

6. Write a program to input a year and check whether the year is leap year or not.
x=int(input(“Enter the year= “))
if x%400==0:
print(x, ”is a Century leap year”)
elif x%100!=0 and x%4==0:
print(x, ”is a leap year”)
else:
print(x, ”is not a leap year”)

OUTPUT:

7. Write a function Div3and5() that takes a 10 elements numeric tuple and return the
sum of element which are divisible by 3 and 5.
def Div3and5(t):
s=0
for i in t:
if i%3==0 and i%5==0:
s=s+i
return s
l=[]
for i in range(10):
print(“Enter”, i+1 ,”number of the tuple”, end=”“,sep=” “)
e=int(input())
l.append(e)
t=tuple(l)
print(“Entered tuple: “,t)
print(“Sum of numberdivisible by 3 and 5=”,Div3and5(t))

OUTPUT:

8. Write a Recursive recurfactorial(n) in python to calculate and return the factorial of


number n passed to the parameter.
def recurfactorial(n):
if n==1:
return n
else:
return n*recurfactorial(n-1)

num=int(input(“Enter the number= “))


if num<0:
print(“Sorry, No factorial for negative number”)
elifnum==0:
print(“The factorial of 0 is 1”)
else:
print(“The factorial of”,num,”is”,recurfactorial(num))

OUTPUT:

9. Write a program to make a pyramid.


def triangle(n):
k=2*n-2
for i in range(0,n):
for j in range(0,k):
print(end=” “)
k=k-1
for j in range(0,i+1):
print(“*”,end=” “)
print(“\r”)
n=5
triangle(n)

OUTPUT:

10. Write a menu based program to demonstrate operation on queue.


def isEmpty(qu):
if len(qu)==0:
return True
else:
return False
def ENQUEUE(qu,item):
qu.append(item)
if len(qu)==1:
rear=front=0
else:
rear=len(qu)-1
front=0
def DEQUEUE(qu):
if isEmpty(qu):
print(“UNDERFLOW CONDITION”)

else:
a=qu.pop(0)
print(“ELEMENT DELETED”,a)
def PEEK(qu):
return qu[-1]
def display(qu):
if isEmpty(qu):
print(“NO ELEMENT FOUND”)
else:
for i in range(len(qu)):
if i==0:
print(“FRONT”,qu[i])
elifi==len(qu)-1:
print(“REAR”,qu[i])
else:
print(“ “,qu[i])
#main
qu=[]
while True:
print(“\t\t QUEUE OPERATION”)
print(“\t\t 1.ENQUEUE 2.DEQUEUE”)
print(“\t\t 3.DISPLAY QUEUE 4.PEEK”)
print(“\t\t 5.EXIT”)
ch=int(input(“\t\t Enter the choice= “))
if ch==1:
x=input(“Enter the element to be inserted= “)
ENQUEUE(qu,x)
print(“ELEMENT HAS BEEN INSERTED”)
elifch==2:
DEQUEUE(qu)

elifch==3:
display(qu)
elifch==4:
if isEmpty(qu):
print(“UNDERFLOW CONDITION”)
else:
print(peek(qu))
elifch==5:
break
else:
print(“INVALID CHOICE ENTERED”)
print(“THANKS FOR USING MY SERVICE”)

OUTPUT:
11. Connect mysql from python and alter the table.
import mysql.connector as mq
cn=mq.connect(host="localhost",user="root",database="krish")
cur = cn.cursor()
cur.execute("alter table tablename add column coluumnname varchar(30)")
cn.commit()

OUTPUT:
12. Fetch the data from database to python.
import mysql.connector as mq
cn=mq.connect(host="localhost",user="root",database="krish")
cur = cn.cursor()
cur.execute("select * from students")
data= cur.fetchall()
for i in data:
print(i)

OUTPUT:

13. Program to write rollno, name and marks of a student in a data file Marks.dat.
count=int(input('How many students are there in the class= '))
fileout=open("Marks.dat","a")
for i in range(count):
print("Enter details of student",(i+1),"below")
rollno=int(input("Enter rollno= "))
name=input("Name= ")
marks=float(input('Marks= '))
rec=str(rollno)+","+name+","+str(marks)+"\n"
fileout.write(rec)
fileout.close()

OUTPUT: Marks.dat File:


14. Program to capitalize first letter of each word.
string=input(“Enter the string= “)
length=len(string)
print(“Original string”,string)
string2=” “
for a in range(0,length,2):

string2+=string[a]
if a<(length-1):
string2+=string[a+1].upper()
print(“Alternately capitalised string :”,string2)

OUTPUT:

15. Write a program to input 3 numbers and print the greatest number using nested if.
a=float(input(“Enter the 1st number= “))
b=float(input(“Enter the 2nd number= “))
c=float(input(“Enter the 3rd number= “))
if a>=b:
if a>=b:
print(“First number ”,a, “is greatest”)
elifb>c:
if b>c:
print(“Second number”, b, ”is greatest”)
elif c>a:
if c>b:
print(“Third number”, c ,”is greatest”)
OUTPUT:

16. Write a program to print Fibonacci series up to n terms and find the sum of series.
n=int(input(“Enter the number of terms in Fibonacci series= “))
a,b=0,1
s=a+b
print(a,b,end=” “)
for i in range(n-2):
print(a+b,end=” “)
a,b=b,a+b
s=s+b
print()
print(“Sum of”, n ,”terms of series= “,s)

OUTPUT:

17. Write a program to read a file book.txt print the content of file along with numbers
of words and frequency of word computer in it.

Content of file:
Python is an interpreted, high-level, general-purpose computer programming
language.
Created by Guido van Rossum and first released in 1991.
Python’s design philosophy emphasizes code readability..Its language constructs and
object -oriented approach aim to help programmers write clear,logical code.
**************FIELD END***********

f=open(“book.txt”,”r”)

L=f.readlines()

c=c1=0

v=[‘a’,’e’,’i’,’o’,’u’]

print(“Contents of file :”)

for i in L:

print(i)

j=i.split()

for k in j:

if k.lower()==”computer”:

c1=c1+1

for x in k:

if x.lower() in v:

c+=1

print()

print(“Number of vowels in the file= ”,c)

print(“Number of times ‘computer’ in the file= “,c1)

f.close()

OUTPUT:
18. Write a program to input a number and check whether it is prime number or not.
n=int(input(“Enter the number= “))
c=1
for i in range(2,n):
if n%i==0:
c=0
if c==1:
print(“Number is prime”)
else:
print(“Number is not prime”)

OUTPUT:

19. Write a program to enter a number and find the sum of its digit.

string=input("Enter the number : ")


dig=0
sum=0
for i in range(len(string)):
if(string[i].isdigit()):
sum+=int(string[i])
dig+=1
if(dig==0):
print("No digit in string",string)
else:
print("Sum of digit is",sum,"String is",string)

OUTPUT:
20. Game made from random().
import random
while(1):
x=random.randint(1,4)
y=random.randint(1,4)
if(x!=y):
print(x,"FIRST")
print(y,"SECOND")
break

OUTPUT:

21. Write a program to input a list and arrange it in ascending order using Bubble
sorting.
l=eval(input(“Enter the list to arrange= “))
for i in range(len(l)-1):
for j in range(len(l)-1):
if l[j]>l[j+1]:
l[j],l[j+1]=l[j+1],l[j]
print(“Arranged list= “,l)

OUTPUT:

string2+=string[a]
if a<(length-1):
string2+=string[a+1].upper()
print(“Alternately capitalised string :”,string2)

OUTPUT:

22. Write a program to plot a bar graph.


import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as mpatches
plt.title(“Death ratio of three months”)
in_death=[25,50,59]

in_covid=[10,56,89]
x=np.arange(len(in_death))
plt.bar(x,in_death,color=’red’,width=0.35)
plt.bar(x+0.35,in_covid,color=’blue’,width=0.35)
plt.xlabel(“Months”)
plt.ylabel(“No. of deaths”)
patch1=mpatches.Patch(color=’red’,label=’Normal’)
patch2=mpatches.Patch(color=’blue’,label=’Covid’)
plt.legend(handles=[patch1,patch2])
plt.show()

OUTPUT:

23. Interface MySQL with Python (Alter and Update commands).


import mysql.connector as mq
mycon=mq.connect(host=’localhost’,user=’root’,database=’krish’)
if mycon.is_connected():
print(“Connected”)
co=mycon.cursor()
sql=”update students set section=’Sc’ where rollno=1”
co.execute(sql)
mycon.commit()

OUTPUT:

24. Write a program to find the area of circle,square and rectangle.


print("Press 1 for area of circle","Press 2 for area of square","Press 3 for area of
rectangle",sep="\n")
choose=int(input("Enter your choice= "))
if choose==1:
r=int(input("Enter the radius= "))
print("Area of circle= ",3.14*(r**2))
elif choose==2:
s=int(input("Enter the side of square= "))
print("Area of square= ",s*s)
elif choose==3:
l=int(input("Enter the 1st side of rectangle= "))
b=int(input("Enter the 2nd side of rectangle= "))
print("Area of rectangle= ",l*b)
else:
print("Invalid choice please try again")

OUTPUT:

25. Write a program to check phone number (valid/not valid).


x=input("Enter your phone number= ")
if len(str(x))==10:
print("valid phone number")
else:
print("invalid phone number")

OUTPUT:

MYSQL QUERIES
1. Creating Database

2. CREATING TABLE

3. INSERTING INTO A TABLE

4. SHOWING FIELD, TYPE ETC.

5. SHOWING CONTENT OF TABLE AFTER INSERTING


6. SELECT A SPECIFIC RECORD BY ECODE

7. SELECT A SPECIFIC RECORD BY NAME

8. ADDING A FIELD TO A TABLE

9. ADDING SAME DATA TO THE FIELD


10. ADDING DATA TO A SPECIFIC NAME IN A FIELD

11. DELETING A PARTICULAR RECORD

12. COUNT NUMBER(SEX, SALARY)


13. DROP A PARTICULAR FIELD FROM TABLE

14. ADDING AN ENTRY AND ARRANGING IN ASCENDING ORDER

15. ARRANGING IN DESCENDING ORDER

You might also like