Python Program to Calculate the Area of a Triangle – 2022

Machine Learning Projects

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

Table of Contents

Example 1: Using Base and Height of 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 of calculating the area of a triangle which is 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.

Check out our other python programming examples

Leave a Comment

Your email address will not be published.