How to Calculate Area of a Triangle in Python


In this example we will show how to calculate the area of a triangle with three given sides in Python.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

import math

print("Please input the lenght of three sides: ")
a = []
for i in range(0, 3):
    b = int(input("Length: "))
    a.append(b)
s = sum(a) / 2
area = math.sqrt(s * (s - a[0]) * (s - a[1]) * (s - a[2]))
print("The area of the triangle is: ", round(area,2))

Output:

Please input the lenght of three sides: 
Length: 3
Length: 4
Length: 5
The area of the triangle is:  6.0

Please input the lenght of three sides: 
Length: 8
Length: 9
Length: 12
The area of the triangle is:  36.0
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments