You are on page 1of 28

RYAN INTERNATIONAL SCHOOL

SANPADA

PRACTICAL FILE
COMPUTER SCIENCE

SUBMITTED BY:
VRUSHANT MUKHERJEE
XII F

ROLL NO: 47

CERTIFICATE
This is to certify that Vrushant Mukherjee, a student of class
XII-F,
Ryan International School, Sanpada has successfully
completed the course work of Computer Science under the
guidance of Mrs. Gargy Dam during the year 2021-22 in
partial fulfilment of Computer Science Practical examination
of the Central Board of Secondary Education. I certify this
Project up to my expectation & as per guidelines issued by
CBSE, NEW DELHI.

_____________________________ ____________________________

Signature of Internal Examiner: Signature of External Examiner:

____________________ ____________________

Principal’s Signature: School Stamp:

ACKNOWLEDGMENT
It is with pleasure that I acknowledge my sincere gratitude
to our teacher, Gargy Maam who taught and undertook the
responsibility of teaching the subject computer science. I
have been greatly benefited from his classes.
I am especially indebted to our Principal Mrs. Muriel
Fernandes who has always been a source of
encouragement and support and without whose inspiration
this project would not have been a successful I would like to
place on record heartfelt thanks to him.
Finally, I would like to express my sincere appreciation for
all the other students for my batch their friendship & the
fine times that we all shared together.
Date: Experiment No: 1
Program 1: Input any number from user and calculate factorial
of a number

# Program to calculate factorial of entered number


num = int(input("Enter any number :"))
fact = 1 # storing num in n for printing
n = num # loop to iterate from n to 2
while num>1:
fact = fact * num
num-=1
print("Factorial of ", n , " is :",fact)

