INDEX
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:
#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:
OUTPUT:
OUTPUT:
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()
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’]
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()
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.
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:
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:
OUTPUT:
OUTPUT:
OUTPUT:
MYSQL QUERIES
1. Creating Database
2. CREATING TABLE