0% found this document useful (0 votes)
49 views4 pages

Python Practical

The document contains programs to: 1) Calculate the distance between two points using the Pythagorean theorem. 2) Add two numbers as command line arguments and print the sum. 3) Define functions to compute the greatest common divisor (gcd) and least common multiple (lcm) of two numbers in one line each. 4) Perform matrix multiplication of two square matrices by taking user input for the rows and columns and individual elements of the matrices.

Uploaded by

Aaditya Maurya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views4 pages

Python Practical

The document contains programs to: 1) Calculate the distance between two points using the Pythagorean theorem. 2) Add two numbers as command line arguments and print the sum. 3) Define functions to compute the greatest common divisor (gcd) and least common multiple (lcm) of two numbers in one line each. 4) Perform matrix multiplication of two square matrices by taking user input for the rows and columns and individual elements of the matrices.

Uploaded by

Aaditya Maurya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Al.

PYTHON PROGRAMMING LAB MANU

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/

print "Thesum isC


Output:
GAdmin strator: C:Windows\system32cmd.exe

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

friw c DI1, *|1"


print c[iU),"\E",
print "

erint"Multiplication is NOT Possible" ri uhpluaho NOT

Dutput: Enter A Matrix Rows:3


husibe
Enter A Matrix Columns:3
Enter value:1
Enter value:2
Enter value:3
Enter value:4
Enter value:5
Enter value:6
Enter value:7
Enter value:8
Enter value:9 VC

a= [[1, 2, 3], [4,


5, 61, 17, 8, 9]]
Enter B Matrix Rows:3
Matrix Columns:3
Enter B
Entervalue:4
Enter value:5
Enter value:6 16
Enter value:1
Enter value:2
Enter value:3
4 2 + 24
Enter value:7

Enter value:8
412419
Enter value:9

2, 3], [7,
8, 9]]
5 6
93
147

You might also like