Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

PRACTICAL FILE

Computer Science

ACADEMIC YEAR: 2023-2024

Name : Pankaj mehra


Class : 12th - A
Roll No. : 4
School : Dhanpatmal Virmani Sr.
Sec. School
Table of Contents
Serial No. Python Programming Page No. Remark

1 Program to read and display file content line by line with each 1
word separated by "#"

2 Program to read the content of file and display the total number 2
of consonants, uppercase, vowels and lowercase characters

3 Program to read the content of file line by line and write it to 3


another file except for the lines contains "a" letter in it

4 Program to create binary file to store Rollno and Name, Search 5


any Rollno and display name if Rollno found otherwise “Rollno
not found”

5 Program to create binary file to store Rollno,Name and Marks and 8


update marks of entered Rollno

6 Program to create CSV file and store empno,name,salary and 10


search any empno and display name,salary and if not found
appropriate message

7 Program to generate random number 1-6, simulating a dice 11

8 Program to implement Stack in Python using List 16


Python Programming

Program to read and display file content line by line with each word separated by "#"

f = open("file1.txt")
for line in f:
words = line.split()
for w in words:
print(w+'#',end='')
print()
f.close()

Note:- If the original content of file is:


India is my country
I love python
Python learning is fun

Output :-
India#is#my#country#
I#love#python#
Python#learning#is#fun#
India is my country
I love python
Python learning is fun
1
Python Programming

Program to read the content of file and display the total number of consonants,
uppercase, vowels and lowercase characters

f = open(“file1.txt” , “r” )
vowels = “aeiou”
v=c=u=l=0
data = f.read()
for ch in data:
if ch.isalpha():
if ch.lower() in vowels:
v += 1
else:
c += 1
if ch.isupper(): Note:- If the original content of file is:
u += 1 India is my country
elif ch.islower(): I love python
l += 1 Python learning is fun
f.close()
print(“Total Vowels in file: “ , v)
print(“Total Consonants in file: “ , c)
Output:-
print(“Total Capital letters in file: “ , u)
Total Vowels in file: 16
print(“Total Small letters in file: “ , l)
Total Consonants in file: 30
Total Capital letters in file: 3
Total Small letters in file: 43

Python#learning#is#fun#
2
India is my country
I love python
Python Programming

Program to read the content of file line by line and write it to another file except for the
lines contains “a” letter in it

f1 = open(“file2.txt”) Note:- Content of file2.txt


f2 = open(“file2copy.txt” , “w”) A quik brown fox

for line in f1: One two three four


Five six seven
if ‘a’ not in line:
India is my country
f2.write(line)
eight nine ten
print(“## File Copied Successfully! bye!
##”)
f1.close()
f2.close()

Output:
## File Copied Successfully! ##

Note:- After copy; Content of file2copy.txt


one two three four
five six seven
eight nine ten
bye!

3
Python Programming

Program to create binary file to store Rollno and Name, Search any Rollno and display
name if Rollno found otherwise “Rollno not found”

import pickle
students = []
with open('student.dat', 'wb') as f:
ans = 'y'
while ans.lower() == 'y':
roll = int(input("Enter Roll Number: "))
name = input("Enter Name: ")
students.append([roll, name])
ans = input("Add More? (Y/N): ")
pickle.dump(students, f)
f.close()
with open('student.dat', 'rb') as f:
students = []
while True:
try:
students = pickle.load(f)
except EOFError:
break
ans = 'y'
while ans.lower() == 'y':
found = False
r = int(input("Enter Roll number to search: "))
for s in students:
if s[0] == r:
print(“## Name is: “ , s[1], “##”)

4
Python Programming

found = True
break
if not found:
print(“#### Sorry! Roll Number Not Found ####”)
ans = input(“Search more? (Y/N): “)
f.close()

Output:
Enter Roll Number: 1
Enter Name: Akashi
Add More? (Y/N): y
Enter Roll Number: 2
Enter Name: Raj
Add More? (Y/N): n
Enter Roll number to search: 2
## Name is: Akashi ##
Search more? (Y/N): y
Enter Roll number to search: 4
#### Sorry! Roll Number Not Found ####
Search more? (Y/N): n

5
Python Programming

Program to create binary file to store Rollno, Name and Marks and update marks of
entered Rollno

import pickle

students = []

with open('student.dat', 'wb') as f:

ans = 'y'

while ans.lower() == “y”:

roll = int(input(“Enter Roll Number: “))

name = input(“Enter Name: “)

marks = int(input(“Enter Marks: “))

students.append([roll,name,marks])

ans = input(“Add More? (Y/N): “)

pickle.dump(students,f)

f.close()

with open(“student.dat” , “rb+”) as f:

students = []

while True:

try:

students = pickle.load(f)

except EOFError:

break

ans = “y”

6
Python Programming

while ans.lower() == “y”:

found = False

r = int(input(“Enter Roll number to update Marks: “))

for s in students:

if s[0] == r:

print(“## Name is : “,s[1] , “ ##”)

print(“## Current Marks is : “ , s[2] , “##”)

m = int(input(“Enter new Marks : “))

s[2] = m

print(“## Record Updated ##”)

found = True

break

if not found:

print(#### Sorry! Roll Number Not Found ####”)

ans = input(“Update more ?(Y/N) : “)

f.close()

7
Python Programming

Output:
Enter Roll Number: 1
Enter Name: Akashi
Enter Marks: 99
Add More? (Y/N): y
Enter Roll Number: 2
Enter Name: Raj
Enter Marks: 90
Add More? (Y/N): y
Enter Roll Number: 3
Enter Name: Sasuke
Enter Marks: 86
Add More? (Y/N): n
Enter Roll number to update Marks:2
## Name is : Raj ##
## Current Marks is : 90
Enter new Marks : 94
## Record Updated ##
Update more ?(Y/N) : y
Enter Roll number to update Marks:5
#### Sorry! Roll Number Not Found ####
Update more ?(Y/N) : n

8
Python Programming

Program to create CSV file and store empno, name, salary and search any empno and display name,
salary and if not found appropriate message

import csv
with open(“myfile.csv”, mode= “a”) as csvfile:
mywriter = csv.writer(csvfile, delimiter = “,” )
ans = “y”
while ans.lower() == “y”:
eno = int(input(“Enter Employee Number: “))
name = input(“Enter Employee Name: “)
salary = int(input(“Enter Employee Salary: “))
mywriter.writerow([eno,name,salary])
print(“## Data Saved… ##”)
ans = input(“Add More? (Y/N): “)
ans = “y”
with open(“myfile.csv”,mode = “r”) as csvfile:
myreader = csv.reader(csvfile, delimiter = “,”)
while ans == “y”:
found = False
e = int(input(“Enter Employee Number to search: “))
for row in myreader:
if len(row)!=0:
if int(row[0]) == e:
print(“============================”)
print(“NAME :”,row[1])
print(“SALARY : row[2])
9
Python Programming

found = Ture
break
if not found:
print(“## Sorry! EMPNO NOT FOUND ##”)
ans = input(“Search More? (Y/N): “)

Output:
Enter Employee Number: 01
Enter Employee Name: kakashi
Enter Employee Salary: 90000
## Data Saved… ##
Add More? (Y/N): y
Enter Employee Number: 02
Enter Employee Name: Itachi
Enter Employee Salary: 120000
## Data Saved… ##
Add More? (Y/N): n
Enter Employee Number to search: 02
============================
NAME : Itachi
SALARY : 120000
Search More? (Y/N):y
Enter Employee Number to search: 04
## Sorry! EMPNO NOT FOUND ##
Search More? (Y/N): n

10
Python Programming

Program to generate random number 1-6, simulating a dice

import random
def roll_dice():
return random.randint(1, 6)
while True:
input("Press Enter to roll the dice...")
number = roll_dice()
print("Your Number is:", number)
play_again = input("Play More? (Y/N): ")
if play_again.lower() != 'y':
break

Output:
Press Enter to roll the dice...
Your Number is: 4
Play More? (Y/N): y
Press Enter to roll the dice...
Your Number is: 2
Play More? (Y/N): n

11
Python Programming

Program to implement Stack in Python using List

def isEmpty(S):
if len(S) == 0:
return True
else:
return False

#Push()
def Push(S, item):
S.append(item)

#Pop()
def Pop(S):
if not isEmpty(S):
return S.pop()
else:
print("Stack is empty. Cannot pop.")

#Peek()
def Peek(S):
if not isEmpty(S):
return S[-1]
else:
print("Stack is empty. Cannot peek.")

12
Python Programming

#Display()
def Display(S):
if not isEmpty(S):
for item in reversed(S):
print(item)
else:
print("Stack is empty. Nothing to display.")
# Now begin here
Mystack =[]
while True:
print("**** STACK DEMONSTRATION ******")
print("1. PUSH\n2. POP\n3. PEEK\n4. DISPLAY STACK\n0. EXIT")
ch = int(input("Enter your choice: "))
if ch == 1:
val = int(input("Enter Item to Push: "))
Push(Mystack, val)
print("Item pushed onto the stack.")
elif ch == 2:
val = Pop(Mystack)
print("\nDeleted Item was:", val)
elif ch == 3:
val = Peek(Mystack)
print("Top Item:", val)
elif ch == 4:
Display(Mystack)
elif ch == 0:
print("Exited Successfully!")
break

13
Python Programming

Output:
**** STACK DEMONSTRATION ******
1. PUSH
2. POP
3. PEEK
4. DISPLAY STACK
0. EXIT
Enter your choice: 1
Enter Item to Push: 10
Item pushed onto the stack.
**** STACK DEMONSTRATION ******
1. PUSH
2. POP
3. PEEK
4. DISPLAY STACK
0. EXIT
Enter your choice: 1
Enter Item to Push: 20
Item pushed onto the stack.
**** STACK DEMONSTRATION ******
1. PUSH
2. POP
3. PEEK
4. DISPLAY STACK
0. EXIT
Enter your choice: 1
Enter Item to Push: 30
Item pushed onto the stack.

14
Python Programming

Output:
**** STACK DEMONSTRATION ******

1. PUSH

2. POP

3. PEEK

4. DISPLAY STACK

0. EXIT

Enter your choice: 4

30

20

10

**** STACK DEMONSTRATION ******

1. PUSH

2. POP

3. PEEK

4. DISPLAY STACK

0. EXIT

Enter your choice: 3

Top Item: 30

**** STACK DEMONSTRATION ******

1. PUSH

2. POP

3. PEEK

4. DISPLAY STACK

0. EXIT

Enter your choice: 2

Deleted Item was: 30

15
Python Programming

Output:
**** STACK DEMONSTRATION ******

1. PUSH

2. POP

3. PEEK

4. DISPLAY STACK

0. EXIT

Enter your choice: 4

20

10

**** STACK DEMONSTRATION ******

1. PUSH

2. POP

3. PEEK

4. DISPLAY STACK

0. EXIT

Enter your choice: 0

Exited Successfully!

16

You might also like