15 programs

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

Program 1

def count(x,y):
w=len(x)
z=len(y)
if w==z:
return"equal string"
else:
return"not equal"
a=input("enter first string")
b=input("enter second string")
c=count(a,b)
print(c)

Program 2
def count_w():
h=0
s=0
fi=open("poem.txt",'r')
a=fi.read()
w=a.split()
for i in w:
if i =="he":
h+=1
if i=="should":
s+=1
print("no of he=",h)
print("no of should=",s)
fi.close()
count_w()

Program 3
def year(x):
w=x%4
return w
y=int(input("enter years="))
a=year(y)
if a==0:
print("leap year")
else:
print("not a leap year")

Program 4

cnt=0
fi=open("poem.txt",'r')
a=fi.read()
for i in a:
if i.isspace():
cnt+=1
print("no of spaces=",cnt)
fi.close()

Program 5
n=int(input('no of students='))
winners={}
for i in range(n):
nm=input("name of students=")
wn=int(input("enter no of competition wins="))
winners[nm]=wn
print("dictionary=",winners)

Program 6

import csv
fo=open("student.csv",'w')
st=csv.writer(fo)
n=int(input("enter no of students="))
st.writerow(['Rollno','Name','Marks'])
for i in range(0,n):
rn=int(input("enter roll no="))
nm=input("enter name=")
m=int(input("enter marks="))
l=[rn,nm,m]
st.writerow(l)
fo.close()
fi=open("student.csv","r")
x=csv.reader(fi)
for i in x:
print(i)
fi.close()

Program 7

import pickle
def create_b():
d={}
fo=open("emp.dat",'wb')
ename=input("enter employee name=")
eno=int(input("enter employee number="))
s=int(input("enter salary="))
d['employee name']=ename
d['employee number']=eno
d['salary']=s
pickle.dump(d,fo)
fo.close()
fi=open("emp.dat",'rb')
x=pickle.load(fi)
print(x)
fi.close()
create_b()

Program 8

n=input("enter a line=")
l=0
u=0
a=0
d=0
length=len(n)
for i in range(0,length):
if n[i].islower():
l=l+1
elif n[i].isupper():
u=u+1
elif n[i].isalpha():
a=a+1
elif n[i].isdigit():
d=d+1
print("lower case=",l)
print("upper case=",u)
print("alphabets=",a)
print("digits=",d)

Program 9

l1=[]
l2=[]
l3=[]
n=int(input("enter length of list="))
for i in range(0,n):
x=int(input("enter an element="))
l1.append(x)
if x%2==0:
l3.append(x*x)
for j in range(0,n):
y=int(input('enter an element='))
l2.append(y)
if y%2==0:
l3.append(y*y)
print(l1)
print(l2)
print(l3)

Program 10

def push(stk,item):
stk.append(item)
top=len(stk)-1
def display(stk):
if stk==[]:
print("empty")
else:
top=len(stk)-1
print(stk[top])
for i in range(top-1,-1,-1):
print(stk[i])
s=[]
top=None
while True:
print("1.push")
print("2.display")
ch=int(input("enter your choice="))
if ch==1:
l=[]
for i in range(0,2):
rn=int(input("enter rollno="))
nm=input("enter name=")
m=int(input("enter marks="))
l=[rn,nm,m]
push(s,i)
push(s,['Rollno', 'Name','Marks'])
elif ch==2:
display(s)
else:
print("wrong choice")
break

Program 11

import pickle
d={}
fo=open("studentrec.dat", 'wb')
ch='y'
while ch=='y':
rn=int(input("enter rollno="))
nm=input("entername=")
g=input("enter grade=")
d['roll no']=rn
d['name']=nm
d['grade']=g
pickle.dump(d,fo)
ch=input("do you want to enter more record ?(y/n)")
fo.close()
fi=open("studentrec.dat", 'rb')
while fi:
x=pickle.load(fi)
print(x)
fi.close()

Program 12

def push(stk,item):
stk.append(item)
top=len(stk)-1
s=[]
d={}
for i in range(0,3):
nm=input("enter name=")
m=int(input("enter marks="))
d['name']=nm
d['marks']=m
if d['marks']>80:
push(s,nm)
print(s)

Program 13
def line():
fi=open("poem.txt",'r')
a=fi.readlines()
print(a[-1])
fi.close()
line()

Program 14

def calc(x,y):
z=(x+y)**2
return z
a=int(input("enter first number="))
b=int(input("enter second number="))
c=calc(a,b)
print(c)

Program 15

def pushelement(stk,item):
stk.append(item)
top-len(stk)-1
s=[]
l=int(input("enter limit="))
for i in range(0,l):
n=int(input("enter element="))
if n%7==0:
pushelement(s,n)

You might also like