Python Program to Calculate the Area of a Triangle – 2024

Hey guys, in this blog we will see a Python Program to Calculate the Area of a Triangle using 2 different formulas.

Example 1: Using the Base and Height of the Triangle

# Python Program to Calculate the Area of a Triangle

# base and height of triangle
base = 12
height = 12

#calculating area of the triangle
area_of_triangle = 0.5*base*height

print(f'The area of a rectangle with base {base} and height {height} = {area_of_triangle}')

Output

The area of a rectangle with base 12 and height 12 = 72.0

Here we have used the simple formula for calculating the area of a triangle which is:

Area of Triangle = 0.5*base*height.

Example 2: Using 3 sides of a Triangle

# Python Program to Calculate the Area of a Triangle

# base and height of triangle
side1 = 4
side2 = 13
side3 = 15

#calculating area of the triangle
s = (side1+side2+side3)/2

area_of_triangle = (s*(s-side1)*(s-side2)*(s-side3))**0.5

print(f'The area of a rectangle with sides {side1}, {side2} and {side3} = {area_of_triangle}')

Output

The area of a rectangle with sides 4, 13 and 15 = 24.0

Here we have used Heron’s formula to calculate the area of the triangle. The formula for it is given below:

Area of Triangle = (s(s-side1)(s-side2)(s-side3))*0.5

Check out our other python programming examples

Abhishek Sharma
Abhishek Sharma

Started my Data Science journey in my 2nd year of college and since then continuously into it because of the magical powers of ML and continuously doing projects in almost every domain of AI like ML, DL, CV, NLP.

Articles: 517

Leave a Reply

Your email address will not be published. Required fields are marked *