Monday 8 February 2021

Practical No. 10: Create a text file and remove the lines from the file which contains letter ‘K’

 





'''Practical No. 10: WAP in Python to create a text file and remove

the lines which contains the letter 'K'.'''

import sys

f=open("sps.txt","w+")

print("Enter the lines/data to insert in the file: ")

data = sys.stdin.readlines()

for i in data:

    f.write(i)

f.close()

print("**********")

print("Content of File: ")

f=open("sps.txt","r")

data=f.read()

print(data)

f.close()

f=open("sps.txt","r+")

data=f.readlines()

f.seek(0)

for i in data:

    if "K" not in i:

        f.write(i)

f.truncate()

f.close()

print("**********")

print("Content of File After Deletion: ")

f=open("sps.txt","r")

data=f.read()

print(data)


Practical No. 9 : Create a binary file with eid, ename and salary and update the salary of the employee.

 





'''Practical No: 9: WAP in Python to create a binary file with eid, ename and 

salary of the employees and update the salary of specific employee.'''

import pickle

E={}

f=open('emp.dat','wb')

c='y'

while c=='y' or c=='Y':

    eid=int(input("Enter the Emp Id of the Employee : "))

    ename=input("Enter the name of the Employee: ")

    salary=float(input("Enter the salary of the Employee: "))

    E['Emp_Id']=eid

    E['Emp_Name']=ename

    E['Salary']=salary

    pickle.dump(E,f)

    c=input("Do You Want to add more employee(y/n): ")

f.close()

f=open('emp.dat','rb+')

eid=int(input("Enter the Emp Id of the employee to be updated: "))

salary=float(input("Enter the updated salary of the employee: "))

f.seek(0,0)

m=0

try:

    while True:

        pos=f.tell()

        E=pickle.load(f)

        if E["Emp_Id"] == eid:

            f.seek(pos)

            E["Salary"]=salary

            pickle.dump(E,f)

            m=m+1

except EOFError:

    f.close()

if m==0:

        print("Employee not Found")

else:      

    f=open('emp.dat','rb')

    try:

        while True:

            E=pickle.load(f)

            print(E)

    except EOFError:

        f.close()




Practical No. : 8 - Create a binary file with roll_no, name and marks of some students and update the marks of specific student.

 









'''Practical No: 9: WAP in Python to create a binary file with roll_no, name and

marks of the students and update the marks of specific student.'''

import pickle

S={}

f=open('stud.dat','wb')

c='y'

while c=='y' or c=='Y':

    rno=int(input("Enter the roll no. of the student : "))

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

    marks=int(input("Enter the marks of the student: "))

    S['RollNo']=rno

    S['Name']=name

    S['Marks']=marks

    pickle.dump(S,f)

    c=input("Do You Want to add more students(y/n): ")

f.close()

f=open('stud.dat','rb+')

rno=int(input("Enter the roll no. of the student to be updated: "))

marks=int(input("Enter the updated marks of the student: "))

f.seek(0,0)

m=0

try:

    while True:

        pos=f.tell()

        S=pickle.load(f)

        if S["RollNo"] == rno:

            f.seek(pos)

            S["Marks"]=marks

            pickle.dump(S,f)

            m=m+1

except EOFError:

    f.close()

if m==0:

        print("Student not Found")

else:      

    f=open('stud.dat','rb')

    try:

        while True:

            S=pickle.load(f)

            print(S)

    except EOFError:

        f.close()





Practical No. 7: WAP in Python to create a binary file with name and roll number of the students. Search for a given roll number and display the name of student.

 











'''

Practical No: 7: WAP in Python to create a binary file with name and roll number

of the students. Search for a given roll number and display the name of student.

'''

import pickle

S={}

f=open('stud.dat','wb')

c='y'

while c=='y' or c=='Y':

    rno=int(input("Enter the roll no. of the student : "))

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

    S['RollNo']=rno

    S['Name']=name

    pickle.dump(S,f)

    c=input("Do You Want to add more students(y/n): ")

f.close()

f=open('stud.dat','rb')

rno=int(input("Enter the roll no. of the student to be search: "))

K={}

m=0

try:

    while True:

        K=pickle.load(f)

        if K["RollNo"] == rno:

            print(K)

            m=m+1

