# Python program to find area of a triangle

In this python tutorial, you will learn how to find area of a triangle in Python.

There are three ways to find the area of the triangle in Python, which is shown below.

• Using the formula: 0.5 * base * height
• Using Heron’s formula
• Using the class

## Find area of a triangle in Python

Here we will discuss each method of finding area of a triangle in Python using an example. And let us start with the first method of using the formula.

### Method-1: Find area of a triangle in Python using the formula: 0.5 * base * height

In this method, we find the area of a triangle using the formula 0.5 * base * height. We define a function triangle_area which takes in the base and height of the triangle as input and returns the area. We then input the values of the base and height and print the result.

``````# Calculating the area of a triangle using the formula: 0.5 * base * height
def triangle_area(base, height):
return 0.5 * base * height

# Inputting the values of base and height
base = float(input("Enter the base of the triangle: "))
height = float(input("Enter the height of the triangle: "))

# Printing the result
print("The area of the triangle is", triangle_area(base, height))``````
``````Output: Enter the base of the triangle: 3
Enter the height of the triangle: 6
The area of the triangle is 9.0``````

### Method-2: Find area of a triangle in Python using Heron’s formula

In this method, we find the area of a triangle using Heron’s formula. Heron’s formula calculates the area of a triangle given its three side lengths.

We import the math module and define a function triangle_area_heron which takes in the three side lengths of the triangle as input and returns the area. We then input the values of the three sides and print the result.

``````# Calculating the area of a triangle using Heron's formula
import math

def triangle_area_heron(a, b, c):
s = (a + b + c) / 2
area = math.sqrt(s * (s - a) * (s - b) * (s - c))
return area

# Inputting the values of the three sides of the triangle
a = float(input("Enter the first side length of the triangle: "))
b = float(input("Enter the second side length of the triangle: "))
c = float(input("Enter the third side length of the triangle: "))

# Printing the result
print("The area of the triangle is", triangle_area_heron(a, b, c))
``````

Once we execute the Python program, we will get the following result.

### Method-3: Find area of a triangle in Python using the class

Here we will understand how to create a Python Class and use that class to calculate the area of a triangle.

``````# Class `sides` for defining the sides of a triangle
class sides:
# Constructor to initialize the sides of a triangle
def __init__(self,a, b, c):
self.a = a
self.b = b
self.c = c

# Class `A` which inherits class `sides`
class A(sides):
# Method to calculate the area of a triangle
def area(self):
# Calculate semi-perimeter of the triangle
s = (self.a + self.b + self.c)/2
# Calculate the area of the triangle using Heron's formula
return (s*(s-self.a)*(s-self.b)*(s-self.c)) ** 0.5

# Create an object of class `A`
obj = A(10,12,14)

# Print the area of the triangle
print("Area of triangle : {}".format(obj.area()))
``````

The above code defines a class side that holds the 3 sides of a triangle as instance variables a, b, and c.

• It then creates a derived class A that calculates the area of the triangle using Heron’s formula. An object obj of class A is then created with sides 10, 12, and 14, and the area of the triangle is printed.
``Output: Area of triangle : 58.787753826796276``

You may also like to read the following Python tutorials.

## Conclusion

In this Python tutorial, we have learned how to create the Python program to find the area of a triangle using the following 3 methods.

• Using the formula 0.5 * base * height
• Using Heron’s formula
• Using the class