Professional Documents
Culture Documents
Practical File (Raj)
Practical File (Raj)
Practical File (Raj)
Computer Science
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
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()
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
Output:
## File Copied Successfully! ##
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 = []
ans = 'y'
students.append([roll,name,marks])
pickle.dump(students,f)
f.close()
students = []
while True:
try:
students = pickle.load(f)
except EOFError:
break
ans = “y”
6
Python Programming
found = False
for s in students:
if s[0] == r:
s[2] = m
found = True
break
if not found:
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
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
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
30
20
10
1. PUSH
2. POP
3. PEEK
4. DISPLAY STACK
0. EXIT
Top Item: 30
1. PUSH
2. POP
3. PEEK
4. DISPLAY STACK
0. EXIT
15
Python Programming
Output:
**** STACK DEMONSTRATION ******
1. PUSH
2. POP
3. PEEK
4. DISPLAY STACK
0. EXIT
20
10
1. PUSH
2. POP
3. PEEK
4. DISPLAY STACK
0. EXIT
Exited Successfully!
16