Practical Programs - 24-25-1
Practical Programs - 24-25-1
PALINDROME
Aim: Write a program to show entered string is a palindrome or not.
str=input("enter the string")
l=len(str)
p=l-1
index=0
while(index<p):
if(str[index]==str[p]):
index=index+1
p=p-1
else:
print("string is not a palindrome")
break
else:
print("string is palindrome")
Output:
2 2. STATUS OF A STRING
Aim: WAP to counts the number of alphabets ,digits, uppercase,
lowercase, # spaces and other characters(status of a string).
str1 =input("enter a string")
n=c=d=s=u=l=o=0
for ch in str1:
if ch.isalnum():
n+=1
if ch.isupper():
u=u+1
elif ch.islower():
l=l+1
elif ch.isalpha():
c=c+1
elif ch.isdigit():
d=d+1
elif ch.isspace():
s=s+1
else:
o=o+1
print("No.of alpha and digit",n)
print("No.of capital alpha",u)
print("No.of smallalphabet",l)
print("No.of digit",d)
print("No. of spaces",s)
print("No of other character",o)
3 3. LIST MANIPULATION
Aim: WAP to remove all odd numbers from the given list.
Program 3:WAP to remove all odd numbers from the given list.
L=[2,7,12,5,10,15,23]
for i in L:
if i%2==0:
L.remove(i)
print(L)
Output:
L=[3,21,5,6,3,8,21,6]
L1=[]
L2=[]
for i in L:
if i not in L2:
x=L.count(i)
L1.append(x)
L2.append(i)
print('element','\t',"frequency")
for i in range (len(L1)):
print(L2[i],'\t\t',L1[i])
Output:
Output:
L=[33,13,92,99,3,12]
sum=0
x=len(L)
for i in range(0,x):
if type(L[i])==int:
if L[i]%10==3:
sum+=L[i]
print(sum)
Output:
8 8. TUPLE - CREATION
Aim: Write a program to accept values from a user and create a tuple.
t=tuple()
n=int(input("enter limit:"))
for i in range(n):
a=input("enter number:")
t=t+(a,)
print("output is")
print(t)
Output:
def special(n):
b=0
c=1
t=a
while a>0:
d = a%10
b +=d
c *=d
a //=10
if b+c==t:
print('Speical number')
else:
print('Not a special number')
def main():
print('Menu....')
print('1. Disarium Number')
print('2. Special Number')
ch=int(input('Press 1 or 2..'))
n=int(input('Enter number'))
if ch==1:
a=disarium(n)
if a == n:
print(str(n) + " is a disarium number");
else:
print(str(n) + " is not a disarium number");
elif ch==2:
special(n)
else:
return
Output:
f1=open("happy.txt","r")
s=f1.read()
print(s)
words=s.split()
print(words,", ",len(words))
maxC=len(words[0])
minC=len(words[0])
minfinal=""
maxfinal=""
for word in words[1:]:
length=len(word)
if maxC<length:
maxC=length
maxfinal=word
if minC>length:
minC=length
minfinal=word
print("Max word : ",maxfinal,", maxC: ",maxC)
print("Min word : ",minfinal,", minC: ",minC)
def create():
f = open("test.dat", "wb+")
for i in range(5):
tid=input('Enter testid')
sub = input('Enter the subject')
maxmarks=int(input('Ener max marks'))
score = int(input('Enter marks scored'))
pickle.dump([tid, sub, maxmarks,score],f)
f.close()
def dispavg():
f = open("test.dat", "rb")
c=0
s=0
sub=input('Enter the subject')
try:
while True:
g = pickle.load(f)
print(g)
if g[1]==sub:
s=s+g[3]
c=c+1
except:
f.close()
print('Average marks...',s/c)
Output:
21 20. READING & WRITING – CSV FILE
Aim: Write a program to insert list data in CSV File and print it.
import csv
fields = ['Name', 'Branch', 'Year', 'CGPA']
# data rows of csv file
rows = [ ['Nikhil', 'COE', '2', '9.0'],
['Sanchit', 'COE', '2', '9.1'],
['Aditya', 'IT', '2', '9.3'],
['Sagar', 'SE', '1', '9.5'],
['Prateek', 'MCE', '3', '7.8'],
['Sahil', 'EP', '2', '9.1']]
filename = "MYCSV.csv"
with open(filename, 'w',newline='') as csvfile:
csvwriter = csv.writer(csvfile)
csvwriter.writerow(fields)
csvwriter.writerows(rows)
with open('MYCSV.csv') as File:
reader = csv.reader(File)
for row in reader:
print(row)
Output:
22 A CSV file student.csv has following contents:
Rno,Name,Score,Class
101,Alex,80,12
102,Monica,84,12
103,Ajay,72,12
104,Irfan,82,11
Write a function count_rec() that count and display number of records where
score is more than 80.
Solution
import csv
def count_rec():
file = open("student.csv","r")
count = 0
creader = csv.reader(file)
next(creader)
for row in creader:
if int(row[2])>80:
count += 1
print("Number of records are",count)
file.close()
count_rec()
Output
Rno,Name,Total
101,Alex,222
102,Monica,203
103,Ajay,243
104,Irfan,271
Solution
import csv
def make_copy():
infile = open("result.csv","r")
outfile = open("copy.csv","w",newline="")
creader = csv.reader(infile)
cwriter = csv.writer(outfile)
next(creader)
cwriter.writerow(['Rno','Name','Total'])
for row in creader:
sum = int(row[2])+int(row[3])+int(row[4])
cwriter.writerow([row[0],row[1],sum])
infile.close()
outfile.close()
make_copy()
Output:
INPUT FILE:
OUTPUT FILE:
24 Write a program to create a CSV File 'Student.csv' (content shown below).
Content of CSV file is input by user.
Rollno,Name,Class
1,Sakham,XII
2,Nisha,XII
3,Irfan,XII
4,Vaani,XII
5,Jasvinder,XII
Solution
import csv
file = open('student.csv', 'w', newline='')
mywriter = csv.writer(file)
data = [ ]
header = ['Rollno', 'Name', 'Class']
data.append(header)
for i in range(5):
rollno = int(input("Enter Roll Number : "))
name = input("Enter Name : ")
Class = input("Enter Class : ")
rec = [rollno,name,Class]
data.append(rec)
mywriter.writerow(data)
file.close()
Output
def push(stack,x):
stack.append(x)
def pop(stack):
n = len(stack)
if(n<=0):
print("Stack empty....Pop not possible")
else:
stack.pop()
def display(stack):
if len(stack)<=0:
print("Stack empty...........Nothing to display")
return
for i in stack:
print(i,end=" ")
#main program starts from here
x=[]
choice=0
while (choice!=4):
print("\n********Stack Menu***********")
print("1. push(INSERT)")
print("2. pop(DELETE)")
print("3. Display ")
print("4. Exit")
choice = int(input("Enter your choice :"))
if(choice==1):
value = int(input("Enter value "))
push(x,value)
if(choice==2):
pop(x)
if(choice==3):
display(x)
if(choice==4):
print("You selected to close this program")