0% found this document useful (0 votes)
14 views36 pages

Practical No

The document is a practical file for Class 12 Computer Science, containing various Python programming exercises. It includes tasks such as calculating factorials, reading and processing text files, creating and updating binary files, and working with CSV files. Additionally, it covers SQL commands and database management, along with examples of integrating Python with MySQL.

Uploaded by

bhoomikt7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views36 pages

Practical No

The document is a practical file for Class 12 Computer Science, containing various Python programming exercises. It includes tasks such as calculating factorials, reading and processing text files, creating and updating binary files, and working with CSV files. Additionally, it covers SQL commands and database management, along with examples of integrating Python with MySQL.

Uploaded by

bhoomikt7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

CS Practical File CLASS 12

Practical No-1: WAP in Python to find the factorial of a number using function.
def fact(n):
f=1

while(n>0):
f=f*n
n=n-1
return f

n=int(input("Enter a number to find the factorial:"))


if(n<0):
print("Factorial of -ive number is not possible")
elif(n==0):

print("Factorial of 0 is 1")
else:
factorial=fact(n)
print("Factorial of",n,"is=",factorial)

OUTPUT:
CS Practical File CLASS 12

Practical No-2: WAP in Python to implement default and positional parameters.

OUTPUT:
CS Practical File CLASS 12

Practical No- 3: Write a program in Python to input the value of x and n and
print the sum of the following series.
1+x+x^2+x^3+--------------------------------x^n

x=int(input("Enter the value of x:"))


n=int(input("Enter the value of n:"))
sum=0
for i in range(0,n+1):
sum=sum+x**i

print("Sum of series=",sum)

OUTPUT
CS Practical File CLASS 12

Practical No -4: WAP in Python to read a text file and print the number of vowels and
consonants in the file.

OUTPUT:
Number of Vowels in the file= 1
Number of Consonants in the file= 35

Number of Total Chars in the file= 41


CS Practical File CLASS 12

Practical No-5: WAP in Python to read a text file and print the line or paragraph starting
with the letter ‘S’.

f=open("abc.txt","r")
line=f.readline()
lc=0

while line:
if line[0]=='s' or line[0]=='S':
print(line,end="")
lc=lc+1

line= f.readline()
f.close()
print("Total number of lines start with 's' or 'S' =",lc)

OUTPUT
sameer
samayra

sandhya
Saisa
Total number of lines start with 's' or 'S' = 4
CS Practical File CLASS 12

Practical No-6: WAP in Python to read a text file and print the number of uppercase and
lowercase letters in the file.
f=open("Vowel.txt","r")

data=f.read()
U=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R',\
'S','T','U','V','W','X','Y','Z']
L=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r',\

's','t','u','v','w','x','y','z']
cu=0
cl=0
for i in data:

if i in U:
cu=cu+1
elif i in L:
cl=cl+1
ct=0

for i in data:
ct=ct+1
print("Number of Uppercase letters in the file=",cu)
print("Number of Lowercase letters in the file=",cl)

print("Number of Total Chars in the file=",ct)

f.close()

OUTPUT
CS Practical File CLASS 12

Practical No-6: WAP in Python to read a text file and print the number of uppercase and
lowercase letters in the file.
f=open("Vowel.txt","r")

data=f.read()

cu=0
cl=0

for i in data:
if i.isupper():
cu=cu+1
elif i.islower():

cl=cl+1

ct=0
for i in data:
ct=ct+1

print("Number of Uppercase letters in the file=",cu)


print("Number of Lowercase letters in the file=",cl)
print("Number of Total Chars in the file=",ct)

OUTPUT
CS Practical File CLASS 12

Practical No-7: WAP in Python to create a binary file with name and roll number of the
students. Search for a given roll number and display the name of student.

OUTPUT
CS Practical File CLASS 12

Practical No-8: Create a binary file with roll no , name and marks of some students and
update the marks of specific student.
import pickle

S={}
f=open('stud.dat','wb')
c='y'
while c=='y' or c=='Y':

rno=int(input("Enter the roll no. of the student:"))


name=input("Enter the name of the student:")
marks=int(input("Enter the marks of the student:"))
S['RollNo']=rno

