How to define own function in python

 Although, python has a large number of standard functions to execute the calculation or logic we need to apply to our dataset, there are always going to be some situations when we need to create a customized calculation logic to arrive at our desired results.

In this scenario, we can create our own function using def(define) code.

A simple example is given below

Let's say, we want to make a calculator which can add the two values we enter.

Simple calculation function that we can create would be like this...

def calculate(x, y, operation):

    if operation == 'add':

        return x+y

To test this function,


If add is the only transaction we need from this calculator, then we can define it as a default operation

def calculate(x, y, operation = 'add'):

            return x+y

This will return us the result


As you can see that by setting a default operation as 'add', we do not need to enter it when we write the code.

But for it to carry out multiple calculations like subtract, multiply, division etc. we have to write conditions using conditional statements if, elif and else

def calculate(a,b,operation='add'):

    if operation == 'add':

        return a+b

    elif operation == 'subtract':

        return a-b

    elif operation == 'multiply':

        return a*b

    elif operation == 'percent':

        return a/b*100

    else:

        return 'Error'

Here we are giving multiple conditions so calculator can perform the task we need it to execute


We can see here that since division is not defined, it gives back Error


This is the simple application of executing our own function to meet our need

Another example is...

Let's say you are working in the payroll and you are asked to calculate pay for employees in such a way that when they work 40 hours or less, they get paid as per their standard rate but once they work any additional hours after 40, they need to be paid over time at the rate of 1.5 times their regular hour

And company pays a flat rate of $100 per hour to all contractors and hence if there is no hourly rate mentioned, then your calculation should consider default hourly rate of 100 and contractors are not paid over time irrespective of hours they work

Let's try to build a customized function for this requirement

def total_pay(hours, rate_per_hour = 100):

    if hours <= 40:

        return hours*rate_per_hour

    elif rate_per_hour == 100:

        return hours*rate_per_hour

    else:

        base_pay = 40*rate_per_hour

        overtime_pay = (rate_per_hour*1.5) * (hours-40)

        return base_pay + overtime_pay

As you can see that when hours worked are n=more than 40, we are going to calculate 1.5 times rate for only those hours worked after 40


As you can see, for employee whose rate is mentioned as $20 per hours, gets paid 800 for 40 hours (40X20) and for extra 1 hour worked, gets paid for $30(1.5X20X1)

But for contractor working for 41 hours only a flat rate of $100 is considered and gets paid $4100

Defining a customized function is a very useful feature of python that we can use frequently for getting the results we want.

No comments:

Post a Comment

Complex query example

Requirement:  You are given the table with titles of recipes from a cookbook and their page numbers. You are asked to represent how the reci...