How to Calculate the Area of a Triangle using Python?


Calculating the area of a triangle is a formula that you can easily implement in python. If you have the base and height of the triangle, you can use the following code to get the area of the triangle,

def get_area(base, height):
   return 0.5 * base * height
print(get_area(10, 15))

This will give the output:

75

If you have the sides of the triangle, you can use herons formula to get the area. For example,

def get_area(a, b, c):
   s = (a+b+c)/2
   return (s*(s-a)*(s-b)*(s-c)) ** 0.5
print(get_area(10, 15, 10))

This will give the output:

49.607837082461074

Lakshmi Srinivas
Lakshmi Srinivas

Programmer / Analyst / Technician

Updated on: 17-Jun-2020

934 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements