PYTHON Programs
PYTHON Programs
18. Write a program to read the content of a file line by line write
it to another file except for the lines which contain the letter
‘a’ letter in it
19. Write a Program to store student’s details like admission
number, roll number, name and percentage in a dictionary
and display information on the basis of admission number.
31. write a program to get item details from the user in the
format (code, description and price) for multiple items from
the user and create a csv file by writing all the item details in
one go
CODE:-
def fibo(n):
a=0
b=1
print(a)
print(b)
c=a+b
print(c)
a=b
b=c
fibo(n)
OUTPUT:-
enter no of terms : 5
3
PYTHON PROGRAM – 2
AIM:- write a program for factorial of a given no
CODE:-
def fac(n):
f=1
f=f*i
return(f)
OUTPUT:-
enter no 3
6
PYTHON PROGRAM - 3
AIM:- write a program to find the maximum of 3 nos
CODE:-
a=int(input("enter no "))
b=int(input("enter no"))
c=int(input("enter no"))
def max():
print("max is", a)
print("max is",b)
else:
print("max is",c)
max()
OUTPUT:-
enter number 12
enter number 14
enter number 49
CODE:-
n=int(input("enter n"))
def sum(n):
s=1
for i in range(2,n+1):
s=s+i
print(s)
sum(n)
OUTPUT:-
enter n 6
21
PYTHON PROGRAM – 5
CODE:-
l=eval(input("enter a list"))
def eosum():
e=0
o=0
for i in l:
if i%2==0:
e=e+i
else:
o=o+i
return o,e
eosum()
print(eosum())
OUTPUT:-
(9, 12)
PYTHON PROGRAM - 6
AIM:- write a program to find a no is pallindrome or not
CODE:-
n=int(input("enter no"))
def pal():
global n
while n!=0:
s=0
r=n%10
s=s*10+r
n=n//10
if s==n:
print("it is pallindrome")
else:
pal()
OUTPUT:-
enter no 563
it is not pallindrome
PYTHON PROGRAM - 7
CODE:-
def sum(x,y):
z=x+y
return z
s=sum(a,b)
OUTPUT:-
enter the number 45
PYTHON PROGRAM - 8
AIM:- write a program to show the effect in output
obtained when use global keyword
CODE:-
print("before using global keyword")
def fun1(): a=10
return a
a=5
print("value of a is",a)
print("after evaluated by function value of a is",fun1())
print("value of a (outside function body after evaluation) is",a)
print("after using global keyword")
def fun2():
global a a=10
return a
a=5
print("value of a is",a)
print("after evaluated by function value of a is",fun2())
print("value of a (outside function body after evaluation) is",a)
OUTPUT:-
before using global keyword
value of a is 5
after evaluated by function value of a is 10
value of a (outside function body after evaluation) is 5 after using
global keyword
value of a is 5
after evaluated by function value of a is 10
value of a (outside function body after evaluation) is 10
PYTHON PROGRAM - 9
CODE:-
def disp(x=2,y=3):
x=x+y y+=2
OUTPUT:-
PYTHON PROGRAM - 10
AIM:- write a program To show the difference in the
output produced rather than the expected output when
we supply a mutable data type to the function
CODE:-
def check(a):
return a
b=[1,2,3,4]
c=check(b)
print("the list after modification by function is",c)
OUTPUT:-
PYTHON PROGRAM - 11
AIM:- write a program to display a list odd number
present in another list
CODE:-
length= len(k)
for i in range(length):
if k[i]%2!=0:
11.append(k[i])
print("the required list with odd numbers only from input is",l1)
list=eval(input("enter a list"))
odd(list)
OUTPUT:-
enter a list[1,2,3,4,5,12,31,45]
the required list with odd numbers only from input is [1, 3, 5, 31,
45]
enter a list[35,87,66,45,88]
the required list with odd numbers only from input is [35, 87, 45]
PYTHON PROGRAM - 12
AIM:- write a program to generate a user defined
function which returns a list of indices of non-zero
elements present in another list which is given as an
argument to function
CODE:-
if I[i]!=0:
11.append(i)
non_zero(k)
OUTPUT:-
CODE:-
K=eval(input("enter a list"))
list(K,P)
OUTPUT:-
updated list is [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
PYTHON PROGRAM - 14
AIM:- write a program to check if a character (case
sensitive) is present in a string or not and return the no
of occurrences of that character in that string
CODE:-
length= len(1)
for i in range(length):
if I[i]==p:
count += 1
if count==0:
print("not found")
k=input("enter a string")
o=input("enter a character")
search(k,o)
OUTPUT:-
enter a character N
PYTHON PROGRAM - 15
AIM:- write a program to generate a user defined
function which returns a list of indices of non-zero
elements present in another list which is given as an
argument to function
CODE:-
for i in range(length):
if I[i]!=0:
11.append(i)
non_zero(k)
OUTPUT:-
PYTHON PROGRAM - 16
AIM:- Program to read the content of file and display
the total number of consonants, upper case, vowels
and lowercase characters.
CODE:-
f = open("file1.txt")
v=0
c=0
u=0
l=0
o=0
data = f.read()
vowels=['a','e','i','o','u']
for ch in data:
if ch.isalpha():
if ch.lower() in vowels:
v+=1
else:
c+=1
if ch.isupper():
u+=1
elif ch.islower():
l+=1
elif ch!=' ' and ch!='\n':
o+=1
print("Total Vowels in file :",v)
print("Total Consonants in file :",c)
print("Total Capital letters in file :",u)
print("Total Small letters in file :",l)
print("Total Other than letters :",o)
f.close()
OUTPUT:-
Total Vowels in file : 16
Total Consonants in file : 30
Total Capital letters in file : 3
Total Small letters in file : 43
Total Other than letters : 0
PYTHON PROGRAM - 17
AIM:-program to read and display content line by line
with each word separated by ‘#’
CODE:-
f=open("file1.txt")
for line in f:
words=line.split()
for w in words:
print(w+'#',end="")
print()
f.close()
OUTPUT:-
India#is#my# country#
I#love#python#
Python#learning#is#fun#
PYTHON PROGRAM - 18
CODE:-
f1 = open("file1.txt")
f2 = open("file2copy.txt","w")
for line in f1:
if 'a' not in line:
f2.write(line)
print("## File Copied Successfully! ##")
f1.close()
f2.close()
OUTPUT:-
## File Copied Successfully! ##
>>>
PYTHON PROGRAM - 19
AIM:- Program to store student’s details like
admission number, roll number, name and
percentage in a dictionary and display
information on the basis of admission number.
CODE:-
record = dict ()
i=1
n= int (input ("How many records u want to enter: "))
while(i<=n):
Adm = input("Enter Admission number: ")
roll = input("Enter Roll Number: ")
name = input("Enter Name :")
perc = float(input("Enter Percentage : "))
t = (roll,name, perc)
record[Adm] = t
i=i+1
Nkey = record.keys()
for i in Nkey:
print("\nAdmno- ", i, " :")
OUTPUT:-
Enter Percentage : 78
Enter Percentage : 79
Admno- 101 :
34 Ganesh 78.0
Admno- 102 :
29 Ritish 79.0
PYTHON PROGRAM - 20
AIM:- Python program to count the number of vowels,
consonants, digits and special characters in a string
CODE:-
print(“Vowels: “, vowels);
print(“Consonants: “, consonants);
print(“Digits: “, digits);
print(“White spaces: “, spaces);
print(“Symbols : “, symbols);
OUTPUT:-
Vowels: 3
Consonants: 7
Digits: 5
White spaces: 3
Symbols: 3
PYTHON PROGRAM - 21
AIM:- Write a Program to add marks and calculate the
grade of a student
CODE:-
total=int(input("Enter the maximum mark of a subject: "))
sub1=int(input("Enter marks of the first subject: "))
sub2=int(input("Enter marks of the second subject: "))
sub3=int(input("Enter marks of the third subject: "))
sub4=int(input("Enter marks of the fourth subject: "))
sub5=int(input("Enter marks of the fifth subject: "))
average=(sub1+sub2+sub3+sub4+sub5)/5
print("Average is ",average)
percentage=(average/total)*100
print("According to the percentage, ")
if percentage >= 90:
print("Grade: A")
elif percentage >= 80 and percentage < 90:
print("Grade: B")
elif percentage >= 70 and percentage < 80:
print("Grade: C")
elif percentage >= 60 and percentage < 70:
print("Grade: D")
else:
print("Grade: E")
OUTPUT:-
Average is 7.6
Grade: C
PYTHON PROGRAM - 22
CODE:-
filein = open("Mydoc.txt",'r')
for w in word:
print()
filein.close()
OUTPUT:-
Text in file:
hello#how#are#you?#python#is#case-sensitive#language.#
PYTHON PROGRAM - 23
CODE:-
def count(w)
c=O
t=f.read()
s=t.split()
for i in
if
f.close()
OUTPUT:-
PYTHON PROGRAM - 24
AIM:- write a program to find the total number of words
present in a textfile
CODE:-
def count()
c=O
t=f.read()
s=t.split()
for i in
f.close()
OUTPUT:-
the number of words present in the text file is 34
PYTHON PROGRAM - 25
AIM:- write a program to write some sentences into a
text file till the user wants to write using write()
function
CODE:-
def write()
f=open (“myfile.txt",”w”)
ans= "yes"
while ans=="yes":
f.write(d)
f.close()
write()
OUTPUT:-
enter yes or no no
TEXT :-
CODE:-
import pickle
student={}
file=open("student.dat","wb")
answer="Y"
while answer=="Y":
name=input("enter name")
marks=int(input("enter marks"))
student["NAME"]=name
student["ROLL_NO"]=roll_no
student["MARK"]=marks
pickle.dump(student, file)
answer=input("enter Y or N")
file.close()
OUTPUT:-
enter marks 99
enter Y or N Y
enter marks 89
enter Y or N Y
enter roll no of students 3
enter Y or N Y
enter marks 55
enter Y or N N
RESULT :-
(ŒNAME"MOHAN"ŒROLL_NO"KŒMARK❞Kcu. €•)
}" (ŒNAME"ŒIROHAN"ŒROLL_NO"KIŒMARK"Kdu.€•)
}" (CHINAME"CHIGUGAN"
PYTHON PROGRAM - 27
AIM:- write a program to get the records from the binary
file student
CODE:-
import pickle
student={}
fin=open("student.dat","rb")
try:
print("filestu.dat stores these records")
while True:
student-pickle.load(fin)
print(student)
except EOFError:
fin.close()
OUTPUT:-
PYTHON PROGRAM - 28
AIM:- write a program to retrieve the records of those
students whose roll no are present in the list of roll
numbers specified by the user
CODE:-
import pickle
student={}
found=False
fin=open("student.dat","rb")
try:
while True:
student-pickle.load(fin)
if student["ROLL_NO"] in searchkeys:
print(student)
found=True
except EOFError:
if found == False:
else:
print("search successful.")
fin.close()
OUTPUT:-
search successful.
enter the roll no to be searched [15,7,19,27]
PYTHON PROGRAM - 29
AIM:- write a program to update the name of the student
in “student.dat”, by the name specified the user who is
having the roll number which is given by the user
CODE:-
import pickle
student={}
found=False
try:
while True:
rpos=fin.tell() student-pickle.load(fin)
if student["ROLL_NO"]==N:
student["NAME"]==m fin.seek(rpos)
pickle.dump(student,fin)
found=True
except EOFError:
if found == False:
print("not matching")
else:
print("succussfully updated.")
fin.close()
OUTPUT:-
succussfully updated.
PYTHON PROGRAM - 30
AIM:- write a program to create a csv file and add
records to it
CODE:-
import csv
file=open("CS.CSV","w")
object=csv.writer(file)
object.writerow(["ROLL NO","NAME","MARKS"])
ans="yes"
while ans=="yes":
name=input("enter name")
object.writerow(list)
file.close()
OUTPUT:
enter roll no 25
enter marks 89
enter roll no 23
enter marks 76
enter roll no 34
enter marks 99
enter roll no 21
enter yes or no no
25 RAJINI 87
23 KARTHIK 76
34 GUPTA 99
21 GUREN 100
PYTHON PROGRAM - 31
CODE:-
import csv
file=open("items.csv","w")
writer=csv.writer(file)
writer.writerow(["CODE","DESCRIPTION","PRICE"])
list=[] ans="Y"
while ans=="Y":
list.append([code,description,price])
ans=input("enter Y or N")
else:
writer.writerow(list)
file.close()
OUTPUT:-
enter Y or N Y
enter Y or N Y
enter Y or N Y
enter Y or N Y
enter Y or N N
insertion successful
PYTHON PROGRAM - 32
AIM:- STACK IMPLEMENTATION
CODE:-
def isempty(stack):
if stack==[]:
return True
else:
return False
def push(stack,item):
def pop(stack):
if isempty(stack):
return "UNDERFLOW"
else:
item=stack.pop()
if len(stack) ==0:
TOP = None
else:
TOP = len(stack)-1
return item
def peek(stack):
if ch==1:
item=int(input("enter item"))
push(stack, item)
elif ch==2:
item=pop(stack)
if item=="UNDERFLOW":
else:
elif ch==3:
item=peek(stack)
if item=="UNDERFLOW":
else:
elif ch==4:
display(stack)
elif ch==5:
break
else:
OUTPUT:-
STACK IMPLEMENTATION
MAIN MENU
enter item 1
enter Y or N Y
STACK IMPLEMENTATION
MAIN MENU
enter item 2
enter Y or N Y
STACK IMPLEMENTATION
MAIN MENU
enter item 7
enter Y or N Y
STACK IMPLEMENTATION
MAIN MENU
enter item 3
enter Y or N Y
STACK IMPLEMENTATION
MAIN MENU
enter item3 2
enter Y or N Y
STACK IMPLEMENTATION
MAIN MENU
enter item 9
enter Y or NY
STACK IMPLEMENTATION
MAIN MENU
popped item is 9
enter Y or N Y
STACK IMPLEMENTATION
MAIN MENU
popped item is 32
enter Y or N Y
STACK IMPLEMENTATION
MAIN MENU
Topmost element is 3
enter Y or N Y
STACK IMPLEMENTATION
MAIN MENU
3742
3 <----- TOP
enter Y or N Y
STACK IMPLEMENTATION
MAIN MENU
enter Y or N Y
STACK IMPLEMENTATION
MAIN MENU
Query-1
Output
Query-2
Output
Query-3
Output
Query-4
Output
Query-1
Output
Query-2
Output
Query-3
Output
PYTHON MYSQL INTERFACE-3
Code :-
Query-1
Output
Query-2
Output
Query-3
Output
Query-4
Output
SINGLE TABLE TYPE
TABLE 1
FRESH
QUERY-1 QUERY-2
OUTPUT OUTPUT
QUERY-3 QUERY-4
OUTPUT OUTPUT
TABLE 2
STUDENT
QUERY-1
OUTPUT
QUERY-2
OUTPUT
QUERY-3
OUTPUT
TABLE 3
TEACHER
QUERY-1
OUTPUT
QUERY-2
OUTPUT
→empty table
QUERY-3
OUTPUT
QUERY-4
OUTPUT
QUERY-5
OUTPUT
QUERY-6
OUTPUT
1.
FLIGHT
PASSENGER
QUERY-1
OUTPUT
QUERY-2
OUTPUT
QUERY-3
OUTPUT
QUERY-4
← DELETED RECORD
2.
DEPT
WORKER
QUERY-1
OTUPUT
QUERY-2
OUTPUT
QUERY-3
OUTPUT
QUERY-4
OUTPUT
QUERY-5
OUTPUT
QUERY-6
OUTPUT
QUERY-7
OUTPUT
3.
PRODUCT
BRAND
QUERY-1
OUTPUT
QUERY-2
OUTPUT
QUERY-3
OUTPUT
QUERY-4
OUTPUT