Table of contents
1.
Introduction
2.
Mathematical Formula for the Area of a Triangle
3.
Example
4.
Program to Calculate the Area of a Triangle in Python
4.1.
Python
5.
Area of a Triangle in Python Using Heron’s Formula
6.
Area of a Right-Angled Triangle in Python
7.
Frequently Asked Questions
7.1.
What built-in Python functions can be useful for calculating the area of a triangle?
7.2.
How can Python handle input from the user to get the dimensions of the triangle?
7.3.
How can Python's error-handling features be useful when calculating the area of a triangle?
8.
Conclusion
Last Updated: Jan 7, 2025
Easy

Area of Triangle in Python

Author Rinki Deka
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The area of a triangle is a fundamental concept in geometry. It represents the amount of space enclosed within the three sides of a triangle. Calculating the area of a triangle is an essential skill for various mathematical & real-life applications. 

Python Program to Calculate the Area of a Triangle

In this article, we will learn how to calculate the area of a triangle using Python programming. We will cover the mathematical formula, look into code examples, & explain the concepts step by step.

Mathematical Formula for the Area of a Triangle

The area of a triangle can be calculated using a simple formula when the base and the height are known. The formula is:

Area=1/2×base×height

This formula tells us that the area is half the product of the base and the height of the triangle. The base of the triangle can be any one of its sides, and the height is the perpendicular distance from the chosen base to the opposite vertex.

Example

To illustrate, let’s consider a triangle with a base of 5 units and a height of 4 units. Using our formula:

Area=1/2×5×4=10 square units


Explanation

This calculation shows that if you multiply the base of the triangle by the height and then divide by two, you will get the area in square units. It’s a straightforward method to calculate the space inside the triangle.

Program to Calculate the Area of a Triangle in Python

  • Python

Python

# Function to calculate the area of a triangle
def calculate_triangle_area(base, height):
return 0.5 * base * height

# Inputs for the base and height of the triangle
base = float(input("Enter the base of the triangle (in units): "))
height = float(input("Enter the height of the triangle (in units): "))

# Calculating the area using the function
area = calculate_triangle_area(base, height)

# Displaying the result
print(f"The area of the triangle is {area} square units.")
You can also try this code with Online Python Compiler
Run Code


Output

Enter the base of the triangle (in units): 5
Enter the height of the triangle (in units): 3
The area of the triangle is 7.5 square units.


Explanation

In this Python script:

  • We define a function called calculate_triangle_area that takes two parameters: base and height. The function returns the calculated area using the formula Area=0.5×base×height.
     
  • The user is prompted to input the values for the base and height of the triangle, which are read as floating-point numbers to accommodate decimals.
     
  • We call the calculate_triangle_area function with the user-provided base and height and store the result in the variable area.
     
  • Finally, the area of the triangle is printed to the console, providing a clear and direct output of the calculation.

Area of a Triangle in Python Using Heron’s Formula

Heron’s Formula:
For a triangle with sides a, b, and c, Heron’s formula calculates the area as follows:

  1. First, find the semi-perimeter
s=2a+b+c

 

Then, use the formula for area: 

Area=s×(s−a)×(s−b)×(s−c)​


Code Implementation:

import math
def herons_formula_area(a, b, c):
   # Calculate the semi-perimeter
   s = (a + b + c) / 2
   # Calculate the area using Heron's formula
   area = math.sqrt(s * (s - a) * (s - b) * (s - c))
   return area
# Example usage
a, b, c = 5, 6, 7
print("Area of the triangle using Heron's formula:", herons_formula_area(a, b, c))
You can also try this code with Online Python Compiler
Run Code


Output

Area of the triangle using Heron's formula: 14.696938456699069
Area of a Right-Angled Triangle in Python

Area of a Right-Angled Triangle in Python

The area of a right-angled triangle with base b and height h is calculated as:

Area=½* ​×b×h

Code Implementation:

def right_triangle_area(base, height):
   # Calculate the area of the right-angled triangle
   area = 0.5 * base * height
   return area
# Example usage
base = 5
height = 12
print("Area of the right-angled triangle:", right_triangle_area(base, height))
You can also try this code with Online Python Compiler
Run Code


Output

Area of the right-angled triangle: 30.0

Frequently Asked Questions

What built-in Python functions can be useful for calculating the area of a triangle?

Python’s math.sqrt() is useful for applying Heron’s formula by calculating the square root. Additionally, input() collects user input, and float() converts values for precise area calculations.

How can Python handle input from the user to get the dimensions of the triangle?

Python uses input() to receive user-entered values, allowing users to input dimensions like side lengths or base and height. Converting inputs to float() ensures accurate calculations, especially for non-integer dimensions.

How can Python's error-handling features be useful when calculating the area of a triangle?

Python’s try-except blocks catch errors, such as invalid inputs or non-positive dimensions, that could otherwise cause calculation failures. This helps handle input issues gracefully, prompting users to enter valid values.

Conclusion

This article discussed calculating a triangle’s area using a simple mathematical formula and implementing it in Python. We demonstrated a straightforward method that uses the triangle’s base and height, along with a Python program that efficiently calculates the area from user-provided dimensions.

Live masterclass