You are on page 1of 15

COMPUTER

SCIENCE
PRACTICAL
FILE
TERM-I

NAME: TANAY AGRAWAL


CLASS: XII-A
ROLL NO.: 46
Index
1. WAP to calculate the area of a triangle.
2. WAP to add, subtract, multiply and divide 2 user inputted numbers.
3. WAP to check to print out the Fibonacci series.
4. WAP to check if a number is a palindrome.
5. WAP to print the factorial of a number.
6. WAP to input and update a dictionary.
7. WAP to count the uppercase and lowercase characters in a string.
8. Write a python code to display the number of lines in a file "abcd.txt”.
9. WAP to display the first character of all the lines in the file "abcd.txt".
10. Write a function in python to count the number of lines in a text file which is
starting with an alphabet ‘A’.
11. Create a text file and display the file and all lines starting with capital letters.
12. Write a function in Python that counts the number of “Me” or “My” words
present in a text file.
13. WAP in python to count the number of lowercase alphabets present in a text file.
14. Write a program to add/insert records of students in file “data.csv”.
15. Write a program to count the number of records of students in a file “data.csv”.
16. Write a program to copy the data from “data.csv” to “temp.csv”.
17. Read CSV file values in the form of comma separated values, instead of lists.
18. Read the Roll number, Name, Class in separate lines from a file 'student.dat'
where data is present in a list.
19. Write a function cou() in Python that would read contents of the file and display
the details of those students whose percentage is above 75. Also display number of
students scoring above 75%.
20. Write a function in Python that would read contents of the file “emp.dat” and
delete the details of the employee.
Q1. WAP to calculate the area of a rectangle.
Ans: def ar(a, b):
c=a*b

return c

x = int(input('Enter length of rectangle- '))

y = int(input('Enter breadth of rectangle- '))

e= ar(x, y)

print("Area of rectangle = ", e, )

print("Perimeter of rectangle = ", f)

Output: Enter length of rectangle- 8

Enter breadth of rectangle- 6

Area of rectangle = 48

Q2. WAP to add, subtract, multiply and divide 2 user inputted numbers.
Ans: num1 = int(input("Enter First Number: "))
num2 = int(input("Enter Second Number: "))

print("Enter which operation would you like to perform?")

ch = input("Enter any of these char for specific operation +,-,*,/: ")

result = 0

if ch == '+':

result = num1 + num2

elif ch == '-':

result = num1 - num2

elif ch == '*':

result = num1 * num2

elif ch == '/':

result = num1 / num2

else:

print("Input character is not recognized!")

print(num1, ch , num2, "=", result)


Output: Enter First Number: 9
Enter Second Number: 2

Enter which operation would you like to perform?

Enter any of these char for specific operation +,-,*,/: *

9 * 2 = 18

Q3: WAP to check to print out the Fibonacci series.


Ans: def fib(n):
a=0

b=1

print(a)

print(b)

for i in range(2, n):

c=a+b

a=b

b=c

print(c)

d = int(input("enter value:"))

fib(d)

Output: enter value:7


0

Q4. WAP to check if a number is a palindrome.


Ans: n=int(input("Enter any number :"))
temp=n
rev=0

while(n>0):

digit=n%10

rev=rev*10+digit

n=n//10

if(temp==rev):

print("The number is palindrome!")

else:

print("Not a palindrome!")

Output: Enter any number :123321


The number is palindrome!

Q5. WAP to print the factorial of a number.


Ans: def fact(a):
b=1

for i in range(a, 1, -1):

b=b*i

return b

d = int(input("Enter number:"))

print(fact(d))

Output: Enter number:6


720

Q6. WAP to input and update a dictionary.


Ans: d={}
n=int(input('Enter number of records:'))

for i in range(n):

rno=int(input('Enter roll no.:'))

name=input('Enter name:')

d[rno]=name
print(d)

d1={}

n1=int(input('Enter the number of records you want to add:'))

