×

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

Python Set intersection_update() method

Python Set intersection_update() method The set.intersection_update() method in Python removes the items that is not present in both sets. It is different from the set.intersection() method, because the intersection()method returns a new set, with only  the common elements...

2 minutes read.

Python Combination

Python Combination Permutations and combinations are the important part of our mathematical tools. We use them often in our Python programs. In this tutorial, we will learn about combinations in Python...

6 minutes read.

Python Ways to find nth occurrence of substring in a string

Introduction In Python, a string is a collection of characters that can be employed to conduct additional operations. In Python, a substring is a group of characters that are a part...

4 minutes read.

Python Comments

In this tutorial, we will discuss the importance of comments in the Python code and how various types of comments can be inserted in the code. Comments are used to describe...

3 minutes read.

What does the if __name__ == "__main__" do in Python

In this article, you will learn about the If__name__==__main__ is a statement in python to define modules and the names of the modules. This statement plays a vital role in...

3 minutes read.

Python property()

Python property() class The property() class in Python returns a property attribute. Syntax class property(fget=None, fset=None, fdel=None, doc=None) Parameter fget: This attribute is used for getting an attribute value. fset : This parameter sets an attribute value. fdel: It is a function for deleting...

1 minute read.

Data Structures and Algorithms using Python | Part 2

Files: A file is a location or information stored in computer storage devices. File handling is essential when the information or the data is to be held permanently. When we try to...

20 minutes read.

How to develop a game in python

Fun always makes a task interesting and easy to execute. Learning in the same theoretical way at some point reaches the boring spot. In this article, using some Python knowledge,...

13 minutes read.

Python comment symbol

Python programmers frequently use the comment system because, without it, things may quickly become very perplexing. The developers' helpful information is provided in the comments, which helps the reader understand...

3 minutes read.

Python len() function

Python len() function The len() function in Python  returns the number of items in an object. Syntax len(s) Parameter s: This parameter represents a sequence (such as a string, bytes, tuple, list, or range) or...

1 minute read.

How to Concat two Dataframes in Python

Using Pandas dataframe, we can concat two dataframes or series in Python. So let's take a brief introduction to what is Pandas in Python. Pandas is a library typically used for...

7 minutes read.

Python Control Statements

Control statements are under the roof topic of loops and loops in python are defined as iteratively and repeatedly working on source code. Loop control statements are defined as they...

3 minutes read.

How to check the version of the Python Interpreter?

As we all know what an interpreter is, and how important it is. We should also be aware of the fact that it is important to have knowledge of the...

2 minutes read.

Python isinstance() function

Python isinstance() function The isinstance() function in Python returns a Boolean value ‘True’ if the given object is of the specified type, otherwise it returns False. Syntax isinstance(object, classinfo) Parameter object: It is a required parameter which represents an object. classinfo: This...

1 minute read.

Nested Tuple in Python

If you are a Python learner, this page contains all the information that helps you know about nested tuple and how to access it in python. What is a Tuple? Multiple items...

4 minutes read.

Anonymous/Lambda Function in Python

Lambda keyword is used to declare an Anonymous function, i.e. a function that does not have any name. It is also called Anonymous functions. In python, normal functions are defined...

3 minutes read.

Python Program to Convert Decimal into Binary, Octal, and Hexadecimal

Python Program to Convert Decimal into Binary, Octal, and Hexadecimal We know that the most widely used number system is a decimal system, but the computer only understands binary values. The...

2 minutes read.

Python print() function

Python print() function The print() function prints the specified message to the screen or other standard output devices. Syntax print(*objects,sep=' ',end='\n',file=sys.stdout,flush=False) Parameter objects: This function represents an object, which will be converted to a string...

1 minute read.

Comments in the Python Programming Language

In this tutorial, we will learn how to comment in multiple lines and paragraphs in the python programming language. Commenting a Paragraph in Python Most people think the importance of comments is...

2 minutes read.

Best resources to learn Numpy and Pandas in python

In this tutorial, we will discuss the different resources where we can learn about NumPy and Pandas libraries of Python in a more efficient way. NumPy NumPy, a Python library used for...

4 minutes read.