You are on page 1of 18

COMPUTER SCIENCE

Practical File
On
Python and MySQL

Made By: -
Aum Singhal
Class:- XII – A
Roll No.:- 29
INDEX
Q1). Write a function to check a number whether it is
palindrome or not?
Q2). Write a function to input a character and to print
whether a given character is an alphabet, digit or any
other character?
Q3). Write a function to calculate the factorial of an
integer?
Q4). Write a function to print multiples of a given no.
up to 10?
Q5). Write a function to print Fibonacci series up to
given no.?
Q6). Write a function to check whether a no. is
Armstrong or not?
Q7). Write a program to count the number of vowels
present in a text file?
Q8). Write a program to write those lines which have
the character 'p' from one text file to another text
file?
Q9). Write a function to read data from a text file and
display it?
Q10). Write a function to write data in a text file?
Q11). Write a program to create a table in mysql
database?
Q12). Write a program to add a data in the
MySQL database?
Q13). Write a program to delete a data in MySQL
database?
Q14). Write a program to update a record in
MySQL Database?
Q15). Write a program for Binary Search?
Solutions:
Q1).
def palindrome(n):
r=0
x=n
while(n>0):
t = n%10
n = n//10
r = r*10 + t
if(x==r):
print('the number is palindrome')
else:
print('the number is not palindrome')
OUTPUT:

Q2).
def character():
n=input('Enter your digit')
if (n.isdigit()==True):
print('the entered character is digit')
elif(n.isalpha()==True):
print('the entered character is alphabet')
else:
print('the entered character is not alphabet
and not a digit')
character()
OUTPUT:

Q3).
def fact(no):
f=1
while no>0:
f=f*no
no=no-1
return f
OUTPUT:

Q4).
def multi(n):
i=1
while(i<=10):
print(i*n)
i+=1
OUTPUT:
Q5).
def fibo(n):
no1=0
no2=1
print(no1)
print(no2)
x=1
while(x<=n):
no3=no1+no2
no1=no2
no2=no3
print (no3)
x=x+1
OUTPUT:

Q6).
def arm():
no=int(input("Enter any number to check : "))
no1 = no
sum = 0
while(no>0):
ans = no % 10;
sum = sum + (ans * ans * ans)
no = int (no / 10)
if (sum == no1):
print("Armstrong Number")
else:
print("Not an Armstrong Number")
OUTPUT:

Q7).
f = open("file(1).txt",'r')
l = f.readlines()
v = 'aeiouAEIOU'
count = 0
for i in l:
a = i.split()
for m in a:
n = len(m)
c=0
while(c <= (n-1)):
if (m[c] in v):
count += 1
c += 1
print('No. of vowels are:',c)
OUTPUT:

Q8).
f = open("file(1).txt", 'r')
a = f.readlines()
c = []
for i in a:
if((i[0][0] == 'p') or (i[0][0] == 'P')):
c.append(i)
f.close()

f = open("file(2).txt", 'w')
f.writelines(c)
print('Write Successful')
f.close()
OUTPUT:
After running the code

Q9).
f = open("file(1).txt",'r')
a = f.read()
print(a)
f.close()
OUTPUT:
Q10).
f = open("file(2).txt", 'w')
l = ['Hello \n',
'i am using python \n',
'data file handling \n']
f.writelines(l)
f.close()
OUTPUT:

After running the code

Q11).
import mysql.connector
demodb =mysql.connector.connect(host="localhost",
user="root", passwd="computer",
database="EDUCATION")
democursor=demodb.cursor( )
democursor.execute("CREATE TABLE STUDENT
(admn_no int primary key, sname varchar(30), gender
char(1), DOB date, stream varchar(15), marks
float(4,2))")

Q12).
import mysql.connector
demodb = mysql.connector.connect(host="localhost",
user="root", passwd="computer",
database="EDUCATION")
democursor=demodb.cursor( )
democursor.execute("insert into student values (%s,
%s, %s, %s, %s, %s)", (1245, 'Arush', 'M', '2003-10-04',
'science', 67.34))
demodb.commit( )

Q13).
import mysql.connector
demodb = mysql.connector.connect(host="localhost",
user="root", passwd="computer",
database="EDUCATION")
democursor=demodb.cursor( )
democursor.execute("delete from student where
admn_no=1356")
demodb.commit( )

Q14).
import mysql.connector
demodb = mysql.connector.connect(host="localhost",
user="root", passwd="computer",
database="EDUCATION")
democursor=demodb.cursor( )
democursor.execute("update student set
marks=55.68 where admn_no=1356")
demodb.commit( )

Q15).
def Binary_Search(sequence, item, LB, UB):
if LB>UB:
return -5 # return any negative value
mid=int((LB+UB)/2)
if item==sequence[mid]:
return mid
elif item<sequence[mid]:
UB=mid-1
return Binary_Search(sequence, item, LB, UB)
else:
LB=mid+1
return Binary_Search(sequence, item, LB, UB)

L=eval(input("Enter the elements in sorted order: "))


n=len(L)
element=int(input("Enter the element that you want
to search :"))
found=Binary_Search(L,element,0,n-1)
if found>=0:
print(element, "Found at the index : ",found)
else:
print("Element not present in the list")
OUTPUT:

You might also like