You are on page 1of 20

COMPUTER SCIENCE

PRACTICAL FILE

Submitted To: Submitted By:


Mr.Su nil Jangra Shaurya Chauhan
Class: XII B
ACKNOWLEDGEMENT

I would like to express my special thanks


of gratitude to my teacher "Mr. Sunil
Jangra as well as our principal "Dr. Anita
Shama" who gave me the golden
opportunity to do this wonderful project on
the topic Computer Science Practical File,
which also helped me in doing a lot of
Research and I came to know about so
many new things . I am really thankful to
them.

Secondly I would also like to thank my


parents and friends who helped me a lotin
finalizing this project within the limited
time frame.

Shaurya Chauhan
XII B
CERTIFICATE

This is to certify that the Shaurya Chauhan


,student of class Xll,Delhi World Public
School,Bhiwani has completed the Practical
file during the academic year 2022-2023
towards partial fulfillment of credit for
Computer Science practical evaluation of
CBSE SSCE-2023 and submitted
satisfactory report,as compiled in the
following pages,under my supervision.

Internal Examiner External Examiner

Principal
Date:
1:Write a python script to take input for 3 numbers,check
and print the largest number.

a int (input ( ' Enter the first number ' ))


b=int (input ( ' Enter the second number ' ))
c=int (input ( ' Enter the third number ' )) Hit
1 t-[a , b, c]
print ( ' The largest number is ' , max (lst)) I

Output:

Enter the first number23
Enter the second number46
Enter the third number35
The largest number is 46

2:Write a python script to take input for name and age of a


person and check and print whether the person can vote or
not.

per name input ("Enter name of the person" )


per:age=int (input ("Enter age of the person" ))
f per_age >= 18 :
print ("The pers n " , per_name , "can vote " )
l .
print ("th"' pi:,rson" , per_namc, "can not votP " )I
Output:

Enter name of the person haurya


Enter age of the personl ~
the person Shaurya can not vote
I

3: Write a program to input two numbers m


and n and display first m multiples of n.

n=int (input ("enter thi:; number"))


m=int (input ("h0·· many multiples do y0u need" ))
x=O
fr i in range (l ,m1) :
X =l
p=i*n
. t ('the ' ,x, ' . ult.1p1e of ' ,n, ' 1s
pr1n . ' ,p)

Output:
__- 8' PHP

,,_
css
jenter the nurnber35
how many multiples do you need5 ~ --
the 1 multiple of 35 is 35
the 2 multiple of 35 is 70
Ithe 3 multiple of
the 4 multiple of
35
35
lS 105
is 140
the 5 multiple of 35 lS 175
4: Write a program to check if the entered
number is Armstrong or not.

num - int (input ("enter the number to be checked" ))

i Changed num variable to string,


and calculated the length (number of digits)
0rder = len str (nurn.)
I
t initialize sum
sum= O
t find the sum of the cube of each digit
ti:mp = nu.rn
whi: temp> 0 :
digit= temp% :o
sum+= digit** order
temp//= 10

j display the result


1f num -= sum:
print (num, "is an Armstrong number" )

pri nt 'm.1!11, "is not an Armstrong number" )

Output:

4 I.\JIJ J

enter the nunilier to be checkedlo)~


1~1~ is an Armstron~ nurru)er
5: Write a program to input day number of a
week and display the corresponding day name .

f Python program to input week number and print week day


wPekday = int (input ("Ent<=>r weekday number (1 -7) : " ))

if weekday== 1 :
print ("\nMonday" ) ;

rlif weekday -- 2 :
print ( ''\nTuesday'' )

(we-=-kday == 3) :
print C"\nWednesday" )

e 1f (~eekday == 4) :
pri nt ( "\nThursday" )

(we<=>kday == 5) :
print ( "\nFi:iday" )

el1f (weeKday == 6) :
pri nt ( "\nsaturday" )

p f (weekday== 7) :
print ("\nSunday" )
el ...
pri nt ("\nPlease enter any weekday number (1-7)" )

Output:

Enter weekday number (1 - 7) : 5

Friday
6: Write a menu driven program to calculatethe
total surface area and volume of a cube,cuboid
or cylinder depending upon user's choice.
1--1

1 f a cube (s1de :
2 area=side*side
3 print ("Thi:> ar<?a of .:he cube is", area)
4
5 v_cube l~td 0 ) :
6 volume=~ide*side*side
7 print ("The volwne of the cube is" ,volume)

