Easy Tutorial
❮ Python If Example Python Att List Remove ❯

Python Calculate the Area of a Triangle

Python3 Examples

The following example calculates the area of a triangle by taking the lengths of its three sides from user input:

Example (Python 3.0+)

# -*- coding: UTF-8 -*-

# Filename : test.py
# author by : www.tutorialpro.org

a = float(input('Enter the length of the first side of the triangle: '))
b = float(input('Enter the length of the second side of the triangle: '))
c = float(input('Enter the length of the third side of the triangle: '))

# 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)

Executing the above code produces the following result:

$ python test.py 
Enter the length of the first side of the triangle: 5
Enter the length of the second side of the triangle: 6
Enter the length of the third side of the triangle: 7
The area of the triangle is 14.70

Python3 Examples

❮ Python If Example Python Att List Remove ❯