Python Practical
Python Practical
Python Practical
Exercise 2 Operationss
distance between two points
a program to compute
a)
Write
input f r o m the
user
taking
(Pythagorean Theorem)
Progam:
math
a-iput e Va lue ")
Enter b value
b=input
cmath. Sqrt (a* "*2+b**2)
The Resuit i s " ,C
Cel 0
a value 12
Enter
b value 14
Enter
is 18.4390889146
The Result
2 numbers as command li
b) Writea program add.py that takes
arguments and prints its sum.
Program:
import sys NCIJ)-
a=int(sys.argy[1])
b=int(sys.argv[2]) b
C-atb cafb/
CNPython272python argsun.py 9 27
The sun is 36
C:NPyt hon27
Write function to
compute gcd, lcm of two numbers. Each
function shouldn't exceed one line.
def gcd(x,y): aLx, y):
return x if y==0 else gcd(y,x%y)
def Icm(x,y): humx X 0
return (x*y)//gcd(x,y) ALY, xY)
print "gcd is",gcd(54,24) cm X, Y)
print "lcm is",lcm(54,24)
Output:
gcd is 6
Icm is 216
2 4 4 , Y0)
A 4 - , 6)
T.MOTHILAL, ASST.PROFF d C6 Page
program to perform multiplication of two square
c) Write a
matrices
Program:
A Mahy Rroe
c1=input("Enter A Matrix Rows:") T1 p urluPntu A MamA
an
Columns:") c l : mpu
B y
Cnput"EnterA Matrix
ä=[] nmga (1):
aPpnAC)
for i in range(r1):
a.append([])
Eula
for i in range(r1): r li,uupur (
for j in range(cl): A
ali.insert(G,input("Enter value: ")
print aE a Eut A
B MA
r2=input("Enter B Matrix Rows:") C pu
c2=input("Enter B Matrix Columns:")
ALmg (3)
b=[ I)
b NPp n M I
for i in range(r2): w 6MAGe(E)
b.append([I) svusmg {C»):
for i in range(r2): Cjimtuur l,nupur( E
for j in range(c2):
b[i].insert(j,input("Enter value:"))
print "b",b Rmint b, b
ifcl==r2
for i in rangetrl CPpena[CI)
T.append([]),
for i in rangelr1):
for j in range(c2);
x=9
Page 18
T.MOTHILAL, ASST.PROF
yTHON PROGRAMMING LAB
MANUAL.
for k in W Y'ngC)
range(c1):
Xt=a[NIk*b{}JUI
c[il.inserti.) Ctrjmueur 1,3)
for i in range(ri):
forjin range(c1); v
Enter value:8
412419
Enter value:9
2, 3], [7,
8, 9]]
5 6
93
147