You are on page 1of 35

CERTIFICATE:

This is to certify that Somya of class


12(A) has successfully completed the
practical work of computer science for
class XII practical examination of the
school 2022-23.It is further certified
that this practical is the individual work
of the candidate.

Somya Mrs.Purnima kHURANA


Signature: Signature:
QUESTION 1:

CODE:
def leexchange():

for i in range(0,num-1,2):

first=lst[i]

second=lst[i+1]

lst[i]=second

lst[i+1]=first

print("new list:",lst)

lst=[]

num=int(input("Enter the number of elements:"))

for n in range(num):

numbers=int(input("Enter the number"))

lst.append(numbers)

print("original list:",lst)

leexchange()
OUTPUT:

QUESTION 2:
QUESTION 3:

CODE:
import pickle

s={}

file2=open("student.dat",'wb')

R=int(input("Enter roll number"))

N=input("Enter name")

M=float(input("Enter marks"))

#add read data into dictionary

s['Rollno']=R

s['Name']=N

s['Marks']=M

#write into the file

pickle.dump(s,file2)

file2.close()

OUTPUT:
QUESTION 4:

CODE:

def reverse():

for i in range(len(L)):

reversedList.append(L[len(L)-i-1])

print("reversedList",reversedList)

reversedList=[]

L=[]

n=int(input("Enter the no.of elements"))

for i in range(0,n):

ele=int(input("Enter the number"))

L.append(ele)

print("original list",L)

reverse()
OUTPUT:

QUESTION 5:

CODE:
def cnt():

f=open("test.txt","r")

cont=f.read()

print(cnt)

v=0

lc=0

uc=0

cons=0

for i in cont:

if(i.islower()):
lc+=1

elif(i.isupper()):

uc+=1

if(i in ['a','e','i','o','u',]):

v+=1

elif(i.isalpha()):

cons+=1

f.close()

print("vowels=",v)

print("consonants=",cons)

print("lowercase=",lc)

print("uppercase=",uc)

cnt()

OUTPUT:
QUESTION 6:

CODE:
student = [{'id': 1, 'success': True, 'name': 'Shyana'},

{'id': 2, 'success': False, 'name': 'Rashi'},

{'id': 3, 'success': True, 'name': 'Alisha'}]

print(sum(d['id'] for d in student))

print(sum(d['success'] for d in student))

OUTPUT:

QUESTION 7:

CODE:
f=open("test.txt","r")
f1=open("writetest2.txt","w")

l=f.readlines()

for i in l:

if 'a' in i:

f1.write(i)

print("file created successfully")

f.close()

f1.close()

OUTPUT:

QUESTION 8:

CODE:
import csv

def write():

f=open("departments.csv","w",newline='')

wrt=csv.writer(f)

wrt.writerow(["Department_ID","Department_Name","Manager_ID"])

while True:
d_id=int(input("Enter department id:"))

d_name=input("Enter department name: ")

m_id=int(input("Enter manager id: "))

data=[d_id,d_name,m_id]

wrt.writerow(data)

ch=input("Do you want to enter more records(Y/N:")

if ch in 'Nn':

break

f.close()

def read():

f=open("departments.csv","r",newline='\r\n')

s_reader=csv.reader(f)

for i in s_reader:

print(i)

write()

read()
OUTPUT:

QUESTION 9:

CODE:
import pickle
def Writerecord(sroll,sname):

with open ('Student.dat','ab') as Myfile:

srecord={"SROLL":sroll,"SNAME":sname}

pickle.dump(srecord,Myfile)

def Readrecord():

with open ('Student.dat','rb') as Myfile:

print("\n-------DISPALY STUDENTS DETAILS--------")

print("\nRoll No.",' ','Name','\t',end='')

print()

while True:

try:

rec=pickle.load(Myfile)

print(' ',rec['SROLL'],'\t ' ,rec['SNAME'])

except EOFError:

break

def Input():

n=int(input("How many records you want to create :"))

for ctr in range(n):

sroll=int(input("Enter Roll No: "))

sname=input("Enter Name: ")


Writerecord(sroll,sname)

def SearchRecord(roll):

with open ('Student.dat','rb') as Myfile:

while True:

try:

rec=pickle.load(Myfile)

if rec['SROLL']==roll:

print("Roll NO:",rec['SROLL'])

print("Name:",rec['SNAME'])

except EOFError:

print("Record not find..............")

print("Try Again..............")

break

def main():

while True:

print('\nYour Choices are: ')

print('1.Insert Records')

print('2.Dispaly Records')
print('3.Search Records (By Roll No)')

print('0.Exit (Enter 0 to exit)')

ch=int(input('Enter Your Choice: '))

if ch==1:

Input()

elif ch==2:

Readrecord()

elif ch==3:

r=int(input("Enter a Rollno to be Search: "))

SearchRecord(r)

else:

break

main()
OUTPUT:
QUESTION 10:

CODE:
import random

min=1

max=6

roll_again="y"

while roll_again=="y" or roll_again=="Y":

print("Rolling the dice...")

val=random.randint(min,max)

print("You get...:",val)

roll_again=input("Roll the dice again?(y/n)...")

OUTPUT:
QUESTION 11:

CODE:
emp_dict = { }

while True :

name = input("Enter employee name : ")

