0% found this document useful (0 votes)
15 views16 pages

Board Wordformat

The document contains multiple sets of Python code for various functionalities, including calculating areas of geometric shapes, checking for palindromes, calculating factorials and Fibonacci series, and handling file operations. It also includes stack operations, reading and writing to binary files, and managing student records using CSV and pickle. Each set provides a menu-driven interface for user interaction.

Uploaded by

TVIS Juniors
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)
15 views16 pages

Board Wordformat

The document contains multiple sets of Python code for various functionalities, including calculating areas of geometric shapes, checking for palindromes, calculating factorials and Fibonacci series, and handling file operations. It also includes stack operations, reading and writing to binary files, and managing student records using CSV and pickle. Each set provides a menu-driven interface for user interaction.

Uploaded by

TVIS Juniors
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/ 16

SET 1

import math

def tarea(b,h):

a=0.5*b*h

return a

def carea(r):

a=math.pi*r*r

return a

def rarea(l,b):

a=l*b

return a

while 1:

print("Menu")

print("1.Area of triangle")

print("2.Area of circle")

print("3.Area of rectangle")

print("4.Exit")

ch=int(input("Enter your choice:"))

if ch==1:

b=float(input("Enter base:"))

h=float(input("Enter height:"))

print("Area of triangle is:",tarea(b,h))

elif ch==2:

r=float(input("Enter radius:"))

print("Area of cirlce is:",carea(r))

elif ch==3:

l=float(input("Enter length:"))

b=float(input("Enter breadth:"))

print("Area of rectangle is:",rarea(l,b))

elif ch==4:

break
else:

print("Wrong choice")
SET 2

def palindrome(s):

rev=s[::-1]

if rev==s:

print("The string is a palindrome")

else:

print("The string is not a palindrome")

def countc(s,c):

c=s.count(c)

return c

while 1:

print("Menu")

print("1.Palindrome")

print("2.No.of occurance")

print("3.Exit")

ch=int(input("Enter your choice:"))

if ch==1:

s=input("Enter the string:")

palindrome(s)

elif ch==2:

s=input("Enter the string:")

c=input("Enter a character:")

if len(c)==1:

print("The character",c,"is in",s,",",countc(s,c),"times")

else:

print("Enter only one character")

elif ch==3:

break

else:

print("Wrong choice")
SET 3

def fact(n):

fact=1

for i in range(1,n+1):

fact=fact*i

return fact

def fibo(n):

a=-1

b=1

for i in range(n):

c=a+b

a,b=b,c

print(c)

def sum(n):

sum=0

while n>0:
d=n%10

n=n//10

sum=sum+d

return sum

while 1:

print("Menu")

print("1.Factorial of a number")

print("2.Fibonacci series")

print("3.Sum of digits of a number")

print("4.Exit")

ch=int(input("Enter your choice:"))

if ch==1:

n=int(input("Enter the number:"))

if n>0:

print("The factorial of the given number is:",fact(n))

else:

print("The factorial of negative number is not defined")

elif ch==2:

n=int(input("Enter the number of terms:"))

print("The Fibonacci series is")

fibo(n)

elif ch==3:

n=int(input("Enter the number:"))

print("The sum of digit is:",sum(n))

elif ch==4:

break

else:

print("Invalid input")
SET 4

f=open("read.txt","r")

s=f.read()

v=0

c=0

up=0
low=0

for i in s:

if i.isupper():

up=up+1

elif i.islower():

low=low+1

if i in "aeiouAEIOU":

v=v+1

elif i in "bcdfghjklmnpqrstvwxyz" or i in "BCDFGHJKLMNPQRSTVWXYZ":

c=c+1

print("Total number of uppercase letter in the text file is:",up)

print("Total number of lowercase letter in the text file is:",low)

print("Total number of vowels in the text file is:",v)

print("Total number of consonants in the text file is:",c)

f.close()
SET 5

f=open("read.txt","r")

d=f.readlines()

for i in d:

words=i.split()

for a in words:

