# Python Program to Calculate the Area of a Triangle

In this post, we will learn how to calculate the area of a triangle using Python Programming language.

One of the easiest way to calculate the area of the triangle is by using the Heronâ€™s formula:

Area of Triangle = (s * (s – a) * (s – b) * (s – c)) ** 0.5, where a, b and c are the three sides of the triangle and s is the semi-perimeter of the triangle.

We will be computing the area of the triangle using the following methods:

1. Using Standard Method
2. Using Functions
3. Using Math File
4. Area of Right Angled Triangle

So, without further ado, letâ€™s begin this tutorial.

Contents

## Python Program to Calculate the Area of a Triangle

```# Python Program to Calculate the Area of a Triangle

a = float(input("Enter the first side of the triangle: "))
b = float(input("Enter the second side of the triangle: "))
c = float(input("Enter the third side of the triangle: "))

# Calculating the semi-perimeter
s = (a + b + c) / 2

# Calculating area
area = (s * (s - a) * (s - b) * (s - c)) ** 0.5

# Displaying output
print("Area of triangle: ", area)
```

Output

``````Enter the first side of the triangle: 3
Enter the second side of the triangle: 4
Enter the third side of the triangle: 5
Area of triangle:  6.0
``````

## How Does This Program Work ?

```a = float(input("Enter the first side of the triangle: "))
b = float(input("Enter the second side of the triangle: "))
c = float(input("Enter the third side of the triangle: "))
```

In this program, the user is asked to enter the three sides of the triangle.

```# Calculating the semi-perimeter
s = (a + b + c) / 2
```

Then, we calculate the semi-perimeter of the triangle.

```# Calculating area
area = (s * (s - a) * (s - b) * (s - c)) ** 0.5
```

Area of the triangle is calculated using the Heronâ€™s Formula.

```# Displaying output
print("Area of triangle: ", area)
```

Finally, the result is displayed on the screen using print() function.

## Python Program to Calculate the Area of a Triangle Using Functions

```# Python Program to Calculate the Area of a Triangle Using Functions
def area(a, b, c):
s = (a + b + c) / 2

# Calculating area
area = (s * (s - a) * (s - b) * (s - c)) ** 0.5
print("Area of the triangle: %.2f" %area)

a = float(input("Enter the first side of the triangle: "))
b = float(input("Enter the second side of the triangle: "))
c = float(input("Enter the third side of the triangle: "))

# Calling out the function
area(a, b, c)
```

Output

``````Enter the first side of the triangle: 10
Enter the second side of the triangle: 12
Enter the third side of the triangle: 15
Area of the triangle: 59.81
``````

## Python Program to Calculate the Area of a Triangle Using Math File

```# Python Program to Calculate the Area of a Triangle Using Math File
import math

a = float(input("Enter the first side of the triangle: "))
b = float(input("Enter the second side of the triangle: "))
c = float(input("Enter the third side of the triangle: "))

if (a < 0 or b < 0 or c < 0 or (a + b <= c) or (a + c <= b) or (b + c <= a)):
print("Triangle is not possible")

else:

# Calculating the semi-perimeter
s = (a + b + c) / 2

# Calculating area
area = math.sqrt(s * (s - a) * (s - b) * (s - c))

# Displaying the output
print("Area of the triangle: %.2f" %area)
```

Output 1

``````Enter the first side of the triangle: 3
Enter the second side of the triangle: 6
Enter the third side of the triangle: 10
Triangle is not possible
``````

Output 2

``````Enter the first side of the triangle: 5
Enter the second side of the triangle: 5
Enter the third side of the triangle: 5
Area of the triangle: 10.83
``````

## Python Program to Calculate the Area of a Right Angled Triangle

```# Python Program to Calculate the Area of a Triangle

base = float(input("Enter the base of the triangle: "));
height = float(input("Enter the height of the triangle: "));

# Computing the area of triangle
area = (1/2) * base * height;

# Displaying output
print("Area of triangle: ", area)
```

Output

``````Enter the base of the triangle: 5
Enter the height of the triangle: 12
Area of triangle:  30.0
``````

## Conclusion

I hope after going through this post, you understand how to calculate the area of a triangle using Python Programming language.

If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to help you.