REVISION TOUR
1.1 Python code to print number number of vowels in a
given string.
str1=input("ENTER THE STRING :")
count=0
for i in str1:
if i in "aeiou" or i in "AEIOU":
count+=1
print("NUMBER OF VOWELS IN THE STRING ARE ",count)
OUTPUT:
1.2 Python code to read a line and print number of
words that start with a vowel.
str1= input("Enter a string:")
count=0
l= [Link]()
for i in l:
if i[0] in "aeiou" or i[0] in "AEIOU":
count+=1
print("Number of words that start with a vowel are:",
count)
OUTPUT:
1.3 Python code to read a string and count number of
alphabets (uppercase and lowercase) and number of digits
in a given string.
line= input("Enter the line:")
icount= ucount= dcount= 0
for i in line:
if [Link]():
icount+=1
elif [Link]():
ucount+=1
elif [Link]():
dcount+=1
print("number of uppercase characters are:", ucount)
print("number of lowercase characters are:", icount)
print("number of digits are:", dcount)
OUTPUT:
1.4 Python code to perform linear search in the given list.
list1= [10,20,4,5,7,30]
length= len(list1)
print(list1)
element= int(input("Enter the element to be searched:"))
for i in range(0, length):
if element==list1[i]:
print(element,"is found at index",i)
break
else:
print(element,"not present in the given list")
OUTPUT:
1.5 Python code to illustrate the positional and default
argument by using 3 variable.
def Add(x,y=52,z=20):
z=x+y
print(x+y)
def Sub(l,m,n=50):
m=l*n
print(l,n,m)
c=50
a=100
b=200
print(a+b)
Add(a,b,c)
Sub(400,300)
Add(b)
OUTPUT:
FUNCTIONS
2.1 Python code illustrating For within for loop.
for x in range(3):
for y in range(2):
print("1,2,3,4,5")
OUTPUT:
2.2 Python code to illustrate return statement using
function.
def Alter(M,N=50):
M=M+N
N=M-N
print(M,"@",N)
return M
A=300
B=100
A=Alter(A,B)
print(A,"#",B)
B=Alter(B)
print(A,"@",B)
OUTPUT:
2.3 To illustrate global and local variable
def check():
global num
num=1000
print(num)
num=100
print(num)
check()
print(num)
OUTPUT:
2.4 Number of uppercase,lowercase and digit in a given
string
“programm to read a line and print its frequency chart”
1)lowewrcase
2)uppercase
3)alphabets
4)no. of digits
line=input("enter the line:")
lcount=ucount=acount=dcount=schar=0
for i in line:
if [Link]():
lcount+=1
elif [Link]():
ucount+=1
elif [Link]():
acount+=1
elif [Link]():
dcount+=1
else:
schar+=1
print("number of lowercase characters in line are:", lcount)
print("number of uppercase characters in line are:", ucount)
print("number of alphabets in line are:", acount)
print("number of digits in line are:", dcount)
OUTPUT:
DATA FILE HANDLING
3.1 (a) Function1 to write a content in a text file [Link].
(b) Function2 to read the content from text file [Link] and
count the number of uppercase “A” or lowercase “a”
appears in text file.
def function1():
f1 = open("[Link]", "w")
[Link]("A clean Environment is Necessary for our Good
health \n Educational Institutions should take the lead")
[Link]()
def function2():
f1 = open("[Link]", "r")
c=0
s1 = [Link]()
for i in s1:
if i == 'a' or i == 'A':
c += 1
print("Number of 'A' or 'a' appearing in the text file is", c)
[Link]()
function1()
function2()
OUTPUT:
3.2 Function2 to read the content from text file ([Link])
and count number of words which are less than 4 character.
def function2():
f1 = open("[Link]", "r")
c=0
s1 = [Link]()
print(s1)
s2 = [Link]()
print(s2)
for i in s2:
if len(i) < 4:
c += 1
print(i)
print("Number of words with length less than 4 is", c)
[Link]()
function2()
OUTPUT:
3.3 Program to write and read the content in text file and
write reverse lines in another text file class XII.
def function1():
f1=open("[Link]","w")
l1=["A Clean Environment is Necessary for our Good Health
\n Educational Institutions should take the Lead"]
[Link](l1)
[Link]()
def function2():
f1=open("[Link]","r")
f2=open("[Link]","w")
data=[Link]()
for i in data:
[Link](i[::-1])
[Link]()
[Link]()
function1()
function2()
OUTPUT:
3.4 Program to write and read the content in text file and
count number of lines starting with “W” or “w”
def function1() :
f1=open("[Link]","w")
[Link]("Net charge inside a sphere is zero.\nHence, its
electric field and flux is also zero.")
[Link]()
def readdata() :
f1=open("[Link]","r")
C=0
S1=[Link]()
for i in S1:
if i[0]=="w" or i[0]=="w":
C=C+1
print("No. of lines starting with w or W:",C)
[Link]
function1()
readdata()
3.5 Program to write and read the content in text file and
count number of times “is” appears in the file.
def function1() :
f1=open("[Link]","w")
[Link]("Net charge inside a sphere is zero.\nHence, its
electric field and flux is also zero.")
[Link]()
def readdata() :
f1=open("[Link]","r")
C=0
S1=[Link]()
S2=[Link]()
for i in S2:
if i== "is":
C=C+1
print("No. of times is appears in the file:",C)
[Link]
function1()
readdata()
3.6 Code to illustrate reading and writing in binary file.
import pickle
def writebinary():
l1=[20,30,40,50,60]
d1={"Rita":100,"Bijoy":80,"Gita":90}
f1=open("[Link]","wb")
[Link](l1,f1)
[Link](d1,f1)
print("File is added to binary file.")
[Link]()
def readbinary():
f1=open("[Link]","rb")
list1=[Link](f1)
list2=[Link](f1)
[Link]()
print(list1)
print(list2)
writebinary()
readbinary()
3.7 Code to write a user-defined function bwrite() to input
data for students and add to [Link]. A binary file
"[Link]" has a structure [roll no, name, per].
import pickle
def bwrite():
student=[]
f1=open("[Link]","wb")
ans="yes"
while ans=="yes":
rollno=int(input("Enter roll no of the student:"))
name=input("Enter the name of the student:")
per=float(input("Enter the percentage of the student:"))
[Link]([rollno, name , per])
ans=input("Want to add more record?")
[Link](student,f1)
print("File is written successfully!")
[Link]()
def bread():
student=[]
f1=open("[Link]","rb")
while True:
try:
student=[Link](f1)
for s in student:
print(s)
except EOFError:
print("End of file encountered.")
break
[Link]()
bwrite()
bread()
3.8 Code to read student data and display the record of
those students whose scored more than 90%.
import pickle
def bread():
student=[]
f1=open("[Link]","rb")
while True:
try:
student=[Link](f1)
for s in student:
if s[2]>90 or s[2]==90:
print(s)
except EOFError:
print("End of file encountered.")
break
[Link]()
bread()
3.9 Code to modify student data.
import pickle
def bsearch():
rn = int(input("Enter roll number to search: "))
found = False
try:
with open("[Link]", "rb") as f1:
while True:
try:
stu = [Link](f1)
for s in stu:
if s[0] == rn:
print("Record found:", s)
found = True
except EOFError:
break
except FileNotFoundError:
print("File not found.")
return
if not found:
print("Sorry, no such roll no present.")
def bupdate():
try:
with open("[Link]", "rb") as f1:
stu = [Link](f1)
except FileNotFoundError:
print("File not found.")
return
except EOFError:
print("No data in file.")
return
print("## Student Records ##")
print(stu)
print("-----------------------------------------")
rn = int(input("Please enter the roll no. to be updated: "))
flag = False
for i in range(len(stu)):
if stu[i][0] == rn:
n1 = input("Enter new name: ")
stu[i][1] = n1
flag = True
print("Record updated.")
break
if not flag:
print("No such record.")
with open("[Link]", "wb") as f1:
[Link](stu, f1)
print("Student record after update:")
print(stu)
print("----------------------------------------------")
def bdelete():
try:
with open("[Link]", "rb") as f1:
stu = [Link](f1)
except FileNotFoundError:
print("File not found.")
return
except EOFError:
print("No data in file.")
return
print("Existing records:")
print(stu)
rn = int(input("Please enter the roll no. to be deleted: "))
flag = False
stu1 = []
for record in stu:
if record[0] != rn:
[Link](record)
else:
flag = True
if flag:
with open("[Link]", "wb") as f1:
[Link](stu1, f1)
print("Record deleted.")
else:
print("No such record to delete.")
print("Records after deletion:")
print(stu1)
bsearch()
bupdate()
bdelete()
3.10 Code to enter employee data in a CSV file.
import csv
f1=open("d:\[Link]", mode="w", newline='')
cwrite=[Link](f1,delimiter=",")
ans="yes"
while ans=='yes':
eno=int(input("Enter employee no:"))
ename=input("Enter employee name:")
salary=int(input("Enter employee salary:"))
[Link]([eno, ename, salary])
print("Data saved!!")
ans=input("Want to add more data?")
[Link]([101,"abc",50000])
[Link]([102,"def",60000])
[Link]()
3.11 Code illustrating seek and tell.
def tellwrite():
f1=open("[Link]","w")
[Link]("Montfort school")
[Link]()
def tellread():
f1=open("[Link]","rb")
print([Link](4,0))
print([Link](2))
print([Link]())
print([Link](2,1))
print([Link]())
print([Link](-4,2))
print([Link](2))
print([Link]())
[Link]()
tellwrite()
tellread()
Stacks
4.1 Simple code for Stacks.
stk=[]
top=-1
def push(stk, student):
[Link](student)
global top
top=len(stk)-1
def pop(stk):
global top
if top==-1:
print("Underflow")
else:
[Link]()
for i in range(1):
sno=int(input("Enter student number: "))
sn=input("Enter student name: ")
data=[sno,sn]
push(stk,data)
print(stk)
pop(stk)
4.2 Code to push all even nos. from a list into a stack and
display the stack. Deleting an element using pop function.
def push(arr):
n=len(arr)
for i in arr:
if(i%2==0):
[Link](i)
elif (stack==[]):
print("Stack empty.")
else:
print("Stack elements after push operation:", stack)
def pop(stk):
n=len(stk)
if (n<=0):
print("Stack empty. Pop not possible.")
else:
print("Element deleted:", [Link]())
stack=[]
list1=[5,10,15,20,25]
push(list1)
pop(stack)
4.3 Code to push the product into a stack costing more
than 50 rupees and pop the items from the stack and
display them. Also, display "stack empty" when there are no
elements in the stack.
L= [("Graphics Card", 100000), ("SmartPhone", 34000), ("S-
Pen", 4300), ("Headphones", 1500)]
product=[]
def pushelement():
for item in L:
name, price=item
if price>50:
[Link](item)
print("Stack after pushing elements:")
print(product)
def popelement():
print("\nPopping elements from stack:")
while len(product)>0:
print([Link]())
print("Stack empty.")
pushelement()
popelement()