sal = int(input("Enter employee salary : "))

emp_dict[ name] = sal

choice = input("Do you want to Enter another record Press 'y' if yes : ")

if choice != "y" :

break

print(emp_dict)

OUTPUT:
QUESTION 12:

CODE:
def PUSH(Arr):

s=[]

for i in range(0,len(Arr)):

if Arr[i]%5==0:

s.append(Arr[i])

if len(s)==0:

print("Stack is empty")

else:

print(s)

Arr=[25,10,14,30,15,9]

PUSH(Arr)

OUTPUT:
QUESTION 13:

CODE:
def RShift(L):

n=len(L)

last=L[n-1]

for i in range(n-2,-1,-1):

L[i+1]=L[i]

L[0]=last

print("modified list is:",

L)

L=[]

n=int(input("Enter the no. of element:"))

for i in range (0,n):

ele=int(input("Enter the elements"))

L.append(ele)

RShift(L)
OUTPUT:

QUESTION 14:
QUESTION 15:

CODE:

import mysql.connector

mydb=mysql.connector.connect(host="localhost", user="root",
passwd="mysql",

database="real_estate")

mycursor=mydb.cursor()

mycursor.execute("create table employee(first_name varchar(20),\

last_name varchar(20),age integer,income varchar(10))")

mycursor.close()

mydb.close()
OUTPUT:

QUESTION 16:

CODE:
import mysql.connector

while True:

print("--------CLIENT PROFILE---------")

print("1-Create table")
print("2-Insert details")

print("3-Update details")

print("4-Deletion of records")

print("5-Display details")

print("6-Main menu")

choice=int(input("Enter your choice(1-6)"))

while True:

#TO CREATE TABLE OF CLIENT

if choice==1:

mydb=mysql.connector.connect(host="localhost",

user="root",

passwd="mysql",

database="real_estate")

mycursor=mydb.cursor()

mycursor.execute ="CREATE TABLE client_detail(Id int(11) PRIMARY KEY NOT


NULL,Name VARCHAR(255), Address VARCHAR(255))"

print("table created successfully")

mydb.close()

break

#TO INSERT RECORD OF CLIENT

elif choice==2:

mydb=mysql.connector.connect(host="localhost",

user="root",

passwd="mysql",

database="real_estate")
mycursor=mydb.cursor()

id=int(input("Enter client id"))

name=input("Enter client name")

adrs=str(input("Enter address"))

sql="INSERT INTO
client_detail(Id,Name,Address)VALUES({},'{}','{}')".format(id,name,adrs)

mycursor.execute(sql)

print(mycursor.rowcount,"Record inserted successfully")

mydb.commit()

mydb.close()

break

#TO UPDATE DETAILS OF CLIENT

elif choice==3:

mydb=mysql.connector.connect(host="localhost",

user="root",

passwd="mysql",

database="real_estate")

mycursor=mydb.cursor()

query="UPDATE client_detail SET address='Kolkata' WHERE name='Riy'"

mycursor.execute(query)

mydb.commit()

print("Data updated sccessfully")

break

#TO DELETE DETAILS OF CLIENT

elif choice==4:
mydb=mysql.connector.connect(host="localhost",

user="root",

passwd="mysql",

database="real_estate")

mycursor=mydb.cursor()

query="DELETE from client_detail WHERE id=1003"

mycursor.execute(query)

mydb.commit()

print("Deletion sccessfully")

break

#TO DISPLAY DETAILS OF CLIENT

elif choice==5:

mydb=mysql.connector.connect(host="localhost",

user="root",

passwd="mysql",

database="real_estate")

mycursor=mydb.cursor()

query="SELECT*from client_detail"

mycursor.execute(query)

myrecords=mycursor.fetchall()

print("Id,Name,Address are:")

for x in myrecords:

print (x)

print(mycursor.rowcount,"record selected")
mycursor.close()

mydb.close()

break

else:

print("Error:Invalid choice try again")

break

OUTPUT:
QUESTION 18:

CODE:

def square_cube(L,n):

h=int(n/2)

for i in range(0,h):

L[i]=L[i]*L[i]

for j in range (h,n):

L[j]=L[j]**3

print("Modified list is:",L)

L=[]

n=int(input("Enter the no.of elements:"))

for i in range(0,n):

ele=int(input("Enter the element:"))

L.append(ele)

square_cube(L,n)
OUTPUT:

QUESTION 19:

CODE:

list = [10, 20, 10, 30, 10, 40, 10, 50]

n = 10

print ("Original list:")

print (list)

i=0

length = len(list)
while(i<length):

if(list[i]==n):

list.remove(list[i])

length=length -1

continue

i=i+1

print("list after removing elements:")

print(list)

OUTPUT:
QUESTION 20:

CODE :

import pickle as p

def Createfile():

BookNo=int(input("Enter Book No:"))

Book_Name=input("Enter Book Name:")

Author=input("Enter Author:")

Price=float(input("Enter Price"))

l=[BookNo,Book_Name,Author,Price]

f=open("Book.dat","ab")

p.dump(l,f)

f.close()

def Countrec(Author):

count=0

f=open("Book.dat","rb")

while True:

try:

l=p.load(f)
if l[2]==Author:

count=count+1

except EOFError:

print("EOF reached")

break

return count

OUTPUT:

You might also like