Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Cs Practical File Term 1, Cbse

Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

KENDRIYA VIDYALYA KAMLANEHRU

NAGAR GHAZIABAD

CBSE PRACTICAL FILE


NAME: Ashish Rawat
ROLL NO: 6
CLASS: XII – A
SUBJECT: Computer Science
SUBMITTED TO: Mrs. Kiran Singh
(PGT CS)
INDEX
S NO. PROGRAMS

1 Read a text file line by line and display each


word separated by a #.
2 Read a text file and display the number of
vowels/ consonants/uppercase/ lowercase
characters in the file.
3 Remove all the lines that contain the
character `a' in a file and write it to another
file.
4 Create a binary file with name and roll
number. Search for a given roll number and
display the name, if not found display
appropriate message.
5 Create a binary file with roll number, name
and marks. Input a roll number and update
the marks.

6 Write a random number generator


that generates random numbers between 1
and 6 (simulates a dice).

7 Write a code to find the factorial of a


natural number.
8 Write a python code to find the sum of all
elements of a list.
S NO. PROGRAMS

9 Write a code to compute the nth Fibonacci


number.
10 Create a CSV file by entering user-id and
password, read and search the password for
given user-id.
11 Write a python program to implement
python mathematical functions.

12 Write a python program to implement


python string functions.

13 Create a binary file containing details of


book [book no, book_name, author , price].
Define two functions , first to input data &
add to file , and second to give no. Of books
by author
14 Create a binary file student.dat with
admiss.no , name , percentage & display the
details of students whose percentage is
above 75.
15 Write a program to reverse a string using
function.
Textfile.txt

Economic growth of a country depends on


its ability to produce Entrepreneurs.

Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021,


13:44:55) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for
more information.
>>>
=========== RESTART: C:/Python 39/p1.py =======
Economic#growth#of#a#country#depends#on#its#a
bility#to#produce#Entrepreneurs.
>>>
1. Read a text file line by line and display
each word separated by a #.

#Simply using replace function

f = open("textfile.txt", "r")
dat = f.readlines()
for i in dat:
print(i.replace(" ", "#"))

f.close()
Textfile.txt

Economic growth of a country depends on


its ability to produce Entrepreneurs.

Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021,


13:44:55) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for
more information.
>>>
=== RESTART: C:\Python 39\vowels in text file.py ==
vowels = 25
consonants = 40
uppercase characters= 2
lowercase characters= 63
>>>
2. Read a text file and display the number
of vowels/ consonants/uppercase/
lowercase characters in the file.
f=open("textfile.txt")
v=0
c=0
u=0
l=0
vowels=["a","e","i","o","u",'A','E','I','O','U']
for i in f.read():
if i.isalpha()==True:
if i in vowels:
v+=1
else:
c+=1
if i.isupper()==True :
u+=1
elif i.islower()==True:
l+=1
else:
continue
print("vowels = ",v)
print("consonants =",c)
print('uppercase characters=',u)
print('lowercase characters=',l)

fi.close()
textfile.txt
Sky
Apple
Qwerty
Sunshine
Baffle

textfile2.txt
Sky
Qwerty
Sunshine

Python 3.9.2 (tags/v3.9.2:1a79785, Feb


19 2021, 13:44:55) [MSC v.1928 64 bit
(AMD64)] on win32
Type "help", "copyright", "credits" or
"license()" for more information.
>>>
===== RESTART: C:/Python 39/p 3.py ==
>>>
3. Remove all the lines that contain
the character `a' in a file and write it to
another file.
file1=open("textfile.txt")
file2=open("textfile2.txt","w")
for i in file1:
if 'a' in i:
i=i.replace('a',' ')
else:
file2.write(i)
file1.close()
file2.close()
program_3.dat
€• ]”(Œ
Aditya Bansal”Ke.€• ]”(ŒAditya Kumar
Singh”Ke.€• ]”(ŒAlankrit singh
kaim”Ke.€• ]”(ŒAnmol yadav”Ke.€• ]”(Œ
Ansh nipra”Ke.€• ]”(Œ Ashish
Rawat”Ke.€• ]”(ŒJaint”Ke.

Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021,


13:44:55) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()"
for more information.
>>>
====================== RESTART: C:\Python
39\program 3.py ======================
Enter the Roll Number: 6
Name of the student: Ashish Rawat
>>>

Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021,


