Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
31 views18 pages

C S Practical

This document is a practical file for a Computer Science course at Sarvodaya Vidyalaya, detailing various programming exercises completed by a student named Abhay Kumar. It includes code snippets for tasks such as reading files, manipulating data, creating binary files, and implementing a stack using Python, as well as MySQL queries for database management. The document serves as a record of the student's practical work during the academic year 2022-23.

Uploaded by

ashish mishra
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)
31 views18 pages

C S Practical

This document is a practical file for a Computer Science course at Sarvodaya Vidyalaya, detailing various programming exercises completed by a student named Abhay Kumar. It includes code snippets for tasks such as reading files, manipulating data, creating binary files, and implementing a stack using Python, as well as MySQL queries for database management. The document serves as a record of the student's practical work during the academic year 2022-23.

Uploaded by

ashish mishra
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/ 18

Page _no.

-01

SARVODAYA VIDYALAYA F.U-BLOCK


PITAM PURA DELHI-110034

COMPUTER SCIENCE
PRACTICAL FILE
Acedemic year(2022-23)

NAME– Abhay Kumar


CLASS– 12th A
ROLL_NO.– 28
STUDENT_ID– 20170228678

SUBJECT TEACHER– HUNNY


TOKAS PGT (C.S.)
Page _no.-02

TABLE OF CONTENTS
S.NO. TOPIC PAGE
NO.
Read a test file line by line and and display each word
01 separated by a .
3
Read a text file and display the number of vowels/
02 consonents/uppercase/lowercase characters in the
4-5
file.
Remove all the lines that contain character "a" in a file
03 and write it to another file.
6
Create a binary file with name and roll number. search
04 for a given roll number and display the name,if not
7-8
found display appropriate message.
Create a binary file with roll_no, name and marks. In-
05 put a roll_no and update the marks.
9-10
Write a random number generator that generate ran-
06 dom numbers between 1 and 6 (simulates a dice)
11

07 Write a python program to implement #a 12-13


stack using list.

08 create a CSV file by entering user-id and 14


#password read and search the password
for #given user-id.
MySQL QUERIES
09 15-18
10
Page _no.-03
PROGRAM CODE 01:
#Read a test file line by line and and display each word separated by a #.
fname=input("Enter the file name:")
fhand=open(fname+".txt","r")
for line in fhand:
for word in line.split():
print(word,end="#")
print()
fhand.close()

RUN AND OUTPUT:


G:\Abha ki practice\School\class 12> practical_1.py
Enter the file name:Temp
Hello#There!#
I#am#python.#
I#am#
X-DSPAM-Confidence:#0.8475#
X-DSPAM-Confidence:#0.7623#
You#can#use#me#to#create#innovative#programs.#
X-DSPAM-Confidence:#0.6534#
Thank#you#
Page _no.-04
PROGRAM CODE 02:
#Read a text file and display the number of vowels/consonents/uppercase/
lowercase characters in the file.
while True:
try:
fname=input("Enter the name of file:")
break
except:
print("!!!!File not exist in current directory!!!!","\n","____TRY-AGAIN____")
continue
vowels,consonents,uppercase,lowercase=(0,0,0,0)
with open(fname+".txt","r") as fhand:
for line in fhand:
line=line.rstrip()
for letter in line:
if letter in ('a',"e",'i',"o","u","A","E","I","O",'U'):
vowels+=1
else: consonents+=1
if letter.isupper(): uppercase+=1
if letter.islower(): lowercase+=1
iter=("vowels",vowels),("consonents",consonents),("uppercase",uppercase),
('lowercase',lowercase)
for name,value in (iter):
print("No. of",name,"letters in your file:",value)
Page _no.-05
RUN AND OUTPUT:
G:\Abha ki practice\School\class 12> practical_2.py
Enter the name of file:Temp
No. of vowels letters in your file: 44
No. of consonents letters in your file: 125
No. of uppercase letters in your file: 27
No. of lowercase letters in your file: 88
Page _no.-06
PROGRAM CODE 03:
#Remove all the lines that contain character "a" in a file and write it to an-
other file.
fname=input("Enter the file name:")
write_old=[]
write_new=[]
with open(fname+".txt","r") as fhand:
for line in fhand:
if "a" in line:
write_new.append(line)
else: write_old.append(line)
#Writting the filtered lines into new file and update old file.
with open(fname+".txt","w") as fhand:
fhand.writelines(write_old)
with open(fname+"_deleted_lines.txt",'w') as fhand:
fhand.writelines(write_new)
print("File named_",fhand.name,"_is created at current directiry")

RUN AND OUTPUT:


