Cs Practical File Term 1, Cbse
Cs Practical File Term 1, Cbse
Cs Practical File Term 1, Cbse
NAGAR GHAZIABAD
f = open("textfile.txt", "r")
dat = f.readlines()
for i in dat:
print(i.replace(" ", "#"))
f.close()
Textfile.txt
fi.close()
textfile.txt
Sky
Apple
Qwerty
Sunshine
Baffle
textfile2.txt
Sky
Qwerty
Sunshine
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.
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.
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)
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)