Showing posts with label area of a shape. Show all posts
Showing posts with label area of a shape. Show all posts

Functions - Area of a Shape

 # Functions 

# Area of a shape


def fnSquare(s):

return (s*s)


def fnRectangle(l,b):

return (l*b)


def fnCircle(r):

return 3.142*r*r

def fnTriangle(base,ht):

return 0.5*base*ht


# square


s = eval(input("Enter side of square : "))

l,b = map(int,(input("Enter length and breath of rectangle : ").split()))

r = eval(input("Enter circle radius : "))

base,ht = map(int,(input("Enter base and height of triangle : ").split()))


print("Area of square =",fnSquare(s))

print("Area of rectangle =",fnRectangle(l,b))

print("Area of circle =",fnCircle(r))

print("Area of triangle =",fnTriangle(base,ht))


"""

Sample output

>python 6_Area.py

Enter side of square : 5

Enter length and breath of rectangle : 4 6

Enter circle radius : 3

Enter base and height of triangle : 5 6

Area of square = 25

Area of rectangle = 24

Area of circle = 28.278

Area of triangle = 15.0

"""

Function overloading in Python - Area of a shape

# Functions - Area of a shape

# Function overloading 


"""

install multipledispatch as below from command prompt


pip install multipledispatch


"""


from multipledispatch import dispatch


@dispatch(int)

def fnShape(x):

print("Area of square = ",(x*x))

@dispatch(float)

def fnShape(x):

print("Area of circle = ",(3.142*x*x))


@dispatch(int,int)

def fnShape(x,y):

print("Area of rectangle = ",(x*y))


# The three function calls differ by number and datatype


fnShape(5) # int

fnShape(2.1)         # float

fnShape(5,6)         # int,int