Professional Documents
Culture Documents
PRACTICAL FILE
Shaurya Chauhan
XII B
CERTIFICATE
Principal
Date:
1:Write a python script to take input for 3 numbers,check
and print the largest number.
Output:
•
Enter the first number23
Enter the second number46
Enter the third number35
The largest number is 46
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.
Output:
4 I.\JIJ J
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:
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 :
Output:
-~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:
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()
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
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:
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:
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:
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.
~ 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")
tin . close()
Output:
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
.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:
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: