Python Program to Calculate the Area of a Triangle

Written: August 22, 2026

Two common classroom versions exist: area from base and height (½·b·h), and area from three sides with Heron’s formula. Both are short and good input-validation practice.

Base and height

base = float(input("Base: "))
height = float(input("Height: "))
area = 0.5 * base * height
print(f"Area = {area:.2f}")

Heron’s formula

import math

a = float(input("Side a: "))
b = float(input("Side b: "))
c = float(input("Side c: "))
s = (a + b + c) / 2
area = math.sqrt(s * (s - a) * (s - b) * (s - c))
print(f"Area = {area:.2f}")

Reject inputs that cannot form a triangle (triangle inequality) before calling sqrt, or you will hit a math domain error.

Keep learning

If this walkthrough on python triangle area helped, open the code again and change one input or assumption. Small experiments beat rereading the same example.

Want more step-by-step tutorials like this? Browse blog.xqa.io — and tell us which topic you want next.

Advertisement

Previous Article

C Program: Alternating Sign Power / Factorial Series

Next Article

Simple Python Calculator Code

Write a Comment

Leave a Comment

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

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