OUTPUT __________
Enter any number: 6
Factorial of 6 is: 720
Date: Experiment No: 2
Program 2: Input any number from user and check if it is prime
number
#Program to input any number from user
#Check if it is a Prime number or not
import math
num=int(input("Enter any number: "))
isPrime=True
for i in range(2,int(math.sqrt(num))+1):
if num % i == 0:
isPrime=False
if num!= 1:
if isPrime:
print("## Number is Prime ##")
else:
print("## Number is not Prime ##")
if num==1:
print(“## Number is not Prime ##")

OUTPUT__________
Enter any number: 7
## Number is Prime ##
Enter any number: 1
## Number is not a Prime ##
Date: Experiment No: 3
Program 3: Write a Program that prints the longest word in a
list of words
#Program that prints longest word in a list of words
l=['Python','is','a','computer','language']
a=0
w=None
for i in range(len(l)):
if len(l[i])>a:
w=l[i]
a=len(l[i])
print('Longest word present in list is: ',w)

OUTPUT__________
Longest word present in list is: computer
Date: Experiment No: 4
Program 4: Write a program that prompts for a phone number
with dashes and check whether the phone number is in valid
format
#Program that prompts for a phone number with dashes
#Check whether the phone number is in valid format
phNo = input("Enter the phone number: ")
length = len(phNo)
if length == 12 \
and phNo[3] == "-" \
and phNo[7] == "-" \
and phNo[:3].isdigit() \
and phNo[4:7].isdigit() \
and phNo[8:].isdigit() :
print("Valid Phone Number")
else :
print("Invalid Phone Number")

OUTPUT__________
Enter the phone number: 017-555-1212
Valid Phone Number

Enter the phone number: 017-5A5-1212


Invalid Phone Number
Date: Experiment No: 5
Program 5: Write a Program that asks the user date in
DDMMYYYY format and Prints Month Day, Year format

#Program to convert DDMMYYYY format to Day Month Year


m=["January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"]
datestr =input("Enter date in DDMMYYYY format: ")
monthindex=int(datestr[2:4])-1
month=m[monthindex]
day= datestr[:2]
year = datestr[4:]
newDateStr = month + ' ' + day + ', ' + year
print(newDateStr)

OUTPUT__________

Enter date in DDMMYYYY format: 14072004


July 14, 2004
Date: Experiment No: 6
Program 6: Write a program to print 3 random numbers from a
range specified
#Function to print 3 random numbers from a range entered
import random
def ran(a,b):
print(random.randint(a,b))
first = int(input("Enter a number: "))
second= int(input("Enter second number: "))

ran(first,second)
ran(first,second)
ran(first,second)

OUTPUT__________

Enter a number: 1
Enter second number: 10
1
6
8
Date: Experiment No: 7
Program 7: Write a program that takes two numbers and
returns the number that has minimum one’s digit
#Program to reutrn the number that has minimum one's digit
def min(x,y):
a=x%10
b=y%10
if a<b:
return x
else:
return y
first = int(input("Enter first number = "))
second = int(input("Enter second number = "))
print("Minimum one's digit number: ", min(first,second))

OUTPUT__________
Enter first number = 456
Enter second number = 651
Minimum one's digit number: 651
Date: Experiment No: 8
Program 8: Create a Module lengthconversion.py that stores
functions for various lengths conversion
#Module for various length conversions
one_mille_in_km = 1.609344
one_feet_in_inche = 12

def miletokm(x) :
"Return value in kilometer from miles "
return x * one_mille_in_km

def kmtomile(x) :
"Return value in miles from kilometer "
return x / one_mille_in_km

def feettoinches( x ):
"Return value in inches from feet "
return x / one_feet_in_inche

def inchestofeet(x) :
"Return value in feet from inches "
return x * one_feet_in_inche
x=int(input("Enter a value for conversion: "))
print(x," miles to kilometers is: ",miletokm(x))
print(x," kilometers to miles is: ",kmtomile(x))
print(x," feet to inches is: ",feettoinches(x))
print(x," inches to feet is: ",inchestofeet(x))

OUTPUT__________

Enter a value for conversion: 10


10 miles to kilometers is: 16.09344
10 kilometers to miles is: 6.2137119223733395
10 feet to inches is: 0.8333333333333334
10 inches to feet is: 120
Date: Experiment No: 9
Program 9: Create a module MassConversion.py that stores
function for mass conversion

#Module for various mass conversions


one_kg_in_tonnes = 0.001
one_kg_in_pound = 2.20462

def kgtotonne( x ):
"Return value in tonne from kilogram"
return x * one_kg_in_tonnes

def tonnetokg( x ) :
"Return value in kilogram from tonnes"
return x / one_kg_in_tonnes

def kgtopound ( x ):
"Return value pound from kilogram"
return x * one_kg_in_pound

def poundtokg( x ) :
"Return value kilogram from pound"
return x / one_kg_in_pound
x=int(input("Enter a value for conversion: "))

print(x," kilogram to tonne is: ",kgtotonne(x))


print(x," tonne to kilogram is: ",tonnetokg(x))
print(x," kilogram to pound is: ",kgtopound(x))
print(x," pound to kilogram is: ",poundtokg(x))

OUTPUT__________

Enter a value for conversion: 10


10 kilogram to tonne is: 0.01
10 tonne to kilogram is: 10000.0
10 kilogram to pound is: 22.0462
10 pound to kilogram is: 4.535929094356398
Date: Experiment No: 10
Program 10: Write a program to read and display file content
line by line with each word separated by ‘#’
#Program to read content of file line by line
#and display each word separated by '#'
f = open("file1.txt")
for line in f:
words= line.split()
for w in words:
print(w+'#',end='')
print()
f.close()

NOTE: If the original content of file is:


India is my country
I love python
Python learning is fun

OUTPUT__________
India#is#my#country#
I#love#python#
Python#learning#is#fun#
Date: Experiment No: 11
Program 11: Program to read the content of file and display the
total number of consonants, uppercase, vowels and lower case
characters
#Program to read content of file
#and display total number of vowels, consonants, lowercase and
uppercase characters
f=open("file1.txt")
v=0
c=0
u=0
l=0
o=0
data=f.read()
vowels=['a','e','i','o','u']
for ch in data:
if ch.isalpha():
if ch.lower() in vowels:
v+=1
else:
c+=1
if ch.isupper():
u+=1
elif ch.islower():
l+=1
elif ch!=' ' and ch!='\n':
o+=1

print("Total Vowels in file :",v)


print("Total Consonants in file n :",c)
print("Total Capital letters in file :",u)
print("Total Small letters in file :",l)
print("Total Other than letters f.close():",o)
f.close()
NOTE: If the original content of file is:
India is my country
I love python
Python learning is fun

OUTPUT__________
Total Vowels in file :16
Total Consonants in file :30
Total Capital letters in file:2
Total Small letters in file :44
Total Other than letters :4
Date: Experiment No: 12
Program 12: Program to create binary file to store Roll no and
Name, Search any Roll no and display name if Roll no found
otherwise “Roll no not found”
#Program to create a binary file to store Rollno and name
#Search for Rollno and display record if found
#otherwise “Rollno not found”
import pickle
student=[]
f=open('student.dat','wb')
ans='y'
while ans.lower()=='y':
roll = int(input("Enter Roll Number: "))
name = input("Enter Name: ")
marks = int(input("Enter Marks: "))
student.append([roll,name,marks])
ans=input("Add More?(Y)")
pickle.dump(student,f)
f.close()
f=open('student.dat','rb+')
student=[]
while True:
try:
student= pickle.load(f)
except EOFError:
break
ans='y'
while ans.lower()=='y':
found=False
r=int(input("Enter Roll number to update:"))
for s in student:
if s[0]==r:
print("Name is: ",s[1])
print("Current Marks is: ",s[2])
m=int(input("Enter new marks: "))
s[2]=m
print("Record Updated")
found=True
break
if not found:
print("Sorry! Roll Number not found")
ans=input("Update More?(Y)")
f.close()

OUTPUT__________
Enter Roll Number: 1
Enter Name: Jack
Enter Marks: 83
Add More?(Y)y
Enter Roll Number: 2
Enter Name: Dawson
Enter Marks: 90
Add More?(Y)n
Enter Roll number to update:1
Name is: Jack
Current Marks is: 83
Enter new marks: 88
Record Updated
Update More?(Y)n
Date: Experiment No: 13
Program 13: Program to read the content of file line by line and
write it to another file except for the lines contains „a‟ letter in
it.
#Program to read line from file and write it to another line
#Except for those line which contains letter ‘a’
f1 = open("file2.txt")
f2 = open("file2copy.txt","w")
for line in f1:
if 'a' not in line:
f2.write(line)
print(“## File Copied Successfully! ##”)
f1.close()
f2.close()

NOTE: Content of file2.txt


a quick brown fox
one two three four
five six seven
India is my country
eight nine ten
bye!
OUTPUT__________
## File Copied Successfully! ##
NOTE: After copy content of file2copy.txt
one two three four
five six seven
eight nine ten
bye!
Date: Experiment No: 14
Program 14: Write a Python program to write a nested Python
list to a csv file in one go.

#Program to write a nested python list to a csv file in one go

import csv

file = open( 'file1.txt.txt',"r+" )

lst = eval(input("Enter a nested list :-"))

path_write = csv.writer( file)

path_write.writerows( lst )

data = csv.reader(file)

for i in data :

print(i)

file.close()

NOTE: If the original content of file is:


India is my country
I love python
Python learning is fun
OUTPUT__________
Enter a nested list :- [['Apple','red'], ['Mango','yellow'],
['Orange','orange']]
['India is my country']
['I love python']
['Python learning is fun']

NOTE: After Execution contents of the file:


India is my country
I love python
Python learning is fun
Apple,red
Mango,yellow
Orange,orange
Date: Experiment No: 15
Program 15: Program to create CSV file and store empno,
name, salary and search any empno and display name, salary,
and if not found appropriate message.
import csv
with open('myfile.csv',mode='a') as csvfile:
mywriter = csv.writer(csvfile,delimiter=',')
ans='y'
while ans.lower=='y':
eno=int(input("Enter Employee Number: "))
name=input("Enter Employee Name ")
salary=int(input("Enter Employee Salary :"))
mywriter.writerow([eno,name,salary])
print("## Data Saved... ##")
ans=input("Add More ?")
ans='y'
with open('myfile.csv',mode='r') as csvfile:
myreader = csv.reader(csvfile,delimiter=',')
while ans=='y':
found=False
e = int(input("Enter Employee Number to search :"))
for row in myreader:
if len(row)!=0:
if int(row[0])==e:
print("============================")
print("NAME :",row[1])
print("SALARY :",row[2])
found=True
break
if not found:
print("==========================")
print(" EMPNO NOT FOUND")
print("==========================")
ans = input("Search More ? (Y)")

OUTPUT__________
Enter Employee Number 1
Enter Employee Name Amit
Enter Employee Salary :90000
## Data Saved... ##
Add More ?y
Enter Employee Number 2
Enter Employee Name Sunil
Enter Employee Salary :80000
## Data Saved... ##
Add More ?y
Enter Employee Number 3
Enter Employee Name Satya
Enter Employee Salary :75000
## Data Saved... ##
Add More ?n
Enter Employee Number to search :2
============================
NAME : Sunil
SALARY : 80000
Search More ? (Y)y
Enter Employee Number to search :3
============================
NAME : Satya
SALARY : 75000
Search More ? (Y)y
Enter Employee Number to search :4
==========================
EMPNO NOT FOUND
==========================
Search More ? (Y)n
BIBLIOGRAPHY
1. Computer Science with Python by Sumita Arora
2. www.pathwalla.com

You might also like