12 cs practicals 1
12 cs practicals 1
Solution:
op == "+":
elif op == "-":
elif op == "*":
result = val1 *
if val2 == 0:
else:
elif op == "//":
else:
Solution:
# To find whether a number is perfect or not
def pernum(num):
divsum=0 for i in
range(1,num):
if num%i == 0:
divsum+=i if
divsum==num:
print('Perfect Number')
else:
pernum(6)
pernum(15)
Solution:
ans = no % 10; sum = sum + (ans * ans * ans) no = int (no / 10) if sum == no1:
print("Armstrong Number")
else:
Solution:
loop) num = int(input("Enter the number for calculating its factorial : "))
fact = 1 i = 1
while i<=num:
fact = fact*i
i=i+1
Solution: #fibonacci i
x=0
y=1z=1
print("Fibonacci series \
while(z<= i):
print(z, end="
") x=y
y=z
z=x+y
Program 6: Write a Program to enter the string and to check if it’s palindrome or not using loop.
Solution:
# Program to enter the string and check if it’s palindrome or not using ‘for’
if newlist[i]!=newlist[ed]:
ed = ed - 1
Solution:
my_list = ['p','r','o','b','e']
# Output: p
print(my_list[0])
# Output: o
print(my_list[2])
# Output: e
print(my_list[4])
n_list = ["Happy",
[2,0,1,5]]
print(n_list[0][1],n_list[0][2],n_list[0]
[3])
# Output: 5 print(n_list[1]
[3])
Program 8: Write a Program to enter the numbers in a list using split () and to use all the
functions related to list.
Solution:
#Program to enter the numbers in a list using split () and to use all the functions related to
list.
# print (len(numbers))
memo=[]
n")) memo.insert(i,x)
i+=1
print(memo)
memo.append(25)
print("Second List")
print(memo) msg=input("Enter
newlist[:0]=msg l=len(newlist)
print(l)
Program 9: Write a Program to enter the number and print the Floyd’s Triangle in
decreasing order.
n=int(input("Enter the
number :")) for i in range(5,0,-1):
for j in range(5,i-1,-1):
print('\n')
Program 10: Write a Program to find factorial of entered number using user-defined module
fact().
Program 11: Write a Program to enter the numbers and find Linear Search, Binary
Search, Lowest Number and Selection Sort using list/array code.
array_operation():
ch=1 while
ch!=10:
Reverse Array\n')
print('10 Exit\n')
if ch==1 : appendarray()
elif ch==3 :
selection_sort()
range(0,10):
arr.insert(i,x)
#-------------------------------------------
---------------------------------------------
---------------------------------
----------------
def print_array():
for i in range(0,10):
print(arr[i]),
#-------------------------------------------------------------------------------------------------------------------------
----------------
def reverse_array():
for i in range(1,11):
print(arr[-i]),
#-------------------------------------------------------------------------------------------------------------------------
----------------
def lsearch():
try:
#-------------------------------------------------------------------------------------------------------------------------
----------------
def linear_search():
fl=0 for i in
range(0,10): if
arr[i]==x :
fl=1
print ('Number Found at %d location'% (i+1))
break
if fl==0 :
#-------------------------------------------------------------------------------------------------------------------------
----------------
def binary_search():
fl=0 low=0
heigh=len(arr)
while low<=heigh :
mid=int((low+heigh)/2)
if arr[mid]==x :
fl=1
print ('Number Found at %d location'% (mid+1))
break
elif arr[mid]>x :
low=mid+1
else :
heigh=mid-1
if fl==0 :
#-------------------------------------------------------------------------------------------------------------------------
----------------
def min_number():
n=arr[0] k=0
for i in range(0,10):
if arr[i]<n :
n=arr[i]
k=i
#-------------------------------------------------------------------------------------------------------------------------
----------------
def selection_sort():
for i in range(0,10):
n=arr[i] k=i
for j in range(i+1,10):
if arr[j]<n :
n=arr[j]
k=j
arr[k]=arr[i]
arr[i]=n
array_operation()
Program 12: Write a Program to read data from data file and show Data File Handling
related functions utility in python.
Solution:
f=open("test.txt",'r')
print(f.name)
f_contents=f.read()
print(f_contents)
f_contents=f.readlines()
print(f_contents)
f_contents=f.readline()
print(f_contents)
for line in f:
print(line, end='')
f_contents=f.read(50)
print(f_contents) size_to_read=10
f_contents=f.read(size_to_read) while
len(f_contents)>0:
print(f_contents)
print(f.tell())
f_contents=f.read(size_to_read)
Program 13: Write a Program to read data from data file in append mode and use
writeLines function utility in python.
Solution:
Solution:
f=open("test.txt",'r')
read=f.readlines()
f.close() times=0 #the variable has been created to show the number of times the
loop runs times2=0 #the variable has been created to show the number of times
print(times2)
Program 15: Write a Program to read data from data file in read mode and append the
words starting with letter ‘T’ in a given file in python.
Solution:
f=open("test.txt",'r')
read=f.readlines()
f.close() id=[]
for ln in
read:
if ln.startswith("T"):
id.append(ln) print(id)
Program 16: Write a Program to show MySQL database connectivity in python.
Solution:
import mysql.connector
con=mysql.connector.connect(host='localhost',user='root',password='',db='school
data=stmt.fetchone() print(data)
Program 17: Write a Python program to implement all basic operations of a stack, such as
adding element (PUSH operation), removing element (POP operation) and displaying the
stack elements (Traversal operation) using lists.
Solution:
#Implementation of List as
number :")
s.append(a)
elif (choice==2):
if (s==[]): print
("Stack Empty")
else:
elif (choice==3):
element to first
print (s[i])
else:
Solution:
#using Stack
Program 19: Write a program in Python to add, delete and display elements from a queue
using list.
Solution:
a.append(b)
elif (choice==2):
if (a==[]):
print("Queue Empty")
else:
a.pop(0)
elif (choice==3):
l=len(a) for i
in range(0,l):
print (a[i])
else:
print("wrong input")
c=input("Do you want to continue or not: ")
Program 20: Perform all the operations with reference to table ‘Employee’ through
MySQL-Python connectivity.
Solution:
import MySQLdb
# Using connect method to connect database db1 =
MySQLdb.connect("localhost","root","","TESTDB" )
cursor = db1.cursor()
sql = "CREATE TABLE EMP(empno integer primary key,ename varchar(25) not null,salary
MySQLdb.connect("localhost","root","","TESTDB" )
cursor = db1.cursor()
# Prepareing SQL statement to insert one record with the given values
try:
cursor.execute(sql)
db1.commit() except:
db1.rollback() db1.close()
Fetching all the records from EMP table having salary more than 70000.
MySQLdb.connect("localhost","root","","TESTDB" )
try:
cursor.execute(sql)
#using fetchall() function to fetch all records from the table EMP and store
db1.close()
cursor = db1.cursor()
#Preparing SQL statement to increase salary of all employees whose salary is less than
80000 sql = "UPDATE EMP SET salary = salary +1000 WHERE
salary<80000;"
try:
cursor.execute(sql)
db1.commit()
except:
db1.rollback()
db1.close()
MySQLdb db1 =
MySQLdb.connect("localhost","root","","TESTDB" ) cursor =
db1.cursor() sal=int(input("Enter salary whose record to be
try:
cursor.execute(sql) print(cursor.rowcount,
db1.rollback()
db1.close()
Output:
1 record(s) deleted
>>>
VANASTHALI PUBLIC
SCHOOL
CS PRACTICAL
FILE
Submitted By:
NAME-LAKSHAY
CLASS-12 A
ROLL NO-07
BOARD-CBSE
Submitted To: MR.BANWARI LAL RATHI
CERTIFICATE
This is to certify that LAKSHAY of
class: XII A of VANASTHALI PUBLIC
SCHOOL has done his PRACTICAL on
PYTHON AND SQL under my
supervision. He has taken interest and
has shown at most sincerity in
completion of this project.
I certify this project up to my expectation &
as per guidelines issued by CBSE, NEW DELHI.
PRINCIPAL
ACKNOWLEDGMENT
It is with pleasure that I acknowledge my
sincere gratitude to our teacher, MR.
BANWARI LAL RATHI who taught and
undertook the responsibility of teaching
the subject computer science. I have been
greatly benefited from his classes.
I am especially indebted to our Principal
MRS.VEENA HIMANI who has always been a
source of encouragement and support and
without whose inspiration this project
would not have been a successful I would
like to place on record heartfelt thanks to
him.
Finally, I would like to express my
sincere appreciation for all the other
students in my batch their friend & the
fine time that we all shared together.
SOFTWARES
Program 5: Write a Program to enter the number of terms and to print the Fibonacci
Series.
Program 6: Write a Program to enter the string and to check if it’s palindrome or not using loop.
Program 8: Write a Program to enter the numbers in a list using split () and to use all the functions
related to list
Program 9: Write a Program to enter the number and print the Floyd’s Triangle in
decreasing order.
Program 10: Write a Program to find factorial of entered number using user-defined module
fact().
Program 11: Write a Program to enter the numbers and find Linear Search, Binary
Search, Lowest Number and Selection Sort using list/array code.
Program 12: Write a Program to read data from data file and show Data File Handling
related functions utility in python.
Program 13: Write a Program to read data from data file in append mode and use
writeLines function utility in python.
Program 14: Write a Program to read data from data file in read mode and count the
particular word occurrences in given string, number of times in python.
Program 15: Write a Program to read data from data file in read mode and append the
words starting with letter ‘T’ in a given file in python.
Program 17: Write a Python program to implement all basic operations of a stack, such as
adding element (PUSH operation), removing element (POP operation) and displaying the
stack elements (Traversal operation) using lists.
Program 18: Write a program to display unique vowels present in the given word using
Stack.
Program 19: Write a program in Python to add, delete and display elements from a queue
using list.
Program 20: Perform all the operations with reference to table ‘Employee’ through MySQL-
Python connectivity.