13:44:55) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()"
for more information.
>>>
==== RESTART: C:\Python 39\program 3.py ===
Enter the Roll Number: 10
This Roll Number does not exist
>>>
4. Create a binary file with name and
roll number. Search for a given roll
number and display the name, if not
found display appropriate message.
import pickle
f=open("program_3.dat", "wb")
pickle.dump(["Aditya Bansal", 1], f)
pickle.dump(["Aditya Kumar Singh", 2], f)
pickle.dump(["Alankrit singh kaim", 3], f)
pickle.dump(["Anmol yadav", 4], f)
pickle.dump(["Ansh nipra", 5], f)
pickle.dump(["Ashish Rawat",6],f)
pickle.dump(["Jaint",7],f)
f.close()

f=open("program_3.dat", "rb")
n=int(input("Enter the Roll Number: "))
exist= False
while True:
try:
x=pickle.load(f)
if x[1]==n:
print("Name of the student:", x[0])
exist = True
except:
break
if exist==False:
print("This Roll Number does not exist")
program_5.dat

(ŒAditya Kumar
Singh”KKZe.€• ]”(ŒAlankrit singh
kaim”KKUe.€• ]”(ŒAnmol
yadav”KKWe.€• ]”(Œ
Ansh nipra”KKRe.€• ]”(Œ Ashish
Rawat”KKKe.€• ]”(ŒJaint”KKYe.

Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19


2021, 13:44:55) [MSC v.1928 64 bit (AMD64)]
on win32
Type "help", "copyright", "credits" or
"license()" for more information.
>>>
== RESTART: C:\Python 39\program 3.py =
Enter the Roll Number: 6
Enter The Updated Marks : 88
>>>
5. Create a binary file with roll number,
name and marks. Input a roll number
and update the marks.
import pickle
f=open("program_5.dat", "wb")
pickle.dump(["Aditya Bansal", 1,80], f)
pickle.dump(["Aditya Kumar Singh", 2,90], f)
pickle.dump(["Alankrit singh kaim", 3,85], f)
pickle.dump(["Anmol yadav", 4,87], f)
pickle.dump(["Ansh nipra", 5,82], f)
pickle.dump(["Ashish Rawat",6,75],f)
pickle.dump(["Jaint",7,89],f)
f.close()

f=open("program_5.dat", "rb")
n=int(input("Enter the Roll Number: "))
m=float(input("Enter The Updated Marks : "))
lst=[ ]
flag=False
while True:
try:
r1=pickle.load(f)
lst.append(r1)
except EOFError:
break
f.close()
Python 3.9.2 (tags/v3.9.2:1a79785, Feb
19 2021, 13:44:55) [MSC v.1928 64 bit
(AMD64)] on win32
Type "help", "copyright", "credits" or
"license()" for more information.
>>>
====== RESTART: C:\Python 39\p 6.py ==
4
>>>
6. Write a random number
generator that generates random
numbers between 1 and 6 (simulates
a dice).

def random():
import random
s=random.randint(1,6)
return s
print(random())
Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19
2021, 13:44:55) [MSC v.1928 64 bit
(AMD64)] on win32
Type "help", "copyright", "credits" or
"license()" for more information.
>>>
===== RESTART: C:\Python 39\p 7.py =====
Enter a Number:6
The factorial of 6 is 720
>>>
7. Write a code to find the factorial of
a natural number.

number= int(input("Enter a Number:"))

fact = 1
if number < 0:
print("factorial does not exist for
negative numbers")
elif number == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,number + 1):
fact = fact*i
print("Factorial of",number,"is",fact)
Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19
2021, 13:44:55) [MSC v.1928 64 bit (AMD64)]
on win32
Type "help", "copyright", "credits" or "license()"
for more information.
>>>
===== RESTART: C:\Python 39\p 8.py =======
Enter element of list:256
do you want to add more element?(y/n)y
Enter element of list:20
do you want to add more element?(y/n)y
Enter element of list:94
do you want to add more element?(y/n)n
your list is: [256, 20, 94]
Sum of all numbers in list: 370
>>>
8. Write a python code to find the sum of
all elements of a list.

