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

Cbse-12th-Lab Programs-Cs-Part-Ii

The document contains 7 sections describing programming tasks. The first task reads lines from a file and copies them to a new file, excluding lines containing the letter 'a'. The second task creates a binary file to store student roll numbers and names, allowing the user to search for a roll number and display the associated name. The third task similarly creates a binary file to store roll numbers, names, and marks, and allows updating a student's marks. The fourth task creates a CSV file to store employee data including number, name, and salary, and allows searching for an employee number to display the associated data. The fifth task generates random numbers between 1 and 6 to simulate rolling a dice. The remaining two tasks implement stack and queue operations
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)
28 views22 pages

Cbse-12th-Lab Programs-Cs-Part-Ii

The document contains 7 sections describing programming tasks. The first task reads lines from a file and copies them to a new file, excluding lines containing the letter 'a'. The second task creates a binary file to store student roll numbers and names, allowing the user to search for a roll number and display the associated name. The third task similarly creates a binary file to store roll numbers, names, and marks, and allows updating a student's marks. The fourth task creates a CSV file to store employee data including number, name, and salary, and allows searching for an employee number to display the associated data. The fifth task generates random numbers between 1 and 6 to simulate rolling a dice. The remaining two tasks implement stack and queue operations
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/ 22

AIM : To read the content from a file line by line and write it to another file

except for those lines contains letter “a” in it.


AIM : To create binary file to store Rollno and Name, Search any Rollno
and display name if Rollno found otherwise “Rollno not found”
AIM : To create binary file to store Rollno, Name and Marks and update the
marks of entered Rollno
AIM : To create a CSV file and store empno, name, salary and search any
empno and display name and salary if exist else return as “Not Exist”.
AIM : To generate random number between 1 and 6 and to simulate using
a dice.
AIM : To implement the Stack operations in Python using List
AIM : To implement the Queue operations in Python using List
AIM : To read the content from a file line by line and write it to another file
except for those lines contains letter “a” in it.
ALGORITHM :
Step 1: To create two text files
Step 2: One file is opened as read only mode and next one is for writing
Step 3: To read the content from the file ‘File1.txt’ as line by line
Step 4: To check the line has a character “a”, if exist, read next line
from ‘File1.txt’
Step 5: If not exist the character “a” in the line then to write into
another file “File1copy.txt’ and follow the above Step 3
up to end of file
CODING :
#Program to create a file to store Rollno and name
#Program to read line from file and write it to another line
#Except for those line which contains letter “a”
f1 = open("file1.txt")
f2 = open("file1copy.txt","w")
for line in f1:
if 'a' not in line:
f2.write(line)
print("## File Copied Successfully! ##")
f1.close()
f2.close()
File1.txt :
a quick brown fox
one two three four
five six seven
India is my country
eight nine ten

File1copy.txt :
one two three four
five six seven
eight nine ten
bye!
AIM : To create binary file to store Rollno and Name, Search any Rollno
and display name if Rollno found otherwise “Rollno not found”
ALGORITHM :
Step 1: Import the module pickle for creation of binary file
Step 2: Open a file ‘Student.dat’ as binary mode for writing
Step 3: Get Roll Number and Name of the student from the user and
write into the file.
Step 4: User can get roll number and display their name , if exist
Step 5: If roll number not exist then it will display as ‘Roll number not
exist in the file
CODING :
#Program to create a binary file to store Rollno and name
#Search for Rollno and display record if found
#otherwise "Roll no. not found"
import pickle
student=[]
#Binary file opened as Append and Write Mode
f=open('student.dat','wb')
ans='y'
while ans.lower()=='y':
roll = int(input("Enter Roll Number :"))
name = input("Enter Name :")
student.append([roll,name])
ans=input("Add More ?(Y)")
pickle.dump(student,f)
f.close()

#Binary file opened as Read Mode


