Today, we will see a Python program to calculate the area of a triangle.

You can calculate the area of a triangle by numerous formulas depending on the metrics you know. For example, if you know the three sides of a triangle, then you can easily calculate are of a triangle using Heron’s formula.

We will see the Python code for each and every scenario to calculate the area of a triangle. To understand the theory, visit Mathisfun.

In case, you are looking for books to learn Python, then check out this recommended list.

## Calculating the area of a triangle with base and height

To calculate the area of a triangle whose base and height are known, you can use this formula = (Base x Height)/2.

Here’s the Python program to calculate the area:

base = input('Enter the base of the triangle:'); # Taking base input from user base = float(base); # Converting string input data to float type height = input('Enter the height of the triangle'); # Taking height input user height = float(height); # Converting string input data to float type Area = float((base * height) / 2); # Calculateing the area of the triangle using (Base * Height)/2 print('Area of triangle', Area); # Outputting the area of the triangle

**Output**:

## Calculating the area of a triangle in Python when all three sides are known.

When you know all the three sides of a triangle, you can calculate the area of a triangle using Heron’s formula.

First, calculate the semi perimeter (S) by this formula – (Side 1 + Side 2 + Side 3)/2

After that, use this formula to calculate the area – √S*(S-Side1)*(S-Side2)*(S-Side3)

Here’s the Python program.

import math Side1 = float(input('Enter the first side of the triangle = ')) #First side of the triangle Side2 = float(input('Enter the second side of the triangle = ')) #Second side of the triangle Side3 = float(input('Enter the third side of the triangle = ')) #Third side of the triangle S = float(((Side1+Side2+Side3)/2)) #Calculating semi perimeter Area = float((S*(S-Side1)*(S-Side2)*(S-Side3))) #Solving Heron's expression within square root Area = math.sqrt(Area) #Calculating the square root to find the area print("Area of the triangle = ", Area) #Outputting the area of the triangle.

**Output**:

## Programming Exercise:

Submit your programming exercise in the comments section or you can also mail it to [email protected] Few lucky problem solvers will be featured on our **Facebook page** – CodeItBro – Programming Made Fun.

**1**. Write a Python program to calculate the area of a triangle when its two sides and the angle between them are known.

**2**. Make this AreaOfTriangles.py file into a Triangle Area Calculator, i.e., program will ask users how they would like to calculate the area of a triangle.