0% found this document useful (0 votes)
13 views22 pages

Anurag 12

Uploaded by

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

Anurag 12

Uploaded by

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

Computer Science Practical File

Code No. 083


CLASS XII
Session: 2024-25

Submitted By: Submitted To:


Name: Anurag Ms. Madhulika pandey
Class&Sec: XII A Computer Science Teacher
Student Id: 20200176965
CBSE Roll No:
Index
S.No List Of Practicals Page No.
1 Read a text file line by line and display each word separated by a #.

2 Read a text file and display the number of


vowels/consonants/uppercase/lowercase characters in the file.
3 Remove all the lines that contain the character 'a' in a file and write it to
another file
4 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.
5 Create a binary file with roll number, name and marks. Input a roll
number and update the marks.
6 Write a random number generator that generates random numbers
between 1 and 6 (simulates a dice).
7 Write a Python program to implement a stack using list

8 Create a CSV file by entering user-id and password, read and search the
password for given userid.
9 Create a student table and insert data. Implement the following SQL
commands on the student table: ALTER table to add new attributes /
modify data type / drop attribute
10 UPDATE table to modify data

11 ORDER By to display data in ascending / descending order

12 DELETE to remove tuple(s)

13 GROUP BY and find the min, max, sum, count and average

14 Create table student inside database “School” using python

15 Insert multiple records in a table using python interface

16 To display all records of table using python interface

17 Search a record using string template with %s formatting

18 Search a record using string template with {} and format fiunction

19 Delete record from table for roll no. using python interface

20 Write a code to connect to a Mysql database namely School and then


fetch all those records from table Student where grade is ‘A’.
PROGRAM 1
AIM: Read a text file line by line and display each word separated by a
#.
SOURCE CODE:
f=open("text2.txt",'r')

for i in f:

words=i.split()

new_line="A#".join(words)

print(new_line)

OUTPUT:

PROGRAM 2
AIM: ● Read a text file and display the number of
vowels/consonants/uppercase/lowercase characters in the file.

SOURCE CODE:
def count_character(text):
vowels = “aeiouAEIOU”
vowels_count = sum(1 for char in text if char in vowels)
consonants_count = sum(1 for char in text if char.isalpha() and char not in vowels)
print(f”vowels: {vowels_count},Constants:{consonants_count}”)

count_characters(“hello world”)
print(“Anurag”)

OUTPUT:

PROGRAM 3
AIM: Remove all the lines that contain the character 'a' in a file and
write it to another file
SOURCE CODE:
With open (‘Original.txt’) as f,open(‘Duplicate.txt’,’w’) as file:
File.writelines(line for line in f if ‘a’ not in line)

OUTPUT:
PROGRAM 4
AIM: 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
SOURCE CODE:
Import pickle
With open(‘students.dat’,’wb’) as f:
Pickle.dump({18:’anurag’,19:’anshik’},f)

Def search(roll):
Try:
With open(‘students.dat’,’rb’) as f:
Except FileNotFounderror:
Print(‘File Not Found.’)

Search(18)

OUTPUT:

PROGRAM 5
AIM: Create a binary file with roll number, name and marks. Input a
roll number and update the marks.
SOURCE CODE:
import pickle

record =[]

while True:

roll_no =int(input("Enter the Roll no."))

name = input("Enter the name ")

marks = input("Enter the marks ")

data=[roll_no,name,marks]

record.append(data)

choice=input("want to add more (Y/N)")

if choice.upper()=="N":

break

f=open("student",'wb')

pickle.dump(record,f)

print("Record is added")

f.close()

OUTPUT:

PROGRAM 6
AIM: Write a random number generator that generates random
numbers between 1 and 6 (simulates a dice).
SOURCE CODE:
Import random
def roll_dice():
return random.radiant(1,6)

result = roll_dice()
print(“you rolled a:”, result)

OUTPUT:

PROGRAM 7
AIM: Write a Python program to implement a stack using list.

SOURCE CODE:
Stack = []

def push (item):


stack.append(item)

def pop():
return stack.pop() if stack else “empty”

push(1)
push(2)
push(1234)
print(pop())

OUTPUT:

PROGRAM 8
AIM: Create a CSV file by entering user-id and password, read and
search the password for given userid.
SOURCE CODE:
import csv

header=["User id","password"]

row=[ ["E_001","Ravi@123"], ["E_002","Karan@123"],

["E_003", "Vishal@123"], ["E_004","sharma@123"] ]

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