f=open('student.dat','rb')
student=[]
while True:
try:
student = 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 student:
if s[0]==r:
print("## Name is :",s[1], " ##")
found=True
break
if not found:
print("####Sorry! Roll number not found ####")
ans=input("Search more ?(Y) :")
f.close()
OUTPUT :
Enter Roll Number :101
Enter Name :SUKUMAR
Add More ?(Y)Y
Enter Roll Number :102
Enter Name :ANANDA KANNAN
Add More ?(Y)Y
Enter Roll Number :103
Enter Name :JACK
Add More ?(Y)Y
Enter Roll Number :104
Enter Name :ABDUL KRISHNA
Add More ?(Y)N
Enter Roll number to search :104
## Name is : ABDUL KRISHNA ##
Search more ?(Y) :Y
Enter Roll number to search :102
## Name is : ANANDA KANNAN ##
Search more ?(Y) :N
>>>
AIM : To create binary file to store Rollno, Name and Marks and update the
marks of entered Rollno
ALGORITHM :
Step 1: Import the module pickle for creation of binary file
Step 2: Open a file ‘Student.dat’ as binary mode for writing
Step 3: Get Roll Number, Name and Mark of the student from the
user and write into the file.
Step 4: User can get roll number and mark from the user and if Roll
number is exist in the file then mark is updated
Step 5: If Roll number does not exist the file it will return as Roll
number not exist in the file
CODING :
#Search for Rollno and display record if found else as not Exist
import pickle
student=[]
f=open('student.dat','wb')
ans='y'
while ans.lower()=='y':
roll = int(input("Enter Roll Number :"))
name = input("Enter Name :")
marks = int(input("Enter Marks :"))
student.append([roll,name,marks])
ans=input("Add More ?(Y)")
pickle.dump(student,f)
f.close()

f=open('student.dat','rb+')
student=[]
while True:
try:
student = pickle.load(f)
except EOFError:
break
ans='y'
while ans.lower()=='y':
found=False
r = int(input("Enter Roll number to update :"))
for s in student:
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("## After Updating the Mark is “,s[2], ” ##")
found=True
break
if not found:
print("####Sorry! Roll number not found ####")
ans=input("Update more ?(Y) :")
f.close()
OUTPUT :
Enter Roll Number :101
Enter Name :SUKUMAR
Enter Marks :56
Add More ?(Y)Y
Enter Roll Number :102
Enter Name :ANANDA KANNAN
Enter Marks :76
Add More ?(Y)Y
Enter Roll Number :103
Enter Name :JACK
Enter Marks :86
Add More ?(Y)N
Enter Roll number to update :101
## Name is : SUKUMAR ##
## Current Marks is : 56 ##
Enter new marks :60
## After Updating the Mark is 60 ##
Update more ?(Y) :Y
Enter Roll number to update :102
## Name is : ANANDA KANNAN ##
## Current Marks is : 76 ##
Enter new marks :80
## After Updating the Mark is 80 ##
Update more ?(Y) :N
>>>
AIM : To create a CSV file and store empno, name, salary and search any
empno and display name and salary if exist else return as “Not Exist”.

