Unlock Analytics

Python for HR : Functions

python for HR : Function

Hello and welcome to this series of Python for HR.

A function in Python is a set of instructions. An HR can use these functions in their data analysis operations for the tasks which are required to be performed again and again. For example, if an HR has to calculate a productivity metric using 3 other metrics, then he can define a function once, and use it as many times in the future, this would save his overhead of writing the same code again and again. There is a huge number of in-built functions present in python and you can also build your own functions.

A function is written to carry out a specific task and works as an object so that python interpreter can easily pass control to it. This means that whenever a function is encountered, python starts executing the codes defined in the function. After performing the task, the function returns the control to the interpreter as well as the result (if there is any) which can be stored into another object. This means that when no code is left in the function python starts from where it left off, that is the code after the function was encountered.

Basic syntax 

Here is the syntax of a python function. It is defined using keyword def and function name with one or more arguments that are accessed to complete the operation, and a function body which contains the codes which define the task of a function.

def my_function():  


    return <expression>  

A simple example for a function:

def hello_world():  
    print(“Hello Folks”)  


Output: Hello_Folks 

Built-in functions

There are so many built-in functions in Python. When we enter parameters, Python matches them with arguments of these functions by value or by position and then execute the function body.

Here are a few examples of inbuilt functions


  • Create the sequence of 1 to 10 numbers


Syntex : range(start, stop, step)  

You can simply give the arguments and function will return the result.


Output :  1 2 3 4 5 6 7 8 9 10

  • Find sum of numbers 1 to 10

sum(iterable, start

a = (1, 2, 3, 4, 5,6,7,8,9,10)


x = sum(a, b) 


Output: 60

User-Defined Functions

These are the functions which are created by us according to our requirements. We need to create these functions because there are some particular tasks which can’t be done with pre-existing functions:

This is the basic syntax to write a function

def function.name():  


    return <expression>  

Here function.name() is used to call the function We have some ways to call the functions which are:

  • Calling a function with arguments by position

#defining the function  

def func (name):  

print(“Hi “,name);  

#calling the function   


Output : Richard

  • Calling a function with arguments by name

#defining the function  

def func (name):  

print(“Hi “,name);  

#calling the function   

func(name= “Richard”) 

Output : Richard


  • Calling a function with default argument


#defining the function  

def func (name= “Richard” ):  

print(“Hi “,name);  

#calling the function   


Output : Richard

You can achieve almost all analytical tasks by using predefined functions, but there are some cases where you have no choice but to define your own function, hence it is always handy if you know how to create a function.

Our next article in this series of Python for HR is IF-Else in Python .