S['Name']=name
S['Marks']=marks
pickle.dump(S,f)
c=input("Do You Want to add more students(y/n):")
f.close()

f=open('stud.dat','rb+')
rno=int(input("Enter the roll no. of the student to be updated:"))
marks=int(input("Enter the updated marks of the student:"))
f.seek(0,0)

m=0
try:
while True:
pos=f.tell()

S=pickle.load(f)
if S["RollNo"]==rno:
f.seek(pos)
S["Marks"]=marks

pickle.dump(S,f)
m=m+1
CS Practical File CLASS 12

except EOFError:
f.close()
if m==0:

print("Student not Found")


else:
f=open('stud.dat','rb')
try:

while True:
S=pickle.load(f)
print(S)
except EOFError:

f.close()

OUTPUT
CS Practical File CLASS 12

Practical No-9: Create a binary file with eid, ename and salary and update the salary of the
Employee.

import pickle
E={}
f=open('emp.dat','wb')
c='y'

while c=='y' or c=='Y':


eid=int(input("Enter the Emp Id of the Employee:"))
ename=input("Enter the name of the Employee:")
salary=float(input("Enter the salary of the Employee:"))

E['Emp_Id']=eid
E['Emp_Name']=ename
E['Salary']=salary
pickle.dump(E,f)
c=input("Do You Want to add more employee(y/n):")

f.close()
f=open('emp.dat','rb+')
eid=int(input("Enter the Emp Id of the employee to be updated:"))
salary=float(input("Enter the Emp updated salary of the employee:"))

f.seek(0,0)
m=0
try:
while True:

pos=f.tell()
E=pickle.load(f)
if E["Emp_Id"]==eid:
f.seek(pos)

E["Salary"]=salary
CS Practical File CLASS 12

pickle.dump(E,f)
m=m+1
except EOFError:

f.close()
if m==0:
print("Employee not Found")
else:

f=open('emp.dat','rb')
try:
while True:
E=pickle.load(f)

print(E)
except EOFError:
f.close()

OUTPUT
CS Practical File CLASS 12

Practical No-10 : Create a Binary File with 10 random numbers from 1 to 40 and print those
numbers.

import pickle,random
N=[]
f=open("abc.txt","wb")
for i in range(10):

N.append(random.randint(1,40))
pickle.dump(N,f)
f.close()
print("File Created:")

print("Content of File:")
f=open("abc.txt","rb")
data=pickle.load(f)
for i in data:
print(i)

f.close()
CS Practical File CLASS 12

OUTPUT
CS Practical File CLASS 12

Practical No-11: WAP in Python to create a CSV file with the details of 5 students.

import csv

f=open("student.csv","w",newline="")
cw=csv.writer(f)
cw.writerow(['Rollno','Name','Marks'])
for i in range(5):

print("Student Record of ",(i+1))


rollno=int(input("Enter Roll No.:"))
name=input("Enter Name:")
marks=float(input("Enter Marks:"))

sr=[rollno,name,marks]
cw.writerow(sr)
f.close()
print("File Created Successfully")

OUTPUT:
CS Practical File CLASS 12

Practical No-12: WAP in Python to read a CSV file.

import csv

f=open("student.csv","r")
cr=csv.reader(f)
print("Content of CSV File:")
for r in cr:

print(r)
f.close()

OUTPUT
CS Practical File CLASS 12

Practical No-13: Write a menu driven program which insert, delete and display the details of
an employee such as eid, ename and salary using Stack.
Employee=[]

c='y'
while(c=="y" or c=="Y"):
print("1:Add Employee Detail:")
print("2:Delete Employee Detail:")

print("3:Display Employee Detail:")


choice=int(input("Enter your choice:"))
if(choice==1):
eid=int(input("Enter Employee Id:"))

ename=input("Enter Employee Name:")


salary=float(input("Enter Employee Salary:"))
emp=(eid,ename,salary)
elif(choice==2):
if(Employee==[]):

print("Stack Empty")
else:
print("Deleted element is:",Employee.pop())
elif(choice==3):

L=len(Employee)
while(L>0):
print(Employee[L-1])
L=L-1

