×

Python program to find the area of the triangle

Python program to find the area of the triangle

This article will discuss how to find the area of a triangle in Python with all three given sides. The area of a triangle represents the total region that is enclosed by the three sides of any particular triangle.

If we know the length of three sides of a triangle, then the following mathematical formula is used to calculate the area of a triangle:

Area of a Triangle = ?(s*(s-a)*(s-b)*(s-c)) Or, Area of a Triangle = sqrt of(s*(s-a)*(s-b)*(s-c))

Here a, b, c are the three sides of a triangle, and s is the semi-perimeter that can be calculated as below:

s = (a + b + c )/ 2

Source Code

The following is a source code to calculate the area of a triangle in Python:

#Program to find the area of the triangle  
# Three sides of the triangle is a, b and c:  
 a = float(input("Enter first side: "))     
 b = float(input("Enter second side: "))   
 c = float(input("Enter third side: "))     
 # 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)  

Output

Here is the output:

Python program to find the area of the triangle

The following source code calculates the area of a triangle in Python by importing math function:

#import math function import math   
# Three sides of the triangle  
a= int(input("Enter first side: ")) 
b=int(input("Enter second side: ")) 
c=int(input("Enter third side: "))   
# calculate the semi-perimeter            
s=(a+b+c)/2 area=math.sqrt(s*(s-a)*(s-b)*(s-c))   
#print the result print("Area of the triangle is: ",round(area,2))

Output

Here is the output:

Python program to find the area of the triangle

Explanation: In this program, a user first asks to enter the length of three sides of a triangle that will be stored in variables a, b and c, respectively. Next, it will first calculate the semi-perimeter and then calculate a triangle area using the specified mathematical expression. After performing the calculation, it will store the area in a variable area, and finally, the result will be printed on the screen.


Related Topics

How to Pass a list as an Argument in Python

Lists in Python A list is a datatype in python which is used to store the data in a sequence. The lists are mutable; that is, we can change the values...

3 minutes read.

Convert XML to JSON in Python

XML conversion is very useful if we work on an API that returns data in JSON format and the source of data is in XML format. JSON A JSON file reserves the...

4 minutes read.

Python Dictionary clear() method

Python Dictionary clear() method The dictionary.clear() method in Python removes all the elements from a dictionary. Syntax dictionary.clear() Parameter NA Return None Example 1 # Python program explaining # the dictionary.clear() method # initialising the dictionary fruits =...

1 minute read.

Python program to add two number

Python program to add two number This program will add the two numbers and display their sum on the screen. Example: Input: Number1 = 20        Number2 = 30   Output: Sum =...

2 minutes read.

Python Matrix Operations

Python Matrix In this section of the Python tutorial, we will look at the introduction of the matrix in Python programming. We will perform many operations on the Python matrix using...

21 minutes read.

Python Write Excel File

Python Write Excel File The Python xlwt module is used to write an excel file and perform multiple operations on it. It can be used to write text, numbers, and formulas for multiple...

3 minutes read.

Python issubclass() Function

Python issubclass() Function The issubclass() function in Python returns a boolean value ‘True’ if the given object is a subclass of classinfo, else it returns False. Syntax issubclass(class, classinfo) Parameter class: It is a required parameter which represents a tuple...

1 minute read.

How to make API calls in Python

Information is extremely critical these days since it drives applications and organizations. It is subsequently critical to figure out how to get this information to serve your application. In fundamental...

4 minutes read.

Python Syntax Error Invalid Syntax

Python is renowned for its simple and direct syntax. However, we might come across some things that Python doesn't allow if we are learning Python for the very first time or if...

14 minutes read.

Index Error in Python

The index errors are the run time error that is raised in Python when we try to access an index that does not exist. This might seem very trivial but...

4 minutes read.

Difference between python list and tuple

In this tutorial, we will understand the key differences between python lists and tuples in python. Firstly, let us see the similarities between Lists and Tuples. Both are two data types...

4 minutes read.

Python min() function

Python min() function returns you the minimum value element in an iterable. We can also use this function to determine the smallest value among various arguments passed to this function. We...

4 minutes read.

Iterators in Python

Introduction In Python, an iterator is defined as an object that enables traversing through all the values of a collection. It contains the countable number of values. The iterator is utilized to...

4 minutes read.

iobase Python

All I/O flow classes derive from this conceptual base class. Derived classes will have to execute several of the class's abstract data types. The loop method is supported by all members of...

4 minutes read.

Python program to find the greatest among two numbers

Python program to find the greatest among two numbers We have many approaches to get the largest number among the two numbers, and here we will discuss a few of them....

2 minutes read.

Spell Corrector GUI using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

3 minutes read.

Python Random Module

The tutorial for the Python random module demonstrates how to produce pseudo-random integers in Python. Random Number Generator (RNG) The RNG (random number generator) generates a series of values with no discernible...

8 minutes read.

Python Project Ideas Based On Django

Introduction If you have learned Python and you are an expert in Django, then your practical skills should be excellent in this field. If you want to check your practical skills,...

9 minutes read.

Python Switch Case

What is a Switch Case Statement? In the programming languages, a switch statement is a logical loop or syntax that tests the variable's value and compares it with multiple cases. Once...

3 minutes read.

Difference between Python and CPP

Difference between Python and C++ We all know that both C++ and Python are two of the most popular programming languages. Both C++ and Python shares some basic similarities such as...

6 minutes read.