0% found this document useful (0 votes)
16 views8 pages

12th Board Practical

The document contains four Python programs: one that separates words in a text file with '#' symbols, another that implements a stack using a list with push and pop operations, a third that creates a binary file to store student records and allows searching by roll number, and a fourth that collects item details and writes them to a CSV file. Each program includes user interaction for input and displays results or messages based on operations performed. The outputs demonstrate the functionality of each program with sample data.

Uploaded by

sanjeev2solve4u
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)
16 views8 pages

12th Board Practical

The document contains four Python programs: one that separates words in a text file with '#' symbols, another that implements a stack using a list with push and pop operations, a third that creates a binary file to store student records and allows searching by roll number, and a fourth that collects item details and writes them to a CSV file. Each program includes user interaction for input and displays results or messages based on operations performed. The outputs demonstrate the functionality of each program with sample data.

Uploaded by

sanjeev2solve4u
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/ 8

1.

word Separated by #
def wordseperated():
f=open("ninja.txt","r")
lines=f.readlines()
print(lines)
for line in lines:
word=line.split()
for x in word:
print(x+"#",end=' ')
wordseperated()

Output:
['Hi i am a Python Programming Language']
Hi# i# am# a# Python# Programming# Language#

Notepad:
Hi I am a Python Programming Language

2.To implement a stack using List:


stack=[]
def push():
element=input("Enter the element:")
stack.append(element)
print(stack)

def pop_element():
if not stack:
print("Stack is empty!")
else:
e=stack.pop()
print("removed element:",e)
print(stack)

while True:
print("select the operation 1.push 2.pop 3.quit")
choice=int(input("Enter your choice"))
if choice==1:
push()
elif choice==2:
pop_element()
elif choice==3:
break
else:
print("Enter the correct operation!")
Output:
select the operation 1.push 2.pop 3.quit
Enter your choice 1
Enter the element:34
['34']
select the operation 1.push 2.pop 3.quit
Enter your choice 1
Enter the element:54
['34', '54']
select the operation 1.push 2.pop 3.quit
Enter your choice2
removed element: 54
['34']
select the operation 1.push 2.pop 3.quit
Enter your choice 2
removed element: 34
[]
select the operation 1.push 2.pop 3.quit
Enter your choice 3
>>>
3. To create a binary file with name and rollno,search for a given
rollno and display the name,if not found display appropriate
message:
import pickle
stu={}
stufile=open("stu.dat","wb")
ans="y"
while ans=="y":
rno=int(input("enter roll number"))
name=input("enter name")
stu["rollno"]=rno
stu["name"]=name
pickle.dump(stu,stufile)
ans=input("want to enter more record?(y/n)...")
stufile.close()
found=False
srch=open("stu.dat","rb")
search=int(input("enter the roll number to be searched for:"))
try:
while True:
stu=pickle.load(srch)
if stu["rollno"]==search:
print(stu)
found=True
except EOFError:
if found==False:
print("no such records found in file")
else:
print("search successful")
srch.close()
Output:
enter roll number 5
enter name Manoj
want to enter more record?(y/n)...y
enter roll number 6
enter name Prathish
want to enter more record?(y/n)...y
enter roll number 7
enter name Madhav
want to enter more record?(y/n)...Geetha
enter the roll number to be searched for:6
{'rollno': 6, 'name': ' Prathish'}
search successful
>>>

4. Write a program to get item details(code,description and


price) for multiple items from the user and create a csv file by
writing all the item details in one go.

import csv
def get_item_details():
items = []
print("Enter item details. Type 'done' to finish.")
while True:
code = input("Enter item code (or type 'done' to stop): ")
if code.lower() == 'done':
break
description = input("Enter item description: ")
price = input("Enter item price: ")
try:
price = float(price)
except ValueError:
print("Invalid price. Please enter a numeric value.")
continue
items.append([code, description, price])
return items

def write_to_csv(filename, items):


with open(filename, mode='w', newline='', encoding='utf-8') as
file:
writer = csv.writer(file)
writer.writerow(["Code", "Description", "Price"])
writer.writerows(items)
print(f"Item details have been saved to '{filename}'")

def main():
filename = "items.csv"
items = get_item_details()
if items:
write_to_csv(filename, items)
else:
print("No items to write.")

if __name__ == "__main__":
main()

Output:
Enter item details. Type 'done' to finish.
Enter item code (or type 'done' to stop): 101
Enter item description: pen
Enter item price: 10
Enter item code (or type 'done' to stop): 102
Enter item description: pencil
Enter item price: 20
Enter item code (or type 'done' to stop): 103
Enter item description: paper
Enter item price: 30
Enter item code (or type 'done' to stop): done
Item details have been saved to 'items.csv'

You might also like