lst=[]
summ=0
n="y"
while n=='y':
a=int(input("Enter element of list:"))
lst.append(a)
n=input("do you want to add more
element?(y/n)")
print("your list is:",lst)

for i in range(0, len(lst)):


summ = summ + lst[i]

print("Sum of all numbers in list: ", summ)


Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19
2021, 13:44:55) [MSC v.1928 64 bit (AMD64)]
on win32
Type "help", "copyright", "credits" or
"license()" for more information.
>>>
========================= RESTART:
C:\Python 39\p 9.py
=========================
enter a number:9
0
1
1
2
3
5
8
13
21
>>>
9. Write a code to compute the nth
Fibonacci number.

def fibonacci(number):
a=0
b=1
if number==1:
print(a)
else:
print(a)
print(b)
for i in range(2,number):
c=a+b
a=b
b=c
print(c)
n=int(input("enter a number:"))
fibonacci(n)
Python 3.9.2 (tags/v3.9.2:1a79785,
Feb 19 2021, 13:44:55) [MSC v.1928
64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or
"license()" for more information.
>>>
=== RESTART: C:\Python 39\p 13.py ==
user-id list is ['user 1', 'user 2', 'user 3',
'user 4', 'user 5']
Enter the user-id: user 1
The password is: 123
>>>
10 Create a CSV file by entering user-id
and password, read and search the
password for given user-id.
import csv
List = [["user 1", "123"],["user 2",
"xyz"],["user 3", "pqr"],["user 4",
"stu"],["user 5", "abc"]]
users=['user 1','user 2','user 3','user 4','user
5']
print("user-id list is",users)
f=open("Userpass.csv","w")
writer=csv.writer(f)
writer.writerows(List)
f.close()
f2=open("Userpass.csv", "r")
rows=csv.reader(f2)
userId = input("Enter the user-id: ")
x = True
for i in rows:
if i[0]==userId:
print("The password is: ", i[1])
x = False
break
if x:
print("User not found. Incorrect user")
Python 3.9.2 (tags/v3.9.2:1a79785, Feb
19 2021, 13:44:55) [MSC v.1928 64 bit
(AMD64)] on win32
Type "help", "copyright", "credits" or
"license()" for more information.
>>>
=========================
RESTART: C:\Python 39\p 14.py
========================
Enter num:20
The value of 20^8: 25600000000.0
Square root of 400: 4.47213595499958
The value of 5^e: 485165195.4097903
The value of Log(625), base 5:
1.8613531161467862
The value of Log(1024), base 2:
4.321928094887363
The value of Log(1024), base 10:
1.3010299956639813
>>>
11. Write a python program to
implement python mathematical
functions.

import math
num= int(input("Enter num:"))
print(f'The value of {num}^8: ' +
str(math.pow(num, 8)))
print('Square root of 400: ' +
str(math.sqrt(num)))
print('The value of 5^e: ' +
str(math.exp(num)))
print('The value of Log(625), base 5: ' +
str(math.log(num, 5)))
print('The value of Log(1024), base 2: ' +
str(math.log2(num)))
print('The value of Log(1024), base 10: ' +
str(math.log10(num)))
Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19
2021, 13:44:55) [MSC v.1928 64 bit
(AMD64)] on win32
Type "help", "copyright", "credits" or
"license()" for more information.
>>>
=== RESTART: C:\Python 39\p 15.py=
Enter a stringAshish will join NDA
Ashish will join nda
ashish will join nda
ASHISH WILL JOIN NDA
Ashish Will Join Nda
does the given strng has only lower
characters False
does the given strng has only upper
characters False
>>>
12. Write a python program to
implement python string functions.

a = input("Enter a string")
a2 = a.capitalize()
print(a2)
b2 = a.lower()
print(b2)
c2 = a.upper()
print(c2)
d2 = a.title()
print(d2)
print("does the given strng has only lower
characters",a.islower())
print("does the given strng has only upper
characters",a.isupper())
l = a.split()

z = "Techvidvan"
z.lstrip()
z.rstrip()
z.strip()
Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021,
13:44:55) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()"
for more information.
>>>
== RESTART: C:\Python 39\Program 13.py ===
1. Input Data
2. No Of Books by Author
Select [1/2]: 1
Author Name: Chetan Bhagat
Book Number: 101
Book Name: 400 Days
Price of Book: $20
Record Updated
[101, '400 Days', 20, 'Chetan Bhagat']
>>>
13. Create a binary file containing details
of book [book no, book_name, author ,
price]. Define two functions , first to
input data & add to file , and second to
give no. Of books by author
import pickle
def file(a1):
f1 = open('book.dat','ab')
b1 = int(input('Book Number: '))
c1 = input('Book Name: ')
p1 = int(input('Price of Book: $'))
rec = [b1,c1,p1,a1]
pickle.dump(rec,f1)
f1.close()
print('Record Updated')
print(rec)

def count(a1):
f1 = open('book.dat','rb')
n=0
try:
while True:
rec = pickle.load(f1)
if a1 == rec[3]:
n=n+1
Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19
2021, 13:44:55) [MSC v.1928 64 bit
(AMD64)] on win32
Type "help", "copyright", "credits" or
"license()" for more information.
>>>
=== RESTART: C:\Python 39\Program 13.py =
1. Input Data
2. No Of Books by Author
Select [1/2]: 2
Author Name: Chetan Bhagat
1
>>>
except:
f1.close()
print(n)

z12=int(input('''1. Input Data


2. No Of Books by Author
Select [1/2]: '''))
if z12 == 1:
a1 = input('Author Name: ')
file(a1)
elif z12 == 2:
a1 = input('Author Name: ')
count(a1)
else:
print('Invalid Operation')
Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021,
13:44:55) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for
more information.
>>>
==== RESTART: C:\Python 39\P14 percentage.py
=======
enter name of the student:Ashish Rawat
enter admission number:10162
enter percentage:80
do you want to add more data?(y/n):y
enter name of the student:Aditya KS
enter admission number:10163
enter percentage:85
do you want to add more data?(y/n):y
enter name of the student:Sourav Singh
enter admission number:10164
do you want to add more data?(y/n):n
{'Name': 'Ashish Rawat', 'adm_no': 10162,
'percentage': 80}
{'Name': 'Aditya KS', 'adm_no': 10163, 'percentage':
85}
>>>
students.dat
€•6 }”(ŒName”Œ Ashish
Rawat”Œadm_no”M²'Œ
percentage”KPu.€•3 }”(ŒName”Œ Adi
tya KS”Œadm_no”M³'Œ
percentage”KUu.
14. Create a binary file student.dat
with admiss.no , name , percentage &
display the details of students whose
percentage is above 75.
import pickle
f=open("students.dat","wb")
dict={}
choice="y"
while choice=="y":
name=input("enter name of the student:")
adm_no=int(input("enter admission
number:"))
percentage=int(input("enter percentage:"))
dict["Name"]=name
dict["adm_no"]=adm_no
dict["percentage"]=percentage
pickle.dump(dict,f)
choice=input("do you want to add more
data?(y/n):")
f.close()
f=open("students.dat","rb")
try:
while True:
s=pickle.load(f)
if s["percentage"]>75:
print(s)
else:
continue
except:
f.close()
Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19
2021, 13:44:55) [MSC v.1928 64 bit
(AMD64)] on win32
Type "help", "copyright", "credits" or
"license()" for more information.
>>>
== RESTART: C:\Program 15.py ===
Enter desired string: Ashish is in Class 12
The reversed string is : 21 ssalC ni si
hsihsA
>>>
15. Write a program to reverse a string
using function.

z=input('Enter desired string: ')


s = ''
for i in z:
s = i+s
print('The reversed string is :',s)

You might also like