12th Board Practical
12th Board Practical
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
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
>>>
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 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'