9 - a_cyl () :
1 PI - 3.14
11 radius= float (input ( 'Please Enter the Radius of a cylinder: ' ))
12 height= fl oat (input ( 'Please Enter the Height of a Cylinder: ' ))
13 sa = 2 *PI* radius+ (radius+ h2~Jh~)
1~ print ("Th-'2 7.otal surtac'? area of :.he cylinder :.s", sa)
15
16 I v_cyl ():
17 FI - 3.14
l - radiu~ - float (input ( 'Please Enter the Radius of a Cylinder: ' ))
19 height= float (input ( 'Please Enter the Height of a Cylinder: ' ))
20 Volume= PI* radius* radius* height
21 print ("The volwne of the cylinder is", Volume)
22
23 f a cuboid (length,width,height):
24 ~A=~* (length* width+ length* height+ width* height)
25 print ("The total surface area of t:he cuboid is" ,SA)
26
27 f v_cuboid (length,widch,height) :
2 Vvl \ffiF =length* width* h0 ight
29 print ("The volume of the cuboid is", ilolum0)
3
31 w l c ~-11F :
32 print ("Welcome to the progra'll" )
33 print ( ' \n' )
34 print ("what would you like to calculate\n"
35 ''!.Total Surface Area\n''
3€ "2.Volume\n"
37 "3 . Ex_:_ t" )
38 nino=int linout lll
38 pinp=int (input ())
39 if pinp==l :
40 print ("Whose Total Surface Area ;;ould you like to calculate?\nn
41 "l . Cube\n"
42 "2. Cuboi d\n"
43 "3 . Cylinder\n"
44 "4 .Exit ")
45 i=int (input ())
4€ if i == 1:
47 aceinp=float (input ( "Please enter the side of the cUbe"))
48 a_cube(aceinp)
49
50 el1f i .... 2:
51 h=float iinput (" Pl,;;ase enter the height of the cuboid"))
52 ;,·=float (input ( "Please enter -:he width of the cuboid"))
53 l=float (input ("Please enter the length of the cuboid"))
54 a_cubci.d( l, ,,i, h)
55 elif i == 3 :
56 a_cyl ()
57 el if i == 4 :
58 bre~K
59 el~e :
EO print (" Pl ease enter a number betwee n 1 and 4" )
El
62
63 e::.::.f pinp==2 :
€4 print (''Whose volume would you like to cal culate\n"
€5 "l . Cube\n"
E6 "2. Cuboid\n"
E7 "3.Cy:!.inder\!l"
ES " 4 . Exit")
€9 vinp=int (input ())
70 1f vinp == 1 :
71 5ide=f loat (input (" Please enter the s i de of the cube "))
ll V CUbf> rstde)
73 '?lif vinp == 2 :
74 length=float (input ("Plo.r1s<> enter the leng-ch of the cuboid" ))
75 width-fl oat (input ( "Please enter t he width of the cuboid" ))
76 height=float (input (" Please: enter the height of the cuboid" ))
71 v_cuboid(length,width,height)
78 el.l.f vinp==3 :
79 v_cyl ()
80 el1.f vinp==4 :
81 brea:k
82 P.ls~ :
83 print (" Please enter a number between 1 and 4 " )
84 <">llf pinp=c=3 :
85
86 bn.:<1k
87 El5E :
88 pri nt (" Pl ease enter a number between l and 3" )
8 s,
90
Output 1 :

We lcome to the program

what would you like to calculate


1 . Tota l surface Area
2 . Vo lume
3 . Exit
2
Whose volume would you like to calculate
l . Cube
2 . Cuboid
3 . Cyl inder
4 . E.x i t
3
Please Enter t he Radius of a cylinder : 25
Please Enter t h e Height of a cylinder : 90
The volume of the c y linder is 176625 . 0
---
Output 2:

Wel come to the program

what would you like to calculate


1 . Total surface Area
2 .Volume
3 . Exit
1
Whose Total surface Area would you like to calculate?
1 . Cube
2 . Cuboid
3 . Cyli nder
4 . Exit
2
Please enter the height of the cuboid1J
Please enter the width of the cuboid 5
Please enter the l ength of the cuboid2 . 5
The total surface area of the cuboid is 175 . 0
7: Write a program to read a string and print outthe
fo llowing:
1)No. of capital alphabets.
2)No. of small alphabets.

def alphabet_count (x) :