for i in range(n1):

r_no=int(input('Enter roll no.:'))

nname=input('Enter new name:')

d1[r_no]=nname

d.update(d1)

print(d)

Output: Enter number of records:2


Enter roll no.:3

Enter name:tanay

Enter roll no.:6

Enter name:aakash

{3: 'tanay', 6: 'aakash'}

Enter the number of records you want to add:1

Enter roll no.:9

Enter new name:samarth

{3: 'tanay', 6: 'aakash', 9: 'samarth'}

Q7. WAP to count the uppercase and lowercase characters in a string.


Ans: def string(s):
d={"UPPER_CASE":0, "LOWER_CASE":0}

for c in s:

if c.isupper():

d["UPPER_CASE"]+=1

elif c.islower():

d["LOWER_CASE"]+=1

else:

pass

print ("Original String : ", s)

print ("No. of Upper case characters : ", d["UPPER_CASE"])


print ("No. of Lower case Characters : ", d["LOWER_CASE"])

string('My Name Is Tanay')

Output: Original String : My Name Is Tanay


No. of Upper case characters : 4

No. of Lower case Characters : 9

Q8. Write a python code to display the number of lines in a file "abcd.txt”.
Ans: a = open ("abcd.txt", "r")
b = a. readlines()

c=0

for i in b:

c = c+1

print(" Number of lines= ", c)

Output: Number of lines=6

Q9. WAP to display the first character of all the lines in the file "abcd.txt".
Ans: a = open("abcd.txt", "r")
b = a.readlines()

c = len(b)

for i in range(c):

d = b[i][0]

print(d)

Output: M
I

M
Q10. Write a function in python to count the number of lines in a text file which is starting with an
alphabet ‘A’.
Ans: a = open ("story.txt", "r")
b = a. readlines()

c=0

for i in b:

if i[0] == "A or a":

c=c+1

print(" Number of lines starting with A= ", c)

Output: Number of lines starting with A= 0

Q11. Create a text file and display the file and all lines starting with capital letters .
Ans: a = open("abcd.txt", "r")
f = a.readlines()

g = []

for i in f:

for j in i:

g.append(j)

for ab in range(len(g)):

if g[ab] == ".":

if g[ab + 1].islower():

g[ab + 1] = g[ab + 1].upper()

else:

if g[ab + 1] == " ":

if g[ab + 2].islower():

g[ab + 2] = g[ab + 2].upper()

for i in range(len(g)):
if g[i] == "\n":

if g[i - 1] == ".":

g[i + 1] = g[i + 1].upper()

aq = open("lines2.txt", "w")

z = ''

for i in g:

z=z+i

aq.writelines(z)

aq.close()

aq = open("lines2.txt", "r")

ae = aq.read()

print(ae)

Output: My name is Tanay


I'm in class 12

I like watching movies

Q12. Write a function in Python that counts the number of “Me” or “My” words present in a text
file.
Ans: def count():
f = open("abcd.txt", "r")

d = f.read()

m = d.split()

c=0

for i in m:

if i =="Me" or i =="My" :

c=c+1

print("Number of Me/My in file are:", c)

count()

Output: Number of Me/My in file are: 3


Q13. WAP in python to count the number of lowercase alphabets present in a text file.
Ans: a = open("abcd.txt", "r")
b = a.readlines()

c=0

for i in b:

for j in i:

if j.islower():

c=c+1

print("Number of lowercase alphabets= ", c)

Output: Number of lowercase alphabets= 76

Q14. Write a program to add/insert records of students in file “data.csv”.


Ans: import csv
def create():

fields = ["Roll Number", "Name", "Marks"]

with open("data.csv", "w", newline='') as d:

csv_w = csv.writer(d, delimiter=",")

csv_w.writerow(fields)

def new():

a = input("Roll Number-")

b = input("Name- ")

c = input("Marks- ")

insert = [a, b, c]