else:
print("Wrong Input")
c=input("Do you want to continue? Press 'y' to Continue:")
CS Practical File CLASS 12

OUTPUT
CS Practical File CLASS 12
CS Practical File CLASS 12

Practical No-14: Create a student table and insert data. Implement the following SQL
commands on the student table:
o ALTER table to add new attributes / modify data type / drop attribute
o UPDATE table to modify data
o ORDER By to display data in ascending / descending order
o DELETE to remove tuple(s)
o GROUP BY and find the min, max, sum, count and average
o Joining Two or more Tables.

DATABASE MANAGEMENT

1.Create Database:

2.Use Database:

3.Create Table:
CS Practical File CLASS 12

4.Insert Command:

5.Select Command :

6.Alter Command(Add Attribute):


CS Practical File CLASS 12

7.Alter Command (Drop Attribute)

8.Alter Command(Modify Datatype):


CS Practical File CLASS 12

9.Update table to modify data:

10.Order by Command (Ascending Order/Descending Order)


CS Practical File CLASS 12

11.Delete Command to remove tuple(s):

12.GROUP BY and find the min, max, sum, count and average
CS Practical File CLASS 12
CS Practical File CLASS 12

13.Joining of two tables


CS Practical File CLASS 12

Practical No-15: Write a python program to maintain employee details like


empno,name and salary using Queues data structure?

(implement insert(), delete() and traverse() functions)

employee=[]
def add_element():
empno=input("Enter empno :")
name=input("Enter name :")

sal=input("Enter sal :")


emp=(empno,name,sal)
employee.append(emp)
def del_element():
if(employee==[]):

print("Underflow")
else:
empno,name,sal=employee.pop(0)
print("poped element is")

print("empno",empno,"name",name,"salary",sal)
def traverse():
if not (employee==[]):
n=len(employee)

for i in range(0,n):
print(employee[i])
else:
print("Empty,No employee to display")

while True:
print("1.Add employee ")
print("2.Delete employee")
print("3.Traversal")
print("4.Exit")
ch=int(input("Enter your choice :"))
CS Practical File CLASS 12

if(ch==1):
add_element()
elif(ch==2):

del_element()
elif(ch==3):
traverse()
elif(ch==4):

print("End")
break
else:
print("Invalid choice")

OUTPUT:
CS Practical File CLASS 12

Practical No-16: Priyanshu,a student of class12,created a table “student”.Grade is one of


the columns of this table.Write the SQL query to find the details of students whose grade
have not been entered.
CS Practical File CLASS 12

Practical No-17: Zara is using a Table with the following details:


Customer(CID, Cname, City)
i) Write the SQL Query to select all the customers that starts with the letter “a”.

ii) Return all customers from a city that starts with ‘L’ followed by one wildcard
character, then ‘nd’ and then two wildcard characters.
CS Practical File CLASS 12

Practical No.18 Harsh is using a table employee.It has following details:


Employee(Code ,Name,Salary,Deptcode).
Write the SQL query to find the maximum salary department wise.
CS Practical File CLASS 12

Practical No-19: Write the SQL Query to increase 10% salary of the employee whose
experience is more than 3 year of the table Emp(Id, Name,Salary,exp).
CS Practical File CLASS 12

Practical No-20: Write the SQL query for Natural Join of two tables
Bank_Account (Acode,Name,Type) and Branch(Acode,City).
CS Practical File CLASS 12
CS Practical File CLASS 12

Interface Python with MySQL

Practical No:-21 Program to Search the particular record .If not found,display appropriate
message.

import mysql.connector
con=mysql.connector.connect(host='localhost',user='root',passwd='12345',

database='Document',charset='utf8')
if con.is_connected():
print("Connected..")
else:

print("Not Connected..")

cur=con.cursor()
while True:
found=0

s=input("Enter Stream..")
query="Select*from student where stream='%s' "%s
cur.execute(query)
data=cur.fetchall()

for i in data:
print(i)
found=1
if found==0:

print("No record found..")


ch=input("Do you want to search more records or not")
if ch in 'Nn':
break
CS Practical File CLASS 12

OUTPUT

You might also like