write=csv.writer(f,delimiter=',')

write.writerow(header)

write.writerows(row)

f.close()

f=open("Employee.csv","r")

csv_reader=csv.reader(f)

n=input("Enter the user_id to be searched:")

for row in csv_reader:

if row[0]==n:

print("Password is : ",row[1])

f.close()

OUTPUT:

PROGRAM 9
AIM: Create a student table and insert data. Implement the following
SQL commands on the student table: ALTER table to add new
attributes / modify data type / drop attribute
SOURCE CODE:

OUTPUT:

PROGRAM 10
AIM: Create a student table and insert data. Implement the following
SQL commands on the student table: UPDATE table to modify data
SOURCE CODE:

OUTPUT:

PROGRAM 11
AIM: Create a student table and insert data. Implement the following
SQL commands on the student table:ORDER By to display data in
ascending / descending order

SOURCE CODE:

OUTPUT:

PROGRAM 12
AIM: Create a student table and insert data. Implement the following
SQL commands on the student table: DELETE to remove tuple(s)
SOURCE CODE:

OUTPUT:

PROGRAM 13
AIM: Create a student table and insert data. Implement the following
SQL commands on the student table: GROUP BY and find the min,
max, sum, count and average

SOURCE CODE:

OUTPUT:

PROGRAM 14
AIM: Create table student inside database “School” using python
SOURCE CODE:
import mysql.connector

db=mysql.connector.connect(host="localhost",user="root",password="anurag@123" )

cur=db.cursor()

q='create database school;'

q1="use school;"

q2='''create table students( name varchar(50), std_id int PRIMARY KEY, class char(5), age int ); '''
cur.execute(q)

cur.execute(q1)

cur.execute(q2)

db.commit()

OUTPUT:

PROGRAM 15
AIM: Insert multiple records in a table using python interface
SOURCE CODE:
import mysql.connector
db=mysql.connector.connect(host="localhost",user="root",password="anurag@123" )

cur=db.cursor()

='use school;'

q="insert into students value ('karam',001,'12A',17);"

q1="INSERT INTO students value ('kanika',002,'12A',16);"

q2="insert into students value ('naina',003,'12A',17);"

q3="insert into students value ('radhika',004,'12A',16);"

cur.execute(a)

cur.execute(q)

cur.execute(q1)

cur.execute(q2)

cur.execute(q3)

db.commit()

OUTPUT:

PROGRAM 16
AIM: To display all records of table using python interface
SOURCE CODE:
import mysql.connector

db=mysql.connector.connect(host="localhost",user="root",password="anurag@123" )

cur=db.cursor()

cur.execute("use school;")

cur.execute("select * from students;")

row = cur.fetchall()

for i in row:

print(i)

OUTPUT:
PROGRAM 17
AIM: Search a record using string template with %s formatting
SOURCE CODE:
import mysql.connector
db=mysql.connector.connect(host="localhost",user="root",password="anurag@123" )

cur=db.cursor()

cur.execute("use school;")

a='kanika'

cur.execute("select * from students WHERE name LIKE '%s'" % a)

row = cur.fetchall()

for i in row:

print(i)

OUTPUT:
PROGRAM 18
AIM: Search a record using string template with {} and format
function
SOURCE CODE:
import mysql.connector
db=mysql.connector.connect(host="localhost",user="root",password="anurag@123" )

cur=db.cursor() cur.execute("use school;")

a='naina’

cur.execute(" select * from students WHERE name LIKE '{}' ".format(a))

row = cur.fetchall()

for i in row:

print(i)

OUTPUT:
PROGRAM 19
AIM: Delete record from table for roll no. using python interface
SOURCE CODE:
import mysql.connector
db=mysql.connector.connect(host="localhost",user="root",password="anuragr@123" )

cur=db.cursor()

cur.execute("use school;")

cur.execute("delete from students where std_id=05;")

db.commit()

OUTPUT:
PROGRAM 20
AIM: Write a code to connect to a Mysql database namely School and
then fetch all those records from table Student where grade is ‘A’.
SOURCE CODE:
import mysql.connector
db=mysql.connector.connect(host="localhost",user="root",password="anurag@123")

cur=db.cursor()

cur.execute("use school;")

q="select * from students where age=16;"

cur.execute(q)

row =cur.fetchall()

for i in row :

print(i)

OUTPUT:

You might also like