Program to Calculate the Area of a Triangle in Python
Below are some of the examples by which we can understand how we can calculate the area of a triangle in Python:
Area of a Triangle in Python Using Heron’s Formula
In this example, we define a function for calculating the area of triangle at first where we use the formula and return the answer. In the driver function we pass the values as sides of triangle and call the function for calculating the area.
Heron’s Formula
Area of Triangle = (s(s-a)*(s-b)*(s-c) )** 0.5
s = a + b + c/2
Python3
def areaTriangle(a, b, c): s = (a + b + c) / 2 return (s * (s - a) * (s - b) * (s - c)) * * 0.5 a = 7 b = 8 c = 9 print ( 'Area = {:.2f}' . format (areaTriangle(a, b, c))) |
Area = 26.83
Area of a Right-Angled Triangle in Python
In this example, we calculate the area of a right angled triangle whose base and height is given. At first, we define a function to calculate the area where we write the formula to calculate the area and then return the answer. In the driver function we pass the values of base and height of triangle.
Formula
Area of Triangle: 1/2 * b * h
Python3
def areaTriangle(b, h): return 0.5 * b * h base = 8 height = 9 print ( 'Area = {:.2f}' . format (areaTriangle(base, height))) |
Area = 36.00
Python Program to Calculate the Area of a Triangle
A triangle is a closed-shaped two-dimensional polygon having three sides and three corners. The corners are called vertices and the sides are called edges. In this article, we will see how we can calculate the area of a triangle in Python.
Example:
Input: 5 6 7
Output: Area = 14.70