G:\Abha ki practice\School\class 12>
practical_3.py
Enter the file name:Temp
File named_ Temp_deleted_lines.txt
_is created at current directiry
Page _no.-07
PROGRAM CODE 04:
#Create a binary file with name and roll number. search for a given
#roll number and display the name,
#if not found display appropriate message.
from pickle import load as l, dump as d
data=dict()
while True:
name=input("Enter the name:")
if len(name)<1: break
roll=int(input("Enter the Roll no.:"))
data[roll]=name
print(">>>>Press Enter to load data in the file.")
with open("std.dat","ab") as fhand:
d(data,fhand)
run=input(">>>>Press Enter to serach by rolI_no. \n >>>>Press any key to Quit!")
if len(run)>=1: quit()
with open("std.dat","rb") as fhand:
search=int(input("Enter the roll_no. you want to search:"))
data=dict()
while True:
try:
tmp_data=l(fhand)
data.update(tmp_data)
except: break
if search in data.keys():
print("Name found of the roll_no.",search,"is",data[search])
else:print("NO Name found at roll_no.",search,"!!!!")
Page _no.-08
RUN AND OUTPUT:
G:\Abha ki practice\School\class 12> practical_4.py
Enter the name:Abhay
Enter the Roll no.:28
>>>>Press Enter to load data in the file.
Enter the name:Adarsh
Enter the Roll no.:26
>>>>Press Enter to load data in the file.
Enter the name:Rahul
Enter the Roll no.:16
>>>>Press Enter to load data in the file.
Enter the name:Kunal
Enter the Roll no.:51
>>>>Press Enter to load data in the file.
Enter the name:
>>>>Press Enter to serach by rolI_no.
>>>>Press any key to Quit!
Enter the roll_no. you want to search:28
Name found of the roll_no. 28 is Abhay
Page _no.-09
PROGRAM CODE 05:
#Create a binary file with roll_no, name and marks. Input a roll_no and up-
date the marks.
from pickle import load as l, dump as d

with open("stud.dat","ab") as fhand:


while True:
name=input("Enter the name:")
if len(name)<1: break
roll=int(input("Enter the Roll no.:"))
marks=int(input("Enter the marks:"))
record=[name,roll,marks]
d(record,fhand)
print(">>>>Press Enter to load data in the file.")
search=int(input("Enter the roll_no to update marks:"))
tmp_data=()
with open("stud.dat",'rb') as fhand:
while True:
try:
tmp_data+=(l(fhand),)
except: break
with open("stud.dat","wb") as fhand:
rec=None
for record in tmp_data:
if search==record[1]:
new_marks=int(input("Enter the new marks:"))
record[2]=new_marks
d(record,fhand) Page _no.-10
rec=record
break
else:
d(record,fhand)
print("Marks updated successfuly....",'\n','updated_record:',rec)

RUN AND OUTPUT:


G:\Abha ki practice\School\class 12>practical_5.py
Enter the name:Abhay
Enter the Roll no.:28
Enter the marks:70
>>>>Press Enter to load data in the file.
Enter the name:Hunny Mam
Enter the Roll no.:1
Enter the marks:99
>>>>Press Enter to load data in the file.
Enter the name:
Enter the roll_no to update marks:1
Enter the new marks:100
Marks updated successfuly....
updated_record: ['Hunny Mam', 1, 100]
Page _no.-11
PROGRAM CODE 06:
#Write a random number generator that generate random numbers be-
tween 1 and 6 (simulates a dice)
import random as r
while True:
print('''>>>>Press any key to get random number between 1 and 6.
>>>>Press enter to quit.''')
iterator=input(">>>>")
if len(iterator)<1: break
print("Random number generated is:",r.randint(1,6))
print("Quited the prgram....You pressed enter key....")

RUN AND OUTPUT:


G:\Abha ki practice\School\class 12>practical_6.py
>>>>Press any key to get random number between 1 and 6.
>>>>Press enter to quit.
>>>>ad
Random number generated is: 6
>>>>Press any key to get random number between 1 and 6.
>>>>Press enter to quit.
>>>>s
Random number generated is: 4
>>>>Press any key to get random number between 1 and 6.
>>>>Press enter to quit.
>>>>df
Random number generated is: 2
>>>>Press any key to get random number between 1 and 6.
>>>>Press enter to quit.
>>>>
Page _no.-12
PROGRAM CODE 07:
#Write a python program to implement if Input=='1':push()
#a stack using list. if Input=='2':s_pop()
stack=list() if Input=='3':display()
def push(): if (Input not in ('1','2','3') or len(Input)<1):break
ele=input("Enter the element:") print()
stack.append(ele)
def s_pop():
if len(stack)<1:
print("Stack is underflow!!!!")
else:
tmp=stack.pop()
print("Deleted element is:",tmp)
def display():
if len(stack)<1:
print("Stack is underflow!!!!")
else:
print("Element of your stack are:")
for i in range(len(stack),0,-1):
print(stack[i-1])
print("----Creating_Stack----")
while True:
Input=input('''
>>>>Enter 1 to push.
>>>>Enter 2 to POP.
>>>>Enter 3 to display my stack.
>>>>Press any other key to Quit!
>>>>''')
RUN AND OUTPUT: >>>>Enter 1 to push.
>>>>Enter 2 to POP.
Page _no.-13

