In this article, I will take you through a basic Python program to find the Area of a Triangle using the Python Programming Langauge. Finding the area of a triangle with Python is same as finding it using mathematics.
So let’s see how we can do it with mathematics so that we can easily implement it with Python.
What is the Area of a Triangle?
In general, the term “area” is defined as the occupied region within the boundary of a flat object or figure. The measurement is made in square units, the standard unit being the square meter (m2). For the calculation of the area, there are predefined formulas for squares, rectangles, circles, triangles, etc. In this article, we’ll learn the area of triangle formulas for different types of triangles, along with some sample problems.
The area of a triangle is defined as the total region bounded by the three sides of a particular triangle. It is equal to half the base times the height, i.e. A = 1/2 × b × h.
Area of Triangle with Python
Now you must be well aware of what is area and how to find the area in mathematics. Now let’s see how can we find it by using the Python programming language.
To find the area with Python programming language we need 4 variables. 3 variables for storing the values of 3 sides either by taking a user input or simply storing a variable.
Then we need a new variable for storing the previous three variables with the mathematical formula of area of a triangle. But to find the area of a triangle we also need to find the semi- perimeter of a triangle so we need to have one more variable.
Now let’s get into the coding part to write a python program to find the area and semi-perimeter of a triangle:
Code language: Python (python)
a = 5 b = 7 c = 10 # calculate the semi-perimeter s = (a + b + c) / 2 # calculate the area area = (s*(s-a)*(s-b)*(s-c)) ** 0.5 print('The area of the triangle is %0.2f' %area)
The area of the triangle is 16.25
I hope you liked this article on how to write a program to find the area and semi-perimeter of a triangle with Python. Feel free to ask your valuable questions in the comments section below.