with open("data.csv", "a", newline='') as d:

csv_w = csv.writer(d, delimiter=",")

csv_w.writerow(insert)

while True:

print('''Enter 0 to create new file/overwrite old file

Enter 1 to add student

Enter 2 to exit the program''')

ab = int(input("Enter 0, 1, or 2- "))
if ab == 0:

create()

elif ab == 1:

new()

else:

if ab == 2:

break

Output: Enter 0 to create new file/overwrite old file


Enter 1 to add student

Enter 2 to exit the program

Enter 0, 1, or 2- 0

Enter 0 to create new file/overwrite old file

Enter 1 to add student

Enter 2 to exit the program

Enter 0, 1, or 2- 1

Roll Number-2

Name- tanay

Marks- 500

Enter 0 to create new file/overwrite old file

Enter 1 to add student

Enter 2 to exit the program

Enter 0, 1, or 2- 1

Roll Number-6

Name- yuvraj

Marks- 600

Enter 0 to create new file/overwrite old file

Enter 1 to add student

Enter 2 to exit the program

Enter 0, 1, or 2- 2
Q15. Write a program to count the number of records of students in a file “data.csv”.
Ans: import csv
a = open("data.csv", "r")

b = csv.reader(a)

c=0

for i in b:

if b.line_num == 1:

continue

c=c+1

print('number of records=',c)

Output: Number of records=2

Q16. Write a program to copy the data from “data.csv” to “temp.csv”.


Ans: import csv

f=open("data.csv","r")

f1=open("temp.csv",'w',newline='')

d=csv.reader(f)

d1=csv.writer(f1)

for i in d:

d1.writerow(i)

print('csv copied')

f.close( )

f1.close( )

Output: csv copied

Q17. Read CSV file values in the form of comma separated values, instead of lists.
Ans: import csv
a = open("student.csv", "r")

b = csv.reader(a)
for i in b:

print(','.join(i))

a.close()

Output: Roll Number, Name, Marks


2, Tanay, 500

6, Yuvraj, 600

Q18. Read the Roll number, Name, Class in separate lines from a file 'student.dat' where data is
present in a list.

Ans: import pickle


def read():

f = open("student.dat", "rb")

while True:

try:

a = pickle.load(f)

print("Roll Number= ", a[0])

print("Name= ", a[1])

print("Percentage= ", a[2])

except EOFError:

break

f.close()

read()

Output: Roll Number=1


Name= Tanay

Percentage=100

Roll Number=2

Name= Ishan

Percentage=95

Roll Number=3

Name= Samarth

Percentage=96
Q19. Write a function cou() in Python that would read contents of the file and display the details of
those students whose percentage is above 75. Also display number of students scoring above 75%.
Ans: import pickle
def cou():

a = open("student.dat", "rb")

num = 0

while True:

try:

b = pickle.load(a)

if b[2] > 75:

num += 1

print(b[0], b[1], b[2])

except EOFError:

break

print("Number of students with 75% or higher", num)

cou()

Output: 1 Tanay 90
2 Ishan 88

3 Samarth 80

Number of Students with 75% or higher 3

Q20. Write a function in Python that would read contents of the file “emp.dat” and delete the
details of the employee.
Ans: import pickle
def read():

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

while True:

try:

a = pickle.load(f)

print(a)

except EOFError:

break

def delete():
op = int(input('Enter Employee number to delete:- '))

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

a = []

b = []

while True:

try:

y = pickle.load(f)

a.append(y)

except EOFError:

break

f.close()

for j in a:

if j[0] == op:

continue

b.append(j)

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

for z in b:

pickle.dump(z, f)

f.close()

print("Employee successfully deleted from records")

read()

delete()

read()

Output: [1, ‘Tanay’]


[2,’’Samarth”]

[3,’Ishan’]

Enter Employee number to delete:- 2

[1,’Tanay’]

[3,’Ishan’]

You might also like