I was working on a Python project where I needed to calculate the area of a triangle based on user input. At first, it seemed like a simple problem, but as I explored different approaches, I realized there are multiple ways to solve it efficiently in Python.
In this tutorial, I’ll walk you through two simple methods to find the area of a triangle in Python, one using the base and height formula and another using Heron’s formula.
I’ll also share my personal tips from over a decade of working with Python so you can understand not just how to do it, but why each method works.
Understand the Formula for the Area of a Triangle
Before we jump into the Python code, let’s quickly recap the math behind it.
The area of a triangle can be calculated in two common ways:
- Using Base and Height:
[
\text{Area} = \frac{1}{2} \times \text{base} \times \text{height}
] - Using Heron’s Formula (when all sides are known):
[
s = \frac{a + b + c}{2}
]
[
\text{Area} = \sqrt{s (s – a)(s – b)(s – c)}
]
Both methods are easy to implement in Python, and I’ll show you how to code each one step by step.
Method 1 – Find the Area of a Triangle in Python Using Base and Height
This is the easy way to calculate the area of a triangle in Python. It’s perfect when you already know the base and height values. Here’s how I usually do it in my projects.
We’ll take user input for the base and height, calculate the area using the formula, and display the result.
Python Code Example
# Taking input from the user
base = float(input("Enter the base of the triangle in inches: "))
height = float(input("Enter the height of the triangle in inches: "))
# Calculating the area
area = 0.5 * base * height
# Displaying the result
print(f"The area of the triangle is {area} square inches.")In this Python program, I used the float() function to ensure the inputs are treated as decimal numbers. This allows for more accurate calculations, especially when dealing with real-world measurements.
Output
Enter the base of the triangle in inches: 46
Enter the height of the triangle in inches: 56
The area of the triangle is 1288.0 square inches.You can see the output in the screenshot below.

In many real-world scenarios, such as geometry problems or engineering tasks, you might only know the lengths of the three sides. That’s where Heron’s formula comes in handy.
Method 2 – Find the Area of a Triangle in Python Using Heron’s Formula
When you know all three sides of a triangle, you can use Heron’s formula to calculate the area without needing the height. This method is slightly more mathematical, but Python makes it easy to handle with its built-in math module.
Python Code Example
import math
# Taking input for three sides
a = float(input("Enter the length of side A in inches: "))
b = float(input("Enter the length of side B in inches: "))
c = float(input("Enter the length of side C in inches: "))
# Calculating the semi-perimeter
s = (a + b + c) / 2
# Calculating the area using Heron's formula
area = math.sqrt(s * (s - a) * (s - b) * (s - c))
# Displaying the result
print(f"The area of the triangle is {area:.2f} square inches.")The math.sqrt() function is used to calculate the square root. I also formatted the output to two decimal places for cleaner readability.
Output
Enter the length of side A in inches: 5
Enter the length of side B in inches: 6
Enter the length of side C in inches: 7
The area of the triangle is 14.70 square inches.You can see the output in the screenshot below.

You can see how Python’s simplicity allows us to perform complex mathematical operations with just a few lines of code.
Method 3 – Find the Area of a Triangle in Python Using Coordinates
Sometimes, you might know the coordinates of the triangle’s vertices instead of side lengths. In such cases, you can calculate the area using the determinant formula.
We’ll take three coordinate points (x₁, y₁), (x₂, y₂), and (x₃, y₃), then apply the formula:
[
\text{Area} = \frac{1}{2} |x_1(y_2 – y_3) + x_2(y_3 – y_1) + x_3(y_1 – y_2)|
]
Python Code Example
# Taking input for coordinates
x1, y1 = map(float, input("Enter coordinates of first vertex (x1 y1): ").split())
x2, y2 = map(float, input("Enter coordinates of second vertex (x2 y2): ").split())
x3, y3 = map(float, input("Enter coordinates of third vertex (x3 y3): ").split())
# Calculating the area using the determinant formula
area = abs((x1*(y2 - y3) + x2*(y3 - y1) + x3*(y1 - y2)) / 2.0)
# Displaying the result
print(f"The area of the triangle is {area:.2f} square units.")Here, I used the abs() function to ensure the result is always positive since the area cannot be negative.
This method is particularly powerful when working with coordinate geometry or when building Python applications that process spatial data.
Bonus Tip – Use a Python Function for Reusability
If you need to calculate the area of multiple triangles, it’s better to wrap your logic in a reusable function.
This makes your code cleaner and easier to maintain.
Python Code Example
import math
def triangle_area(a, b, c):
"""Returns the area of a triangle given three sides."""
s = (a + b + c) / 2
return math.sqrt(s * (s - a) * (s - b) * (s - c))
# Example usage
print(triangle_area(5, 6, 7))You can see the output in the screenshot below.

This function-based approach is great for larger Python projects where you might need to perform multiple area calculations dynamically.
Common Mistakes to Avoid
- Not converting input to float: Always convert user input to a float to handle decimal values correctly.
- Invalid triangle sides: Ensure the sum of any two sides is greater than the third side; otherwise, the triangle is invalid.
- Forgetting to import the math module: If you use math.sqrt(), make sure to import the module at the top.
Real-Life Example (U.S. Context)
Let’s say you’re working on a Python program for a construction company in Texas. The engineers need to calculate the area of triangular roof sections to estimate material requirements.
By using Heron’s formula method in Python, you can automate these calculations instantly, saving hours of manual computation and ensuring accuracy.
Conclusion
Both methods, base-height and Heron’s formula, are practical and easy to implement in Python.
If you know the base and height, go with the first method. But if you only have the side lengths, Heron’s formula is your best friend.
Python makes mathematical computations like this not only simple but also extremely readable. With just a few lines of code, you can solve geometry problems that would otherwise take several steps by hand.
You may also like to read:
- For loop vs while loop in Python
- Python For Loop with Index
- Use Python While with Assignment
- Python While Multiple Conditions

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.