0% found this document useful (0 votes)
21 views

Python Lab Work

The document contains 7 code examples demonstrating various Python functions: 1) A function to calculate the area of a triangle using Heron's formula. 2) A function to swap the values of two numbers. 3) A function to calculate the length of the hypotenuse of a right triangle. 4) Functions to calculate permutations and combinations of numbers. 5) Functions to determine if a number is an Armstrong number based on the sum of its digit cubes. 6) A function to determine if a number contains all even, all odd, or both even and odd digits. 7) A function to check if a number is a "perfect" number by summing its proper

Uploaded by

hjai8015
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Python Lab Work

The document contains 7 code examples demonstrating various Python functions: 1) A function to calculate the area of a triangle using Heron's formula. 2) A function to swap the values of two numbers. 3) A function to calculate the length of the hypotenuse of a right triangle. 4) Functions to calculate permutations and combinations of numbers. 5) Functions to determine if a number is an Armstrong number based on the sum of its digit cubes. 6) A function to determine if a number contains all even, all odd, or both even and odd digits. 7) A function to check if a number is a "perfect" number by summing its proper

Uploaded by

hjai8015
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1)Write a function to find the area of a traingle.

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)

Enter the side-1: 3.5


Enter the side-2: 6.5
Enter the side-3: 4.5
The area of the required traingle is : 7.488272080393447

2)Write a function to swap the contents of 2 numbers

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))

Enter the base of a right-angled traingle: 3


Enter the height of a right-angled traingle: 4
The hypotenuse of the rigth-angled traingle is: 5.0
4)Analyse the following program:

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")

Enter a number: 1345235


The number contains both even and odd 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

You might also like