Python Calculate the Area of a Triangle with Example

In this snippet, we will learn to calculate the area of a triangle in Python.

Formula of Area of a Triangle

Let's see the formula:

# find the semi-perimeter of a triangle
s = (a+b+c)/2

# formula for the area of a triangle
area = (s*(s-a)*(s-b)*(s-c))**0.5

Find Area of a Triangle

Now let's see an example:

a = 4
b = 5
c = 6

# calculate the semi-perimeter of a triangle
s = (a + b + c) / 2

# calculate the area
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)

The output:

The area of the triangle is 9.92

You can also run the program in our IDE.


Software Engineer | Ethical Hacker & Cybersecurity...

Md Obydullah is a software engineer and full stack developer specialist at Laravel, Django, Vue.js, Node.js, Android, Linux Server, and Ethichal Hacking.