Python Lab Work
Python Lab Work
def area(a,b,c):
s=(a+b+c)/2
k=s*(s-a)*(s-b)*(s-c)
return k**0.5
a=float(input("Enter the side-1: "))
b=float(input("Enter the side-2: "))
c=float(input("Enter the side-3: "))
Area=area(a,b,c)
print("The area of the required traingle is : ",Area)
def swap(a,b):
t=a
a=b
b=t
return a,b
a=int(input("Enter a number: "))
b=int(input("Enter another number: "))
print("Numbers before swapping: ",a," ",b)
a,b=swap(a,b)
print("Numbers after swapping: ",a," ",b)
Enter a number: 3
Enter another number: 2
Numbers before swapping: 3 2
Numbers after swapping: 2 3
3)Write a function to find the length of the third side of a traingle if the length of the
remaining sides are given.
def hypotenuse(a,b):
c=(a**2+b**2)**0.5
return c
a=int(input("Enter the base of a right-angled traingle: "))
b=int(input("Enter the height of a right-angled traingle: "))
print("The hypotenuse of the rigth-angled traingle is:
",hypotenuse(a,b))
import operator as op
#Returns the factorial of a number
def factorial(num):
if num == 1:
return num
return num * factorial(num-1)
#Returns the permutations of a number
def permutation(n,r):
return int(factorial(n) / factorial(n-r))
#Returns the combinations of a number
def combination(n, r):
return int(factorial(n) / (factorial(r) * factorial(n-r)))
print("Permutation: ",permutation(15,4))
print("Combination: ", combination(15,4))
Permutation: 32760
Combination: 1365
5)Write a function cubesum() that accepts an integer and returns the sum of the cubes of
individual digits of that number. Use this function to make functions PrintArmstrong() and
isArmstrong() to print Armstrong numbers and to find whether it is an Armstrong number.
s=0
def cubesum(n):
if(n==0):
return
global s
d=n%10
s+=d**3
cubesum(n//10)
return s
n=int(input("Enter a number: "))
t=n
a=cubesum(n)
print("The cube sum of it's digit: ",s)
def PrintArmstrong(a):
if(t==s):
return True
return False
def isArmstrong(a):
if(a==s):
return "it is an armstrong number"
else:
return "it is not an armstrong number"
if(PrintArmstrong(t)):
print(t)
print(isArmstrong(t))
Enter a number: 153
The cube sum of it's digit: 153
153
it is an armstrong number
6)Write a function to find if all the digits in a given number are odd or even, or both.
l1=[]
l2=[]
def digit(n):
if n==0:
return
global l1,l2
d=n%10
if(d%2==0):
l1.append(d)
else:
l2.append(d)
digit(n//10)
n=int(input("Enter a number: "))
digit(n)
if(len(l1)!=0 and len(l2)!=0):
print("The number contains both even and odd digits")
elif(len(l1)==0):
print("The number contains only odd digits")
else:
print("The number contains only even digits")
7)A number is called perfect if the sum of proper divisors of that number is equal to the
number. For example, 28 is a perfect number since 1+2+4+7+14=28. Write a function to
evaluate if the given number is perfect or not.
def factors(n):
s=0
for i in range(1,n):
if(n%i==0):
s+=i
else:
pass
return s
n=int(input("Enter a number: "))
t=n
k=factors(n)
if(k==n):
print("It is a perfect number")
else:
print("It is not a perfect number")
Enter a number: 28
It is a perfect number