ALGORITHM :
Step 1: To create a CSV file for appending purpose
Step 2: Get empno, name and salary from user
Step 3: To write empno, name and salary to the CSV file repeatedly
Step 4: To open the file as read only mode for updating
Step 5: If employee number is exist in the CSV file it will be updated
Step 6: If not exist employee number then it will display as not exist.
CODING :
#To create CSV File for storing empno, name and salary
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 ?")
ans='y'
with open('myfile.csv',mode='r') as csvfile:
while ans.lower()=='y':
myreader = csv.reader(csvfile,delimiter=',')
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])
found=True
break
if not found:
print("==========================")
print(" EMPNO NOT FOUND")
print("==========================")
csvfile.seek(0)
ans = input("Search More ? (Y)")
OUTPUT :
Enter Employee Number 1
Enter Employee Name SUKUMAR
Enter Employee Salary :10000
## Data Saved... ##
Add More ?Y
Enter Employee Number 2
Enter Employee Name ANANDA KANNAN
Enter Employee Salary :20000
## Data Saved... ##
Add More ?Y
Enter Employee Number 3
Enter Employee Name JACK
Enter Employee Salary :30000
## Data Saved... ##
Add More ?N
Enter Employee Number to search :3
============================
NAME : JACK
SALARY : 30000
Search More ? (Y)Y
Enter Employee Number to search :1
============================
NAME : SUKUMAR
SALARY : 10000
Search More ? (Y)Y
Enter Employee Number to search :2
============================
NAME : ANANDA KANNAN
SALARY : 20000
Search More ? (Y)N
>>>
Enter Employee Number 6
Enter Employee Name RAJESH
Enter Employee Salary :25000
## Data Saved... ##
Add More ?N
Enter Employee Number to search :4
==========================
EMPNO NOT FOUND
==========================
Search More ? (Y)Y
Enter Employee Number to search :2
============================
NAME : ANANDA KANNAN
SALARY : 20000
Search More ? (Y)N
>>>
AIM : To generate random number between 1 and 6 and to simulate using
a dice.
ALGORITHM :
Step 1: Import the module random for generating random number
generation
Step 2: To get option from the user to roll a dice [Y/N]
Step 3: To generate random number between 1 and 6 using randint(),if
pressed ‘Y’
Step 4: To roll the dice and generate random numbers up to press ‘N’
CODING :
# Gnenerates a random number between 1 and 6 including 1 and 6
import random
x=input("Press Y to roll the DICE and N to exit : ")
x = "Y"
while x == "Y":
no = random.randint(1,6)
if no == 1:
print("[------]")
print("[ ]")
print("[ 1 ]")
print("[ ]")
print("[------]")
if no == 2:
print("[------]")
print("[ 1 ]")
print("[ ]")
print("[ 1 ]")
print("[------]")
if no == 3:
print("[------]")
print("[ ]")
print("[1 1 1]")
print("[ ]")
print("[------]")
if no == 4:
print("[------]")
print("[1 1]")
print("[ ]")
print("[1 1]")
print("[------]")
if no == 5:
print("[------]")
print("[1 1]")
print("[ 1 ]")
print("[1 1]")
print("[------]")
if no == 6:
print("[------]")
print("[1 1 1]")
print("[ ]")
print("[1 1 1]")
print("[------]")
x=input("press Y to roll again and N to exit:")
print("\n")
OUTPUT :
Press Y to roll the DICE and N to exit : Y
[------]
[1 1 1]
[ ]
[1 1 1]
[------]
press Y to roll again and N to exit:Y

[------]
[1 1]
[ 1 ]
[1 1]
[------]
press Y to roll again and N to exit:Y

[------]
[ ]
[1 1 1]
[ ]
[------]
press Y to roll again and N to exit:Y

[------]
[1 1 1]
[ ]
[1 1 1]
[------]
press Y to roll again and N to exit:N
>>>
AIM : To implement the Stack operations in Python using List
ALGORITHM :
Step 1: To create an empty stack for storing elements
Step 2: Insert elements up to the stack is full
Step 3: If the stack is full , not possible to insert the element
Step 4: To remove an element at a time if the stack has element
Step 5: If the stack has no elements , not possible to remove any element
Step 6: While on insertion each element to be stored at top of the stack
Step 7: While on removing an element then remove the element from
top of the stack
CODING :
def isEmpty(stk): # checks whether the stack is empty or not
if stk==[]:
return True
else:
return False

def Push(stk,item): # Allow additions to the stack


stk.append(item)
top=len(stk)-1

def Pop(stk):
if isEmpty(stk): # verifies whether the stack is empty or not
print("Underflow")
else: # Allow deletions from the stack
item=stk.pop()
if len(stk)==0:
top=None
else:
top=len(stk)
print("Popped item is "+str(item))

def Display(stk):
if isEmpty(stk):
print("Stack is empty")
else:
top=len(stk)-1
print("Elements in the stack are: ")
for i in range(top,-1,-1):
print (str(stk[i]))

