Python Program to Find the Area of a Triangle
Beginner · Basics
The area of a triangle is half its base times its height, where the height is measured at right angles to the base. When you only know the three sides, Heron's formula gives the area instead, and this page shows both ways.
Program
Base: 10 cm Height: 6 cm Area of the triangle: 30.0 square cm
How it works
- The formula is area = 1/2 x base x height, written in Python as
0.5 * base * height. - The height must be the straight-up distance from the base to the opposite corner, not the length of a sloping side.
- Multiplying by
0.5always gives a decimal, so the area prints as 30.0.
Other ways to write it
Using Heron's formula (three sides)
Sides 5, 6, 7: semi-perimeter s = 9.0 Area = 14.70 square cm
Heron's formula needs only the three sides: area = square root of s(s - a)(s - b)(s - c), where s is half the perimeter. The check first makes sure each pair of sides is longer than the third, or there is no triangle.
From the corners on a graph
Area of the triangle with these corners: 6.0 square units
When the corners are points on a graph, this coordinate formula gives the area directly, and abs() keeps it positive whichever order the corners are listed in.
Try it yourself
Use Heron's formula on the sides 3, 4 and 5 and check that it matches 1/2 x 3 x 4.
Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.
Related programs
- Find the Area of a Circle
- Find the Area and Perimeter of a Rectangle
- Check Whether a Triangle Is Equilateral, Isosceles or Scalene
- Find the Distance Between Two Points
- Display a Welcome Message
- Find the Larger of Two Numbers
- Find the Largest of Three Numbers
- Check a Leap Year
- Check Whether a Number Is Even or Odd
- Swap Two Variables
All Python programs · CBSE class 12 practical file · Python exercises