Cs Practical File (Final)
Cs Practical File (Final)
Cs Practical File (Final)
AIM:
To write a python program to check whether the given number is perfect or not.
SOLUTION:
OUTPUT:
PROGRAM 2:
AIM :
SOLUTION:
# An Armstrong number has sum of the cubes of the digits is equal to the number itself
no=int(input("Enter any number to check: "))
no1=no
sum=0
while(no>0):
ans=no%10
sum=sum+(ans*ans*ans)
no=int(no/10)
if sum==no1:
print("Armstrong number!")
else:
print("Not an Armstrong number")
OUTPUT:
PROGRAM 3:
SOLUTION:
OUTPUT:
PROGRAM 4:
Write a program to enter the number of terms and to print the Fibonacci Series.
AIM :
To write a python program to print the Fibonacci series for the given number of
terms.
SOLUTION:
OUTPUT:
PROGRAM 5:
Write a program to enter the string and to check if it is a palindrome or not using
loop.
AIM:
SOLUTION:
def isPanlindrome(s):
return s==s[::-1]
s=str(input("Enter the string data: "))
ans=isPanlindrome(s)
if ans:
print("Given string is a Palindrome")
else:
print("Given string is NOT a Palindrome")
OUTPUT:
PROGRAM 6:
Write a program to enter the number and print the Floyd's Triangle in
decreasing order.
AIM:
To print the Floyd’s Triangle from the given number to 1 in decreasing order.
SOLUTION:
OUTPUT:
PROGRAM 7:
Write a program to enter the numbers and perform linear search and binary
search using list.
AIM:
To python program to perform Linear search and Binary search in a given list.
SOLUTION:
arr=[]
def array_operation():
ch=1
while ch!=5:
print("Various array operations \n")
print("1 Create and enter value \n")
print("2 Print array \n")
print("3 Linear Search \n")
print("4 Binary Search \n")
print("5 Exit \n")
if ch==1:
appendarray()
if ch==2:
print_array()
if ch==3:
linear_search()
if ch==4:
binary_search()
def appendarray():
for i in range(0,10):
x=int(input("Enter Number: "))
arr.insert(i,x)
def print_array():
for i in range(0,10):
print(arr[i])
def linear_search():
x=int(input("Enter the number you want to search: "))
fl=0
for i in range(0,10):
if arr[i]==x:
fl=1
print("Number found at %d location"%(i+1))
break
else:
fl==0
print("Number not found")
def binary_search():
x=int(input("Enter the number you want to search: "))
fl=0
low=0
high=len(arr)
while low<=high:
mid=int((low+high)/2)
if arr[mid]==x:
fl=1
print("Number found at %d location"%(mid+1))
break
elif arr[mid]>x:
high=mid-1
else:
low=mid+1
if fl==0:
print("Number not found")
array_operation()
OUTPUT:
PROGRAM 8:
AIM:
SOLUTION:
OUTPUT:
PROGRAM 9:
AIM:
SOLUTION:
OUTPUT:
PROGRAM 10:
AIM:
SOLUTION:
OUTPUT:
PROGRAM 11:
AIM:
SOLUTION:
def isEmpty(stk):
if stk==[]:
return True
else:
return False
def Push(stk,item):
stk.append(item)
top=len(stk)-1
def Pop(stk):
if isEmpty(stk):
return "underflow"
else:
item=stk.pop()
if len(stk)==0:
top=None
else:
top=len(stk)-1
return item
def Peek(stk):
if isEmpty(stk):
return "underflow"
else:
top=len(stk)-1
return stk[top]
def Display(stk):
if isEmpty(stk):
print("stack empty")
else:
top=len(stk)-1
print(stk[top],"<-top")
for a in range(top-1,-1,-1):
print(stk[a])
Stack=[]
top=None
while True:
print("Stack operations")
print("1.Push")
print("2.Pop")
print("3.Peek")
print("4.Display stack")
print("5.Exit")
ch=int(input("enter your choice from (1-5):"))
if ch==1:
item=int(input("enter item:"))
Push(Stack,item)
elif ch==2:
item=Pop(Stack)
if item=="underflow":
print("Underflow!!, stack is empty")
else:
print("Popped item is:",item)
elif ch==3:
item=Peek(Stack)
if item=="underflow":
print("Underflow!!, stack is empty")
else:
print("Topmost item is:",item)
elif ch==4:
Display(Stack)
elif ch==5:
break
else:
print("Invalid choice")def isEmpty(stk):
if stk==[]:
return True
else:
return False
def Push(stk,item):
stk.append(item)
top=len(stk)-1
def Pop(stk):
if isEmpty(stk):
return "underflow"
else:
item=stk.pop()
if len(stk)==0:
top=None
else:
top=len(stk)-1
return item
def Peek(stk):
if isEmpty(stk):
return "underflow"
else:
top=len(stk)-1
return stk[top]
def Display(stk):
if isEmpty(stk):
print("stack empty")
else:
top=len(stk)-1
print(stk[top],"<-top")
for a in range(top-1,-1,-1):
print(stk[a])
Stack=[]
top=None
while True:
print("Stack operations")
print("1.Push")
print("2.Pop")
print("3.Peek")
print("4.Display stack")
print("5.Exit")
ch=int(input("enter your choice from (1-5):"))
if ch==1:
item=int(input("enter item:"))
Push(Stack,item)
elif ch==2:
item=Pop(Stack)
if item=="underflow":
print("Underflow!!, stack is empty")
else:
print("Popped item is:",item)
elif ch==3:
item=Peek(Stack)
if item=="underflow":
print("Underflow!!, stack is empty")
else:
print("Topmost item is:",item)
elif ch==4:
Display(Stack)
elif ch==5:
break
else:
print("Invalid choice")
OUTPUT:
PROGRAM 12:
AIM:
SOLUTION:
def cls():
print("\n"*100)
def isEmpty(Qu):
if Qu==[]:
return True
else:
return False
def Enqueue(Qu,item):
Qu.append(item)
if len(Qu)==1:
front=rear=0
else:
rear=len(Qu)-1
def Dequeue(Qu):
if isEmpty(Qu):
return "Underflow"
else:
item=Qu.pop(0)
if len(Qu):
front=rear=None
return item
def Peek(Qu):
if isEmpty(Qu):
return "Underflow"
else:
front=0
return Qu[front]
def Display(Qu):
if isEmpty(Qu):
print("Queue empty")
elif len(Qu)==1:
print(Qu[0],"<-front,rear")
else:
front=0
rear=len(Qu)-1
print(Qu[front],"<-front")
for a in range(1,rear):
print(Qu[a])
print(Qu[rear],"<-rear")
queue=[]
front=None
while True:
cls()
print("QUEUE OPERATIONS")
print("1.Enqueue")
print("2.Dequeue")
print("3.Peek")
print('4.Display queue')
print("5.Exit")
ch=int(input("Enter your choice:"))
if ch==1:
item=int(input("Enter the item:"))
Enqueue(queue,item)
input("Press enter to continue..")
elif ch==2:
item=Dequeue(queue)
if item=="Underflow":
print("Underflow! queue is empty")
else:
print("Dequeue-ed item is:",item)
input("Press enter to continue..")
elif ch==3:
item=Peek(queue)
if item=="Underflow":
print("Queue is empty")
else:
print("Frontmost item is:",item)
input("Press enter to continue..")
elif ch==4:
Display(queue)
input("Press enter to continue..")
elif ch==5:
break
else:
print("Invalid choice")
input("Press enter to continue..")
OUTPUT:
PROGRAM 13:
Write a program to read the data and count the number of occurrences of a given
string in the file.
AIM:
SOLUTION:
f=open("test.txt",'r')
r=f.readlines()
f.close()
chk=input("Enter String to search : ")
count=0
for word in r:
line=word.split()
for cr in line:
line2=cr
if chk==line2:
count+=1
print("The search String ", chk, "is present : ", count, "times")
OUTPUT:
PROGRAM 14:
Write a program to read data from data file in read mode and append the words
starting with letter ‘T’ in a given file in python.
AIM:
Write a python program to read the data and append the words starting with “T”
in a given file .
SOLUTION:
f=open("test.txt",'r')
read=f.readlines()
f.close()
id=[]
for ln in read:
if ln.startswith("T"):
id.append(ln)
print(id)
OUTPUT:
PROGRAM 15:
Write a program to read data from data file and show Data File Handling related
functions utility in python.
AIM:
SOLUTION:
f=open("C:/Users/Z3-Z22/Documents/test.txt",'r')
print(f.name)
f_contents=f.read()
print(f_contents)
print()
f.seek(0)
f_contents=f.readlines()
print(f_contents)
print()
f.seek(0)
f_contents=f.readline()
print(f_contents)
print()
f.seek(0)
for line in f:
print(line)
f.seek(0)
print()
f_contents=f.read(50)
print(f_contents)
print()
size_to_read=10
f_contents=f.read(size_to_read)
while len(f_contents)>0:
print(f_contents)
print(f.tell())
f_contents=f.read(size_to_read)
OUTPUT:
PROGRAM 16:
Write a program to display unique vowels present in the given word using Stack.
AIM:
To write a python program to display the unique vowels present in the given
string .
SOLUTION:
vowels =['a','e','i','o','u']
word = input("Enter the word to search for vowels :")
Stack = []
for letter in word:
if letter in vowels:
if letter not in Stack:
Stack.append(letter)
print(Stack)
print("The number of different vowels present in",word,"is",len(Stack))
OUTPUT:
PROGRAM 17:
Write a python program to write the data in a CSV file student.csv and also to
read and display the data from the file.
AIM:
To write a python program to write the data in a csv file and also read and
display the data from a csv file.
SOLUTION:
#writing data
import csv
fh=open("d:\student.csv","w")
stuwriter=csv.writer(fh)
stuwriter.writerow([1,'aman',50])
stuwriter.writerow([2,'Raman',60])
fh.close()
#reading and displaying data
fh=open("d:\student.csv","r")
stureader=csv.reader(fh)
for rec in stureader:
print(rec)
fh.close()
OUTPUT:
PROGRAM 18:
Write a python program to perform basic operations in a binary file using pickle
module.
AIM:
To write a python program to perform the basic operations in a binary file using
pickle module.
SOLUTION:
OUTPUT:
PROGRAM 19:
(i) Display the Mobile company. Mobile name & price in descending order of
their manufacturing date.
(ii) List the details of mobile whose name starts with “S”.
(iii) Display the Mobile supplier & quantity of all mobiles except “MB003‟.
(iv) To display the name of mobile company having price between 3000 &
5000.
i. Display the Trainer Name, City & Salary in descending order of their
Hiredate.
ii. To display the TNAME and CITY of Trainer who joined the Institute in
the month of December 2001.
Write python code to display employee details whose salary is more than 70,000.
AIM:
SOLUTION:
cursor=mycon.cursor()
for i in range(total):
mycon.commit()
data=cursor.fetchall()
print(row)
OUTPUT:
PROGRAM 23:
Write a python code to update the salary by thousand for employees whose
salary is less than 8,000.
AIM:
SOLUTION:
cursor=mycon.cursor()
for i in range(total):
mycon.commit()
mycon.commit()
data=cursor.fetchall()
print(row)
OUTPUT:
PROGRAM 24:
Write a python code to delete the record with the given salary.
AIM:
To write a python program to delete the record with the given salary.
SOLUTION:
cursor=mycon.cursor()
for i in range(total):
mycon.commit()
mycon.commit()
data=cursor.fetchall()
print(row)
OUTPUT:
PROGRAM 25:
Write a python code to display all the records in a database one by one.
AIM:
To write a python code to display all the records in a database one by one.
SOLUTION:
for i in range(total):
mycon.commit()
while True:
data=cursor.fetchone()
if data==None:
break
else:
print("\nNext record")
print(data)
OUTPUT: