English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python basic tutorial

Python flow control

Fonctions en Python

Types de données en Python

Python file operations

Python objects and classes

Python date and time

Advanced knowledge of Python

Python reference manual

Python program to calculate the area of a triangle

Python example大全

In this program, you will learn to calculate the area of a triangle and display it.

To understand this example, you should understand the followingPython programmingTopic:

If a, b, and c are the three sides of a triangle. Then,

s = (a+b+c)/2
area = √(s(s-a)*(s-b)*(s-c))

Source code

# Use Python program to calculate the area of a triangle
a = 5
b = 6
c = 7
# Uncomment the following to input from the user
# a = float(input('Enter the first side: '))
# b = float(input('Enter the second side: '))
# c = float(input('Enter the 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 result

The area of the triangle is 14.70

In this program, we useHeron's formulaGiven three sides, you can calculate the area of a triangle.

If you need to calculate the area of a triangle based on user input, you can useinput() function.

Python example大全