Javascript required
Skip to content Skip to sidebar Skip to footer

How to Find the Length of a Triangle

In this python tutorial, you will learn about how to find area of a triangle in Python, the Python program to find the area of a triangle and, also we will check:

  • Python program to find the area of a triangle
  • Python program to calculate the area of a triangle using function
  • Python program to find the area of a triangle given all three sides
  • Python program to find the area of a triangle using class
  • Python program to find the area of a triangle when base and height are given
  • Python program to find the area of a triangle using function when base and height are given

Python program to find the area of a triangle

Let see python program to find the area of a triangle.

  • In this example, we will ask the user to enter the length of three sides of a triangle.
  • We will use Heron's formula to calculate the semi-perimeter.
  • And Area = (s*(s-a)*(s-b)*(s-c)) ** 0.5 to calculate the area of the triangle.
  • At last,print the area of the triangle to get the output.

Example:

          a = float(input('Enter the length of first side: '))   b = float(input('Enter  the length of second side: '))   c = float(input('Enter  the length of third side: '))   s = (a + b + c) / 2   Area = (s*(s-a)*(s-b)*(s-c)) ** 0.5   print('The area of the triangle is %0.2f' %Area)        

You can refer to the below screenshot to see the output for the python program to find the area of a triangle.

Python program to find the area of a triangle
Python program to find the area of a triangle

The above code we can use to find area of a triangle in Python.

Also, read, Python program to find the area of square.

Python program to calculate area of a triangle using function

Now, we will see python program to calculate the area of a triangle using function.

  • Firstly, we will import math module which will allow us to use the mathematical function like math.sqrt function.
  • Now, we will define the function with three arguments using the def keyword as def Areaoftriangle(a, b, c).
  • We will use Heron's formula to calculate the semi-perimeter.
  • And Area = math.sqrt((s*(s-a)*(s-b)*(s-c)))to calculate the area of the triangle.
  • The user will enter the three sides of the triangle a,b,c and we will pass those values to function arguments to calculate the area.
  • At last,print is used to get the output.

Example:

          import math def Areaoftriangle(a, b, c):     Perimeter = a + b + c     s = (a + b + c) / 2     Area = math.sqrt((s*(s-a)*(s-b)*(s-c)))     print("The perimeter of traiangle = %.2f" %Perimeter);     print("The Semi Perimeter of traiangle = %.2f" %s);     print("The Area of a triangle is %0.2f" %Area) Areaoftriangle(8, 9, 10)        

You can refer to the below screenshot to see the output for python program to calculate the area of a triangle using function.

Python program to calculate the area of a triangle using function
Python program to calculate the area of a triangle using function

The above code, we can use to calculate area of a triangle using function in Python.

You may like, Python program to find an area of a rectangle.

Python program to find area of a triangle given all three sides

Here, we will see python program to find the area of a triangle given all three sides

  • Firstly, we will import math module which will allow us to use the mathematical function like math.sqrt function.
  • We will ask the user to enter the length of three sides of a triangle and it will be stored in a separate variable.
  • We will use Heron's formula for calculating the semi-perimeter which is s=(a+b+c)/2.
  • And Area = math.sqrt(s*(s-a)*(s-b)*(s-c)) to calculate the area of the triangle.
  • At last,print the area of the triangle to get the output.

Example:

          import math a=int(input("Enter the length of first side: ")) b=int(input("Enter the length of second side: ")) c=int(input("Enter the length of third side: ")) s=(a+b+c)/2 Area=math.sqrt(s*(s-a)*(s-b)*(s-c)) print("Area of the triangle is: ",round(Area,2))        

You can refer to the below screenshot to see the output for the python program to find the area of a triangle given all three sides.

Python program to find the area of a triangle given all three sides
Python program to find the area of a triangle given all three sides

This is the python program to find area of a triangle given all three sides.

Check out, How to calculate area of a circle in Python?

Python program to find area of a triangle using class

Let's see python program to find the area of a triangle using class

  • We will create a class called class sides.
  • Another class called class A(sides) it has a method called def area(self) return(s*(s-self.a)*(s-self.b)*(s-self.c)) ** 0.5 which is the area of the class.
  • Anobject for the class is created asobj = A(10,12,14).
  • By using the object, the methodarea() is called.
  • At last,print the area of a rectangle to see the output.

Example:

          class sides:     def __init__(self,a, b, c):         self.a = a         self.b = b         self.c = c class A(sides):     def area(self):         s = (self.a + self.b + self.c)/2         return (s*(s-self.a)*(s-self.b)*(s-self.c)) ** 0.5 obj = A(10,12,14) print("Area of triangle : {}".format(obj.area()))        

You can refer to the below screenshot to see the output for the python program to find the area of a triangle using class.

Python program to find the area of a triangle using class
Python program to find the area of a triangle using class

Also, read, How to calculate simple interest in Python?

Python program to find area of a triangle when base and height are given

Now, we will see python program to find the area of a triangle when base and height are given

  • In this example, we will ask the user to enter the base and height of a triangle.
  • To calculate the area of a triangle we will use the formula Area = (b * h)/2.
  • At last,print the area of the triangle to get the output.

Example:

          b = float(input('Enter the base of a triangle: '))   h = float(input('Enter the height of a triangle: '))   Area = (b * h)/2 print('The area of the triangle is %0.2f' %Area)        

You can refer to the below screenshot to see the output for the python program to find the area of a triangle when base and height are given.

Python program to find the area of a triangle when base and height are given
Python program to find the area of a triangle when base and height are given

Read: PdfFileReader Python example

Python program to find area of a triangle using function when base and height are given

Here, we will see a python program to find the area of a triangle using function when base and height are given

  • In this example, we will define the function as def Area(base, height).
  • Here, Area(10, 15) is the value passed to the function argument.
  • At last, it will return the area of the triangle and print the output.

Example:

          def Area(base, height):    return 0.5 * base * height print(Area(10, 15))        

You can refer to the below screenshot to see the output for the python program to find the area of a triangle using function when base and height are given.

Python program to find the area of a triangle using function when base and height are given

You may like the following Python tutorials:

  • Python program to print pattern
  • How to print factorial of a number in Python
  • How to swap two numbers in Python

In this Python tutorial, we have learned about thePython program to find the area of a triangle. Also, we covered these below topics:

  • Python program to find the area of a triangle
  • Python program to calculate the area of a triangle using function
  • Python program to find the area of a triangle given all three sides
  • Python program to find the area of a triangle using class
  • Python program to find the area of a triangle when base and height are given
  • Python program to find the area of a triangle using function when base and height are given

How to Find the Length of a Triangle

Source: https://pythonguides.com/find-area-of-a-triangle-in-python/