Class 12
Class 12
ACADEMIC YEAR:2024-2025
NAME: hanshika MALIK
CLASS: XIi (science)
ROLL NO: 02
SUBJECT: computer science
GUIDED BY: MS suneeta mam
Acknowledgement
I would like to express my special thanks
of gratitude to my teacher Ms Suneeta
Mam as well as our principal Dr.
Pratiksha Dixit who gave me the golden
opportunity to do this wonderful project
on the topic Python which also helped
me in doing a lot of Research and I came
to know about so many new things I am
really thankful to them.
Secondly I would also like to thank my
parents and friends who helped me a lot
in finalizing this project within the
limited time frame.
Hanshika Malik
Bibiligoraphy
For successfully completing my project I
have taken help from the following
resources
Computer science with python
www.google.com
1.Write a program to enter two numbers and print
the arithmetic operations like +, -, *, /, // and %.
Source code
x=float(input("Enter the first number :"))
y=float(input("Enter the second number :"))
print("Sum of two number is :",x+y)
print("Difference of two number is :",x-y)
print("Multiplication of two number is :",x*y)
print("Division of two number is :",x/y)
print("Floor Division of two number is :",x//y)
print("Modulus of two number is :",x%y)
OUTPUT
2.Write a program to check the given number is
Armstrong or not.
Source code
x=int(input("Enter a number :"))
sum=0
y=x
while y>0 :
digit= y%10
sum += digit**3
y //= 10
if x==sum:
print(x,"is an Armstrong number.")
else:
print(x,"is not an Armstrong number:")
OUTPUT
3.Write a program find factorial of the entered
number.
Source code
x=int(input("Enter a number :"))
fact=1
if x < 0:
print("Error! Factorial of a negative number
doesn't exist.")
elif x==0 :
print(" Factorial of 0 is 1.")
else:
for i in range(1,x+1):
fact=fact*i
print("Factorial of a",x,"is",fact);
OUTPUT
4.Write a program to enter number of terms and to
print Fibonacci series.
Source code
terms = int(input("How many terms? "))
n1, n2 = 0,1
count = 0
if terms <= 0:
print("Please enter a positive integer")
elif terms == 1:
print("Fibonacci sequence upto",terms,":")
print(n1)
else:
print("Fibonacci sequence:")
while count < terms:
print(n1)
nth = n1 + n2
n1 = n2
n2 = nth
count += 1
OUTPUT
5.Write a program that generates random number
between 1 to 6(simulates a dice).
Source code
import random
n=int(input("how many dice throws?"))
for i in range(1,n+1):
print("throw",i,":",random.randint(1,6))
OUTPUT
6.Write a function EOReplace() in Python,
which accepts a list L of numbers. Thereafter,
it increments all even numbers by 1 and
decrements all odd numbers by 1.
Source code
def EOReplace():
l=eval(input("Enter a list :"))
print("original list",l)
for a in range(len(l)):
if l[a]%2==0:
l[a]+=1
elif l[a]%2!=0:
l[a]-=1
print("modified list :",l)
EOReplace()
OUTPUT
7. Write a function count Dwords ( ) in Python
to count the words ending with a digit in a text
file ” Details . txt”. For Example: If the file
content is as follows: On seat2 VIP 1 will sit
and On seat1 VVIP2 will be sitting Output will
be: Number of words ending with a digit are 4
Source code
def Dcount():
f=open("Details.txt","r")
lines=f.readlines()
word=""
count=0
for line in lines:
l=line.split(' ')
for word in l:
if (word[-1]>='0' and word[-1]<='9'):
count +=1
f.close()
print("No of words ending with digit a
is :",count)
Dcount()
OUTPUT
8. Remove all the lines that contain the
character “a” in a file and write it into another
file.
Source code
f=open(story.txt')
data=f.readlines()
for i in data :
print(i)
f1=open(story.txt','w')
f2=open(story1.txt','w')
for i in data:
if 'a' in i:
f2.write(i)
else:
f1.write(i)
f1.close()
f2.close()
OUTPUT
9. Read a text file and display the number of
vowels/consonants/uppercase/lowercase
characters in the file
Source code
def countVCUL():
f=open("Report.txt","r")
uppercount=lowercount=vowelcount=concoun
t=0
data=f.read()
for i in data:
if i.isupper():
uppercount+=1
if i.islower():
lowercount+=1
if i in 'aieouAEIOU':
vowelcount+=1
if i in
'BCDFGHJKLMNPQRSTVWXYZbcdfghjklmn
pqrstvwxyz':
concount+=1
print("No of vowels are :",vowelcount)
print("No of consonants are :",concount)
print("No of lowercase :",lowercount)
print("No of uppercase :",uppercount)
countVCUL()
OUTPUT
10.Create a binary file with name and roll no.
Search for a given roll number and display the
name, if not found display appropriate
message.
Source code
import pickle
def write():
f=open("StudentDetails.dat","wb")
while True:
r=int(input("Enter Roll no :"))
n=input("Enter Name :")
Data=[r,n]
pickle.dump(Data,f)
ch=input("More? (Y/N)")
if ch in 'Nn':
break
f.close()
def Search():
found=0
rollno=int(input("Enter roll no whose name
you want to display :"))
f=open("StudentDetails.dat","rb")
try:
while True:
rec=pickle.load(f)
if rec[0]==rollno:
print(rec[1])
found=1
break
except EOFError:
f.close()
if found==0:
print("Sorry no record found. ")
write()
Search()
OUTPUT
11.Create a CSV file by entering user-id and
password, read the password for given user-
id.
Source code
import csv
def write():
f=open("Details.csv","w")
wo=csv.writer(f)
wo.writerow(["UserID","Password"])
while True:
u_id=input("Enter UserID :")
pswd=input("Enter password :")
data=[u_id,pswd]
wo.writerow(data)
ch=input("Do you want to add more
records? (Y/N): ")
if ch in 'Nn':
break
f.close()
def read():
f=open("Details.csv","r")
ro=csv.reader(f)
for i in ro:
print(i)
f.close()
write()
read()
OUTPUT
12. Read a text file line by line and display
each word separated by a #.
Source code
def seprate():
f=open("text.txt","r")
lines=f.readlines()
for line in lines:
words=line.split()
for word in words:
print(word+'#',end='')
print()
seprate()
OUTPUT
13.Create a binary file with roll number, name,
and marks. Input a roll number and update the
marks.
Source code
import pickle
f=open('student.dat','wb')
rec=[]
while True:
rn=int(input("Enter roll no :"))
name=input("Enter name :")
marks=int(input("Enter marks :"))
l=[rn,name,marks]
rec.append(l)
ch=input("Want to enter more? ")
if ch in 'Nn':
break
pickle.dump(rec,f)
print("The student details are",rec)
f.close()
f=open('student.dat','rb')
found=0
try:
while True:
rec=pickle.load(f)
srn=int(input("Enter rollno, whose marks
you want to update :"))
for i in rec:
if i[0]==srn:
smks=int(input("enter new marks:"))
i[2]=smks
print("The updated record :",i)
found=1
break
except:
f.close()
if found==0:
print("No such record")
OUTPUT
14. Write a python program to implement a
stack using a list data structure.
Source code
def Push(stk,item):
stk.append(item)
print(stk)
def Pop(stk):
if stk==[]:
print("Underflow")
else:
print("The deleted element is",stk.pop())
def Peek(stk):
if stk==[]:
print("Underflow")
else:
print("The topmost element
is",stk[len(stk)-1])
def Display(stk):
if stk==[]:
print("Underflow")
else:
for i in range(len(stk)-1,-1,-1):
print(stk[i])
Stack=[]
while True:
print("stack operations")
print("1.Push")
print("2.Pop")
print("3.Peek")
print("4.Display")
print("5.Exit")
ch=int(input("Enter the choice b/w (1..5) :"))
if ch==1:
item=int(input("Enter the item :"))
Push(Stack,item)
elif ch==2:
Pop(Stack)
elif ch==3:
Peek(Stack)
elif ch==4:
Display(Stack)
elif ch==5:
break
else:
print("Invalid choice")
OUTPUT
15.A list contains the following record of
customer: [Customer_name, Room Type]
Write the following user-defined functions to
perform given operations on the stack named
‘Hotel’:
i) Push_Cust ()– To Push customers names
of those customers who are staying in Delux’
Room Type.
ii) Pop_Cust ()- To Pop the names of
customers from the stack and display them.
Also, display “Underflow” when there are no
customers in the stack.
For example:
If the lists with customer details are as follows:
[“siddarth”, “Delux”]
[“Rahul”, “Standard”]
[“Jerry”, “Delux”]
The stack should contain:
Jerry
Siddharth
The output should be:
Jerry
Siddharth
Underflow
Source code
Hotel=[]
customer=[["Siddhart","Delux"],
["Rahul","Standard"],["Jerry","Delux"]]
def Push_Cust():
for i in customer:
if i[1]=="Delux":
Hotel.append(i[0])
def Pop_Cust():
while len(Hotel)>0:
print(Hotel.pop())
else:
print("Underflow")
Push_Cust()
print(Hotel)
Pop_Cust()
OUTPUT