:count=:
scount=O
fr i in x:
l·r- -•-'14
i :c·o~p•·()
j.__
''
ccoun:+=l
. ·~ _,1s
e.1 ' 1ower () :
sc~~'.lt+=l
print ("~t~ number of ca;:tal ltc:er~ in s:rir.g arf",cccun:)
print (":he nlli!lbe: of s'.'la:l latte rs :n sering aren, scocnt)
str="7his is a Great day 11
-·n~abPt c~ur-(~tr)
' .
J.. 1,,
(l_ '-'•~
- ..,. V \i ,;.,

Output:

The number of capital letters in string are 2


The number of small latters in string are 13

8: Write a program to read a string and print it afterreplacing


each of it's capital alphabets by thecorrespond ing small
alphabets and each smallalphabet by it's corresponding capital
alphabet.
def str_interchange (x) :
p ri n t (x . swapcase())

str= " My name is Shat1rya 11

str interchange( str )


Output:

rnY NAME IS SHAURYA

9: Write a python program to read a file named"


article.txt",the following from the file ?
(i) length of the file (total characters in the file)
(ii) total alphabets
(iii) total digits
(iv)total spaces
(v)total special characters

articl<> file = op,;;n (r "F : \ N<>w folder\articl<" . txt" , " r +" l


text= a1ticle file . read()
spa e • o
d191ts 0
special char
alphabets,. O
l "'ngth=len (text)
print (" ThP tota l 1~ng ch of tha fll P lS" , l a ngth)
1 text :
1 . i:,al 1 ha() :
a1 1 hab t + l
i . isdigit() :
digits + 1
i. is space() :
s pac"' +- l

-~ec1al char+ 1
article file . cl ()
pr1nt (" Th<> t tal numb 1 t. alphab ts 1n t:11 ar " , alphabets)
print (" The total nunib i.: of d191ts in fil are " , dig its)
print ("The total nunlher of spaces 1n file are" , .space)
print ("The total number of special cha ract<> rs i n f ile ari=-" ,special_char)
Output:

The total length of the file is 211


The total number of alphabets in file are 125
The total number of digits in file are 29
The total number of spaces in file are 35
The total number of special characters in file are 22

10: Write a python program to read a file named "story.txt",


count and print the total words starting with "a" or "A".

de ~ count words () :
w=C'
wi th open (r"F : \New folder\st.ory .txt ") as f:
t line in f:
fr word ~n line.split():
i f (word{O] =="a" ;,;ord [OJ =="A" ):
print (word)
•,.;=w+l
print ('' ~a~a~ words starting with ' a' are '' ,w)
# function calling
count_words ()

Output:

AMSTERDAM
AZERBAIZAN
''
AUSTRALIA
america
atlantic
an
total words starting with ' a ' are 6
11 : Write a python program to read a file named "story.txt",
count and print the total lines starting with vowels in the file.

vowels="AE!OOaeiou"
'wl~r. open (r"F; \New foldc.r\story. txt") fp:
line = fp.readline()

_f (.:. ___ e '.CJ


in vowel~):
print (line)
cnt=cnt+l
_ine = fp.readline()
print ( "\nTh:;, total numb=:: of lines sta.rtir.g 1,;ith vo·,.--=-!s are ", cnt)

Output:

AMS,ERDAM is brilliant place to travel if you ' re planning tor a trip ot europe
AZERBAIZAN is the place to travel !f you're planning co visit some of the old ussr

australia & new zealand


america
atlantic ocean
end

The total nlJl!U)f'r of lines starting with vowels are : 7

12: Write a program to enter a string and to check if it is in


palindrome or not using loop.

def isPalindrorne (s):


!., = s[:: - 1}

J Driver code
s = input "Enter the string to be checked")
a~- i~P•lindrom~

.l t c,ns:
print( " Y~s, it is a palindrome'')
.. e :
print( " No, i t is not a palindrome·•>
Output:

Enter the string t o be checkedMADAM


Yes, it is a palindrome

13: Write a python program to read a file named "sample.txt",


count and print the following from the file.
a) The number of lines present in the file.
b) The number of words present in the file

with open ,r "F: \New folder\sample . txt " , ' r ' ) as fp:
count= 0
wcount=O
for line in fp:
if line i - "\n" :
count+- 1
word=line . split()
for i in word:
wcount+=l
print ( ' Total Lines ' , count)
print ( ' Total words : ' ,wcount)
'

Output:

rotal ::..i nes 7


<otal words : 16

14: Create a binary file with name and roll no. Search for a
given roll no and display the name ,if not found display the
appropriate error message.
i ln~ '~ - - pickle
tcreating the file and writing the data
f=open ( " rec'Jrds. dat.", "wb" )
pickle. dump ( [ "Wakil " , 1] , f)
pickle.dump([ "Tanish", 2], f)
pickle. dump ( [ " Priyashi " , 3] , f)
pickle .dtunp({ "Kanupriya " , 4], f)
pickle.dump(( "Aaheli " , 5), f)
f.close()
fopeining the file to read contents
f= open (" .records . dat", " rb" )
n=int (input ( "Enter the Roll Number: " ))
flag= Fa::..:2
whil• Tru :
try :
x=pickle.load(f)
if X[l]==n :
print ("Name : ", x [O])
flag= rue
XCPpt EOFError :
br l
if flag== False :
print ( "Thi s Roll Number does not exist" )

Output:

Enter the Roll Number : 2


Name : Tanish
15 :Write a random number generator that generates random
numbers between 1 and 100.

1 rt randorr,
print ("The randO!ll r.u.:ber between 1 and 100 is", rando:n.randin~(l,!0.))
I
Output:
--------------------------------. -- J.'1:..1 .... J...t'IJ.\..l.

The random number between 1 and 100 i s 74


1• • , I

16:Write a python program to increase the salary by Rs.2000/-


of the employee having empno is 2521 in a binary file emp.dat.

~ pickle
err.p={}
found- 1--=
~in=open ("emp. dat" , "rb+")
y:
Wh' p
rpos=fin . tell()
emp-pickle .!oad(fin)
if emp[ ' emp_no' ]=2521 :
fin.seek(rpos)
pickle.dump(emp,fin)
found= ru
EOFError :
1 tound==r 1 ° :
print ("S:>r!'y, no matching record :ound")

print ("Record(s) successfully updated")

tin . close()

Output:

IRecord(s) successfully updated

17:Write a program in python to read a text file and remove


all the lines that contain character 'a' in a file and write it to
another file.
fo - open ( ' hp . txt' , ' r ' )
fi= open ( ' writehp . txt ' , ' w' )
l=fo . readlines()
for i in 1 :
if ' a ' in i:
i=i.replace( ' a ' , '' )
fi.write(i)
fi . close()
fo . close ()

Output:
contents of file before program :
Harry PotterThere is a difference in all harry potter books
We can see it as harry grows
the books were written by J.K rowling

contents of file after executing the program:


Hrry PotterThere is difference in 11 hrry potter books
We en see its hrry grows

18: Write a program in python that swipe the first half of


sorted list with second half of list.

d J Swaphal fli st(list) :


.=len (list )
for i ~n range (l//2):
list [iJ, list [l//2+i]=list [l//2+i:, 1ist [il
print (list )

lst=eval (input ("li:nter the list"))


swaphalflist{lst)
Output:

Ent er the list [~, 2,3,4 , 5, 6, 7, 8, 9, lO]


[6, 7, 8, 9, 10 , 1, 2, 3, 4, 5]

19: Write a program in python to get item details (icode,


description and price) for multiple items from the user and
create a csv file by writing all items in one go .

.l.mp csv
~ ~
fh = open (":tems . csv" , "w" )
iwriter = csv .writer (fh)
ans= 'Y'
itemrec = [ [ 'Item_Code' , 'Description"' , 'Price' ])
print ("Enter item details'' )
whi~e ans == 'y ' :
icode = input ( "EnL-er Item code : " )
desc = input ("E:tter description : " )
price= float (input (''Enter price: '' ) )
itemrec . append([icode, desc , price])
ans= input ("Want to ente1 more items? (y/n) .. . " )
el e :
iwriter . ·.. r i t erows (itemrec)
print c" 0 ecords ~ritten successfully ." )
fh . clos,.. ()

Output:

Enter item details


Enter Item code : 35
Enter description : A.1 Apple
Enter price : 18
want to enter more items? (y/n) ... n
Records written successfully .
20:Write a program in python to find out the largest number
and sum of all numbers without using built in methods like
max() & sum().

myMax (tpll) :
max - tpll[O J
1. x tpll :
if x > max :
max = x
max
tplsum (tpll ):
total=
for ele L~ range ( ~, len (tpl l) +l) :
to~al = t ota: + tpll[el e]
r n tota.1.
t pl-eval (input (" Please enter the tuple" ) )
print ("Sum of all elements in given list : " , tplsum(tpl))
print ("Largest e l ement is :" , myMax(tpl))

Output:

Please enter the tuple (38 , 69 , 36 , 7~,93)


Sum of all elements in given list : 38
Larg_e st element is : 93

You might also like