Hi all, Greetings of the day & welcome to Technifyed this is Vasav and today I’ll teach you how to calculate the area of Figures using Python
So, Let’s Start:
Here’s the very first line
def tri_area(base, height, unit):
We know that we need to start a function def. tri_area is the name of our function and then we have three variables namely base, height & unit.
print(str((base * height) / 2) + " sq " + unit)
I know this line of code is a bit/ difficult to understand but I’ll try to make it easy for you.
Let’s start from the innermost bracket. That is the formula for calculating the area of a triangle using python. We’ve put str there because we need to add a string to it and the str function will help with that. Sq means Square and the unit is variable.
This is code in all:
def tri_area(base, height, unit): print(str((base * height) / 2) + " sq " + unit)
The input you will give:
tri_area(5, 6, 'm')
Output:
15 sq m
Like this, I’ll now give you the code for three more shapes square, rectangle & circle-
def sqr_area(side, unit):
print(str(side*side) + ' sq ' + unit)
def rect_area(length, breadth, unit): print(str(length * breadth) + ' sq ' + unit) def circ_area(radius, unit): pi = 3.14 print(str(pi * (radius**2)) + ' sq ' + unit)
Also Read:
Meta Front-End Developer Professional Certificate: A Roadmap to Success