You are on page 1of 30

CS Practical File Session 2023-24 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 Session 2023-24 CLASS 12

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

OUTPUT:
CS Practical File Session 2023-24 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 Session 2023-24 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 Session 2023-24 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 Session 2023-24 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 Session 2023-24 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 Session 2023-24 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 Session 2023-24 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
CS Practical File Session 2023-24 CLASS 12

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

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 Session 2023-24 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
CS Practical File Session 2023-24 CLASS 12

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

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:
CS Practical File Session 2023-24 CLASS 12

f.close()

OUTPUT

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")
CS Practical File Session 2023-24 CLASS 12

data=pickle.load(f)

for i in data:

print(i)

f.close()

OUTPUT
CS Practical File Session 2023-24 CLASS 12

Practical No-11: WAP in Python to create a CSV file with the details of 5 students.
CS Practical File Session 2023-24 CLASS 12

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:

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


CS Practical File Session 2023-24 CLASS 12

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

Practical No-14: Write a menu driven program which insert, delete and display the
details of an employee such as eid, ename and salary using Stack.
CS Practical File Session 2023-24 CLASS 12

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:")

OUTPUT
CS Practical File Session 2023-24 CLASS 12
CS Practical File Session 2023-24 CLASS 12
CS Practical File Session 2023-24 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 Session 2023-24 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 Session 2023-24 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 Session 2023-24 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 Session 2023-24 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 Session 2023-24 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 Session 2023-24 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 Session 2023-24 CLASS 12
CS Practical File Session 2023-24 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 Session 2023-24 CLASS 12

OUTPUT

You might also like