G:\Abha ki practice\School\class 12>practical_7.py >>>>Enter 3 to display my stack.


----Creating_Stack---- >>>>Press any other key to Quit!
>>>>Enter 1 to push. >>>>3
>>>>Enter 2 to POP. Element of your stack are:
>>>>Enter 3 to display my stack. Abhay
>>>>Press any other key to Quit!
>>>>1 >>>>Enter 1 to push.
Enter the element:Abhay >>>>Enter 2 to POP.
>>>>Enter 3 to display my stack.
>>>>Enter 1 to push. >>>>Press any other key to Quit!
>>>>Enter 2 to POP. >>>>2
>>>>Enter 3 to display my stack. Deleted element is: Abhay
>>>>Press any other key to Quit!
>>>>1 >>>>Enter 1 to push.
Enter the element:Hunny Mam >>>>Enter 2 to POP.
>>>>Enter 3 to display my stack.
>>>>Enter 1 to push. >>>>Press any other key to Quit!
>>>>Enter 2 to POP. >>>>3
>>>>Enter 3 to display my stack. Stack is underflow!!!!
>>>>Press any other key to Quit!
>>>>3 >>>>Enter 1 to push.
Element of your stack are: >>>>Enter 2 to POP.
Hunny Mam >>>>Enter 3 to display my stack.
Abhay >>>>Press any other key to Quit!
>>>>2
>>>>Enter 1 to push. Stack is underflow!!!!
>>>>Enter 2 to POP. >>>>Enter 1 to push.
>>>>Enter 3 to display my stack. >>>>Enter 2 to POP.
>>>>Press any other key to Quit! >>>>Enter 3 to display my stack.
>>>>2 >>>>Press any other key to Quit!
Deleted element is: Hunny Mam >>>>
Page _no.-14

PROGRAM CODE 08: RUN AND OUTPUT:


#create a CSV file by entering user-id and G:\Abha ki practice\School\class
#password read and search the password for 12>practical_8.py
#given user-id. Enter the user-id:Abhay01

from csv import reader as r, writer as w Enter the password:#Abhi007


>>>>Press Enter to save file:
with open('cred.csv','w',newline='') as fhand: Enter the user-id:Ak
csv_writer=w(fhand) Enter the password:12Ka4
csv_writer.writerow(('user_id','password')) >>>>Press Enter to save file:
while True: Enter the user-id:
user_id=input("Enter the user-id:") ----CSV File created successfuly----
if len(user_id)<1: Enter a valid user_id:A
print("----CSV File created successfuly----") !!!!You Entered a invalid user_id!!!!
break Enter a valid user_id:Ak
password=input("Enter the password:") password: 12Ka4
print(">>>>Press Enter to save file:")
csv_writer.writerow((user_id,password))

while True:
id=input("Enter a valid user_id:")
with open('cred.csv','r') as fhand:
csv_reader=r(fhand)
for record in csv_reader:
if id==record[0]:
print("password:",record[1])
quit()
else:continue
print("!!!!You Entered a invalid user_id!!!!")
Page _no.-15
MySQL QUERIES;
#Creating table.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.31 MySQL Community Server - GPL

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its


affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database School;


Query OK, 1 row affected (0.05 sec)

mysql> create table student(std_id INTEGER AUTO_INCREMENT PRIMARY KEY,


-> name VARCHAR(28) NOT NULL,
-> class INTEGER(2),
-> stream VARCHAR(21),
-> marks INTEGER(4));
Query OK, 0 rows affected, 2 warnings (0.29 sec)

mysql> desc student;

5 rows in set (0.04 sec)

#Alter commands.
mysql> ALTER TABLE student ADD (remark varchar(14));
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0 Page _no.-16
mysql> ALTER TABLE student MODIFY stream varchar(28);
Query OK, 0 rows affected (0.13 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc student;

6 rows in set (0.01 sec)

mysql> ALTER TABLE student DROP remark;


Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc student;

5 rows in set (0.00 sec)

#update commands.
mysql> UPDATE student SET marks=90 WHERE std_id=2;
Query OK, 1 row affected (0.02 sec) Page _no.-17
Rows matched: 1 Changed: 1 Warnings: 0

mysql> SELECT * FROM student;

4 rows in set (0.01 sec)


#Order by Ascending order and descending order commands, respectively.
mysql> SELECT * FROM student ORDER BY name;

4 rows in set (0.17 sec)

mysql> SELECT * FROM student ORDER BY name desc;

4 rows in set (0.00 sec)


#Delete and group by commands. Page _no.-18
mysql> DELETE FROM student WHERE std_id=2;
Query OK, 1 row affected (0.07 sec)

mysql> SELECT * FROM student;

3 rows in set (0.01 sec)

mysql> SELECT min(marks),max(marks),sum(marks),count(marks),avg(marks) FROM student GROUP


BY stream;

2 rows in set (0.19 sec)

You might also like