except EOFError:

    f.close()

if m==0:

      print("Student not Found")


Practical No. 6: WAP in Python to read a text file and print the number of uppercase and lowercase letters in the file.

 













'''
Practical No. 6: WAP in Python to read a text file and print the number of
uppercase and lowercase letters in the file
'''
f=open("Vowel.txt","r")
data=f.read()
U=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R', \
   'S','T','U','V','W','X','Y','Z',]
L=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r', \
   's','t','u','v','w','x','y','z']

cu=0
cl=0
for i in data:
    if i in U:
        cu=cu+1
    elif i in L:
        cl=cl+1

ct=0
for i in data:
    ct=ct+1
print("Number of Uppercase letters in the file=",cu)
print("Number of Lowercase Letters in the file=",cl)
print("Number of Total Chars in the file=",ct)


f.close()







'''
Practical No. 6: WAP in Python to read a text file and print the number of
uppercase and lowercase letters in the file
'''
f=open("Vowel.txt","r")
data=f.read()


cu=0
cl=0
for i in data:
    if i.isupper():
        cu=cu+1
    elif i.islower():
        cl=cl+1

ct=0
for i in data:
    ct=ct+1
print("Number of Uppercase letters in the file=",cu)
print("Number of Lowercase Letters in the file=",cl)
print("Number of Total Chars in the file=",ct)


f.close()


Practical No. 5: WAP in Python to read a text file and print the line or paragraph starting with the letter ‘S’

 








#Practical No. 5- WAP in  python  to read a text file, count and print no. of line

#start from 'S' or 's'


f=open("abc.txt","r")

line=f.readline()

lc=0

while line:

    if line[0]=='s' or line[0]=='S':

        print(line,end="")

        lc=lc+1

    line=f.readline()

f.close()

print("Total number of lines start with 's' or 'S'=",lc)



Practical No. 4: WAP in Python to read a text file and print the number of vowels and consonants in the file.

 














'''
Practical No. 4: WAP in Python to read a text file and print the number of Vowels and
Consonants in the file
'''
f=open("Vowel.txt","r")
data=f.read()
V=['A','E','I','O','U','a','e','i','o','u']
C=['B','C','D','F','G','H','J','K','L','M','N','P','Q','R','S','T','V','W','X', \
   'Y','Z','b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w', \
   'x','y','z']

cv=0
cc=0
for i in data:
    if i in V:
        cv=cv+1
    elif i in C:
        cc=cc+1

ct=0
for i in data:
    ct=ct+1
print("Number of Vowels in the file=",cv)
print("Number of Consonants in the file=",cc)
print("Number of Total Chars in the file=",ct)


f.close()



Practical 3: Write a program in Python to input the value of x and n and print the sum of the following series 1+x+x^2+x^3+ ----------------x^n

 









#Practical No. - 3 : WAP in Python to input the value of x and n and
#print the sum of the following series
# 1 + x + x^2 + x^3 + ............. + x^n

x=int(input("Enter the value of x: "))
n=int(input("Enter the value of n: "))
sum=0
for i in range(0,n+1):
    sum=sum+x**i


print("Sum of series=",sum)


Practical No. 2: WAP in Python to find the factorial of a number using function.

 

#Practical No. - 2: WAP in Python to implement default and positional parameter

#Default Parameter

def show(a,b,c=8):
    print("Sum=",(a+b+c))

show(5,6)
show(5,6,10)


def show1(a,b=9,c=8):
    print("Sum1=",(a+b+c))

show1(5)
show1(5,6)
show1(5,6,10)



#Positional Parameter

def show2(a,b):
    print("Sub=",(a-b))

show2(15,6)
show2(6,15)






















Practical No. 1: WAP in Python to find the factorial of a number using function.

 







#Practical No. - 1: WAP in Python to find the factorial of a number using function


def fact(n):

    f=1

    while(n>0):

        f=f*n

        n=n-1

    return f



n=int(input("Enter a number to find the factorial: "))

if(n<0):

    print("Factorial of -ive number is not possible")

elif(n==0):

    print("Factorial of 0 is 1")

else:

    factorial=fact(n)

    print("Factorial of ",n," is =",factorial)