After this tutorial, you will be able to write a Python program to calculate the area of the triangle.
If you are new to Python programming, you should first read these topics to have a better understanding of this code:
- Variables in Python
- How to get input from the user in Python?
- Arithmetic operations in Python programming
Now let’s take a look at the code (I have an explained version of the same code down below).
Simple Code
print('To calculate the area of a triangle')
base = float(input('Enter its base: '))
height = float(input('Enter its height : '))
area = 1 / 2 * ( base * height )
print('The area of the triangle is' , area)
Output
To calculate the area of a triangle
Enter its base: 12
Enter its height : 12
The area of the triangle is 72.0
Explained: Python Program to find the area of a triangle
print('To calculate the area of a triangle')
#take input from user, and then convert it into float. This input is stored in base variable
base = float(input('Enter its base: '))
#take input from user, and then convert it into float. This input is stored in height variable
height = float(input('Enter its height : '))
#this is the formula to find the area of a triangle
#after calculating the area, it is stored in the area variable
area = 1 / 2 * ( base * height )
#finally the output is printed on the output screen
print('The area of the triangle is' , area)
Discover more from Python Mania
Subscribe to get the latest posts sent to your email.