Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

THE VELAMMAL INTERNATIONAL SCHOOL

XII-COMPUTER SCIENCE(083)
LIST OF RECORD PROGRAMS
Program-1
Aim:

A python program to search for a specific element using linear search

Source Code:

def LINEAR(a,key):

for i in a:

if i==key:

print(key,"found at location",a.index(key))

break

else:

print("Element not found")

a=eval(input("Enter the list of elements"))

key=eval(input("Enter the key to be searched"))

LINEAR(a,key)

Sample Output:
Program-2
Aim:

A python program to swap first half of the list with second half in a list

Source Code:

def SWAP(a):

if len(a)%2==0:

j=len(a)//2

else:

j=len(a)//2+1

i=0

while i<len(a)//2:

a[i],a[j]=a[j],a[i]

i+=1

j+=1

print("After swapping the list is",a)

a=eval(input("Enter the list of elements"))

SWAP(a)

Sample Output:

Program-3
Aim:

A python program to find the largest in a given tuple(without max() function).

Source Code:
def Largest(a):

lar=a[0]

for i in a[1:]:

if i>lar:

lar=i

print("Largest is ",lar)

a=eval(input("Enter a tuple"))

Largest(a)

Sample Output:

Program-4
Aim:

A python program to update the value in a dictionary

Source Code:

def UPDATE(d):

k=eval(input("Enter the key whose value has to be updated"))

v=eval(input("Enter a value to be updated"))

d[k]=v

print("Dictionary after updation",d)

d={}

n=int(input("Enter the number of key value pairs"))

for i in range(n):

key=eval(input("Enter the key"))

value=eval(input("Enter the value"))

d[key]=value
UPDATE( d)

Sample Output:

Program-5
Aim:

A python program to check whether the inputted number is palindrome or not

Source Code:

def PALINDROME(n):

rev=0

i=n

while i>0:

rev=rev*10+(i%10)

i=i//10

if n==rev:

print(n,"is palindrome")

else:

print(n,"is not palindrome")

n=int(input("Enter a number"))

PALINDROME(n)

Sample Output:
Program-6
Aim:

A python program to count vowels in an inputted string

Source Code:

def COUNT(a):

c=0

for i in a:

if i in'aeiouAEIOU':

c+=1

print("Total number of vowels are",c)

a=input("Enter a string")

COUNT(a)

Sample Output:

Program-7
Aim:

A python program to copy all the consonants from file1.txt to file2.txt

Source Code:

f1=open("file1.txt",'r')

f2=open("file2.txt",'w')

data=f1.read()

for i in data:

if i not in 'AEIOUaeiou':
f2.write(i)

f1.close()

f2.close()

Sample Output:

Program-8
Aim:

A python program to print all the five letter words from text file File1.txt

f=open("file1.txt",'r')

c=0

data=f.read()

data=data.split()

print("Five letter words present in the text file are")

for i in data:

if len(i)==5:

print(i)

f.close()

Sample Output:
Program-9
Aim:

A python program to display all the lines starting with ‘I’ from text file File1.txt

Source Code:

f=open("file1.txt",'r')

data=f.readlines()

for i in data:

if i[0]=='I':

print(i,end='')

f.close()

Sample Output:

Program-10
Aim:

A python program to write,read and display the content of a binary file

Source Code:

import pickle

f=open("Student.dat",'wb+')

n=int(input("Enter the number of students"))

rec=[]

for i in range(n):

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


name=input("Enter the student name :")

marks=float(input("Enter the total marks in 500 :"))

rec=[rno,name,marks]

pickle.dump(rec,f)

f.seek(0)

print("The records are",end='')

try:

while True:

rec=pickle.load(f)

print(rec)

except EOFError:

f.close()

Sample Output:

Program-11
Aim:

A python program to search and display the content of a binary file

Source Code:

import pickle
f=open("Student.dat",'wb+')

n=int(input("Enter the number of students"))

rec=[]

for i in range(n):

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

name=input("Enter the student name :")

marks=float(input("Enter the total marks in 500 :"))

rec=[rno,name,marks]

pickle.dump(rec,f)

f.seek(0)

r=int(input("Enter the rollno to be searched"))

try:

while True:

rec=pickle.load(f)

if r in rec:

print(rec)

except EOFError:

f.close()

Sample Output:
Program-12
Aim:

A python program to implement operations in a stack

Source Code:

a=[]

def PUSH():

n=int(input("Enter the number of elements to be pushed"))

for i in range(n):

ele=int(input("Enter the element"))

a.append(ele)

def POP():

if len(a)==0:

print("Underflow")

else:

print("Deleted element is",a.pop())

def DISPLAY():

if len(a)==0:

print("Underflow")

else:

print("Elements are",a[::-1])

PUSH()

POP()

DISPLAY()

Sample Output:
Program-13
Aim:

A python program to check whether an inputted number is prime or not

Source Code:

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

for i in range(2,n):

if n%i==0:

print(n,"is not prime")

break

else:

print(n,"is prime")

Sample Output:

Program-14
Aim:

A python program to generate a random number between an upper limit and lower limit

Source Code:

import random

l=int(input("Enter the lower limit"))

u=int(input("Enter the upper limit"))

print(random.randint(l,u))
Sample Output:

Program-15

Aim:

A python program to perform search and display operations in a CSV file.

Source Code:

import csv

student=[(1,'Lata',450),(2,'Anil',496),(3,'John',390)]

f=open('students.csv','a', newline='')

obj=csv.writer(f)

for stud in student:

obj.writerow(stud)

f.close()

f=open('students.csv','r', newline='')

obj=csv.reader(f)

print("The details in the files are")

for i in obj:

print(i)

f.close()

f=open('students.csv','r', newline='')

n=input("Enter the name to be searched")

obj=csv.reader(f)

for stud in obj:

if n in stud:

print("The details are")


print (stud)

break

else:

print("Data is not available")

f.close()

Sample Output:

You might also like