How to calculate area of triangle in Python?

Hello readers, today we are going to discuss how the area of a triangle is calculated in Python.

Calculating Area of Triangle in Python

Case1:

If the length of the base and height of the triangle is known:

length = a , height = h

Then

area = (1/2)*a*h

Code:

length = 10
height = 8
area = (1/2)*length*height
print(f'Area of triangle is {area}')

Output:

Area of triangle is 40.0

Case2:

If the length of each side of the triangle is known

Lets consider a,b,c as sides of the triangle

semi_perimeter = (a+b+c)/2

Then

area = sqrt(s*(s-a)*(s-b)*(s-c))

Code:

a = 3
b = 4
c = 5
s = (a+b+c)/2  #semi_perimeter
area = (s*(s-a)*(s-b)*(s-c))**0.5
print(f'Area of the triangle is {area}')

Output:

Area of the triangle is 6.0

Case3:

If it is known that the triangle is an equilateral triangle and the length of the side is given.

Let length of the side of a triangle  = a

area = (sqrt(3)*a*a)/4

Code:

a = 6
area = (((3)**0.5)*a*a)/4
print(area)

Output:

15.588457268119896

Python Program to Calculate Area of any Triangle using its coordinates

Thanks for reading!!!

Leave a Reply

Your email address will not be published.