# executable code
if __name__ == "__main__":
stk=[]
top=None
c='Y'
while (c=='Y'):
Opt=int(input('Enter 1. PUSH 2. POP 3. DISPLAY : '))
if (Opt==1):
Element=input('Enter a value to be inserted : ')
Push(stk,Element)
elif Opt==2:
Pop(stk)
elif Opt==3:
Display(stk)
else:
print('Invalid input!')
c=input('Do you want to continue the stack operations [Y/N] : ')
print('STACK OPERATION IS OVER...')
OUTPUT :
Enter 1. PUSH 2. POP 3. DISPLAY : 1
Enter a value to be inserted : 11
Do you want to continue the stack operations [Y/N] : Y
Enter 1. PUSH 2. POP 3. DISPLAY : 1
Enter a value to be inserted : 12
Do you want to continue the stack operations [Y/N] : Y
Enter 1. PUSH 2. POP 3. DISPLAY : 1
Enter a value to be inserted : 13
Do you want to continue the stack operations [Y/N] : Y
Enter 1. PUSH 2. POP 3. DISPLAY : 3
Elements in the stack are:
13
12
11
Do you want to continue the stack operations [Y/N] : Y
Enter 1. PUSH 2. POP 3. DISPLAY : 2
Popped item is 13
Do you want to continue the stack operations [Y/N] : N
STACK OPERATION IS OVER...
>>>
AIM : To implement the Queue operations in Python using List
ALGORITHM :
Step 1: To create an empty Queue for storing elements
Step 2: Insert elements from one end of the queue
Step 3: Delete elements from another end of the queue
Step 4: If the queue has no elements , not possible to remove any element

CODING :
def isEmpty(Que): # checks whether the Queue is empty or not
if Que==[]:
return True
else:
return False

def Enqueue(Que,item): # Allow additions to the queue


Que.append(item)
top=len(Que)-1

def Dequeue(Que):
if isEmpty(Que): # verifies whether the queue is empty or not
print("Underflow")
else: # Allow deletions from the queue
item=Que.pop(0)
if len(Que)==0:
top=None
else:
top=len(Que)
print("Popped item is "+str(item))

def Display(Que):
if isEmpty(Que):
print("Queue is empty")
else:
top=len(Que)-1
print("Elements in the queue are: ")
for i in range(top,-1,-1):
print (str(Que[i]))
# executable code
if __name__ == "__main__":
Que=[]
top=None
c='Y'
while (c=='Y'):
Opt=int(input('Enter 1. ENQUEUE 2. DEQUE 3. DISPLAY : '))
if (Opt==1):
Element=input('Enter a value to be inserted : ')
Enqueue(Que,Element)
elif Opt==2:
Dequeue(Que)
elif Opt==3:
Display(Que)
else:
print('Invalid input!')
c=input('Do you want to continue the queue operations [Y/N] : ')
print('QUEUE OPERATION IS OVER...')
OUTPUT :
Enter 1. ENQUEUE 2. DEQUE 3. DISPLAY : 1
Enter a value to be inserted : 11
Do you want to continue the queue operations [Y/N] : Y
Enter 1. ENQUEUE 2. DEQUE 3. DISPLAY : 1
Enter a value to be inserted : 12
Do you want to continue the queue operations [Y/N] : Y
Enter 1. ENQUEUE 2. DEQUE 3. DISPLAY : 1
Enter a value to be inserted : 13
Do you want to continue the queue operations [Y/N] : Y
Enter 1. ENQUEUE 2. DEQUE 3. DISPLAY : 3
Elements in the queue are:
13
12
11
Do you want to continue the queue operations [Y/N] : Y
Enter 1. ENQUEUE 2. DEQUE 3. DISPLAY : 2
Popped item is 11
Do you want to continue the queue operations [Y/N] : Y
Enter 1. ENQUEUE 2. DEQUE 3. DISPLAY : 3
Elements in the queue are:
13
12
Do you want to continue the queue operations [Y/N] : N
QUEUE OPERATION IS OVER...
>>>

You might also like