PYTHON FUNCTIONS: Built-in & User Defined..

Subhomoy Chattopadhyay
4 min readOct 19, 2020

Let us have a look at functions in PYTHON!

Image source: Medium

What does someone think of when they hear the word ‘Functions’?? Are you thinking about anything?? I am pretty much sure you are thinking of a particular activity or a job!! Yeah!! It is the same with programming languages also. A function is created to perform certain roles. It is similar to a team game! Every players are assigned to a specific task. So if you want your code to work properly it is better to start writing functions. That will develop modularity of your code and it will be much easier to understand.

In Python we have a number of really useful built-in functions. And we can create a function of our choice also. So let us see some of them.

Firstly, we will look at some of the useful Python built-in functions.

sum(): As the name suggests this function is going to do. We can pass an iterable as the function argument and it will return the summed up value of it.

sum() Function

See here we are passing a list(an iterable) and a single value. Internally the sum function is calculating the total of the iterable first and then it is adding that sum with the other value passed in it.

len(): This function takes a string or character type as input and returns the number of individual symbols as well as the special characters and spaces in it.

len() Function

id(): This function takes a variable name and returns the memory address of the particular object that the assigned variable is viewing.

id() Function

type(): This function returns the data type of a particular variable.

type() Function

Just like these four functions there are so many built-in functions which are really useful and they are making Python so much powerful. Some of the libraries like ‘numpy’, ‘matplotlib.pyplot’ are full of such powerful functions and those are making our lives easier to analyse different datasets. I will write separate articles on them in future. We can check the different functions in a particular library by using ‘dir()’ and ‘help()’ function.

dir() Function

By using ‘dir()’ we get a list of all member functions inside a class. Here in the above example the list of functions inside the class ‘list’ have been shown.

If you want to understand a particular function from a class you need to use ‘help()’.

help() Function

Here we understood in detail about the function ‘append’ of the class ‘list’ by using ‘.’ operator.

Let us now discuss about user defined functions. We can create a function of our choice by using the ‘def’ or ‘lambda’ keywords in Python. If the function can be written within a single line and we want it to be anonymous in our code we prefer to use ‘lambda’. Otherwise, we use ‘def’ keyword .

def Keyword

Here in the example we wrote two separate functions by using ‘def’ keyword. And we call them at the end to perform the tasks we have assigned to them. You need to specify the valid arguments a function can take inside round parentheses. We do not need to specify any return type of a function in Python unlike C++ because Python is dynamically typed.

Lambda functions are really handy when it comes to one-liners.

In the above example we have created a function named as ‘mysum’. If we use ‘def’ keyword we need to write at least 4 lines for the task performed by the function ‘mysum’.

We can make use of very useful cumulative functions like ‘functools.reduce()’ and ‘itertools.accumulate()’.

reduce and accumulate

Here by using the one-liner ‘mysum’ we can calculate the total sum at once by using functools.reduce(function_name,iterable) and cumulative sum in every steps by using itertools.accumulate(iterable,function_name).

So it is really important to learn and explore about functions in any programming language if we want to make our codes more robust, versatile and modular. Hope you liked the different functions I have mentioned and how to create your own functions by using ‘def’ and ‘lambda’ keywords.

Thank you so much for your patience to read the article. I am pretty much sure it will inspire you to explore more about different Python functions!!Good-Bye for now!! See you soon in my next article..Stay Tuned!! Stay Safe!!

--

--