print(a+"#")

f.close()

infile=open("read.txt","r")

outfile=open("read_res.txt","w")

for line in infile:

if 'a' in line:

line=line.replace(".","")

else:

outfile.write(line)

print("Writing into another file is completed")

infile.close()

outfile.close()

SET 6

R={"OM":76,"JAI":45,"BOB":89,"ALI":65,"ANU":90,"TOM":82}

def push(s,n):

s.append(n)

def pop(s):

if s!=[]:

return s.pop()

else:

return None

st=[]
for k in R:

if R[k]>=75:

push(st,k)

while True:

if st!=[]:

print(pop(st),end=" ")

else:

break

SET 7

import csv

f=open("student.csv","w",newline='')

w=csv.writer(f)

w.writerow(['Rollno','Name','Marks'])

for i in range(5):

print("Student record",(i+1))

rollno=int(input("Enter rollno:"))

name=input("Enter Name:")

marks=int(input("Enter Marks:"))
sturec=[rollno,name,marks]

w.writerow(sturec)

f.close()

SET 8

s=[]

def push():

b_ID=int(input("Enter book id:"))

b_NAME=input("Enter book name:")

b_PRICE=float(input("Enter book price:"))

data=[b_ID,b_NAME,b_PRICE]

s.append(data)

print("BOOK ADDED TO STACK")

def pop():

if len(s)==0:

print("stack is empty")

else:

dn=s.pop()

print(dn)
def disp():

if len(s)==0:

print("stack is empty")

else:

for i in range(len(s)):

print("BOOK ID:",s[i][0])

print("BOOK NAME:",s[i][1])

print("BOOK PRICE:",s[i][2])

while True:

print('Menu')

print('1.Push')

print('2.Pop')

print('3.Display')

print('5.Exit')

ch=int(input('Enter your choice: '))

if ch==1:

push()

elif ch==2:

pop()

elif ch==3:

disp()

elif ch==4:

break

else:

print("invalid choice")
SET9

import pickle

def writefile(file):

admno=int(input("Enter admission number:"))

name=input("Enter name:")

age=int(input("Enter age:"))

data=[admno,name,age]

pickle.dump(data,file)

def readfile(file):

while 1:

try:

data=pickle.load(file)

print(data)

except EOFError:

return
def search(file):

admno=int(input("Enter admission number:"))

flag=0

while 1:

try:

data=pickle.load(file)

if data[0]==admno:

print(data)

flag=1

return flag

except EOFError:

return flag

file=open("student.dat","ab+")

while 1:

print("Menu")

print("1.Write binary file")

print("2.Read binary file")

print("3.Search binary file")

print("4.Exit")

ch=int(input("Enter your choice:"))

if ch==1:

file.seek(0,0)

writefile(file)

elif ch==2:

file.seek(0,0)

readfile(file)

elif ch==3:

file.seek(0,0)

res=search(file)

if res==0:

print("Record not found")


elif ch==4:

break

else:

print("Invalid input")

file.close()

SET 10

def push(n):

stk.append(n)

top=len(stk)-1

def pop(stk):

if stk==[]:

print("underflow")

else:

print("The element popped out is",stk.pop())

def peek(stk):

if stk==[]:
print("underflow")

else:

top=len(stk)-1

print("Topmost element of the stack is:",stk[top])

def display(stk):

if stk==[]:

print("underflow")

else:

top=len(stk)-1

print(stk[top])

for i in range(top-1,-1,-1):

print(stk[i])

stk=[]

top=None

while True:

print('\t\t\tSTACK OPERATIONS - 1')

print('\t\t\t....................')

print()

print('1.Push')

print('2.Pop')

print('3.Display Stack')

print('4.Peek')

print('5.Exit')

print()

ch=int(input('Enter your choice: '))

if ch==1:

n=int(input('Enter number to be pushed inside stack: '))

push(n)

elif ch==2:

pop(stk)
elif ch==3:

display(stk)

elif ch==4:

peek(stk)

else:

break

You might also like