Skip to Main Content

Python for Basic Data Analysis

Start your data science journey with Python. Learn practical Python programming skills for basic data manipulation and analysis.

Functions in Python

Functions are a way of writing code whereby you group together a set of "tasks". Therefore, instead of writing tasks individually, multiple times, you simply use the Function that houses the set of tasks. 

This means if you have a repetitive task, you do not have to write the code over and over. Just write the code into a function and use the function to do the task.

Functions will be building blocks for constructing complex codes. 

Basic Structure for Functions

Structure of a function

Functions start with a def, followed by a function name and parenthesis, the docstring (optional), the function statements (i.e. codes) and ending with either return, pass, or nothing at all. Watch the accompanying video for a more detailed explanation. 

Python functions usually looks something like this:


def functionname(arguments):

 """This is the docstring that explains what this function is supposed to do"""
 function statements



Calling a function

To use the function we simply call its name by writing functionname()


In this example, my function name is hello.
Note that this function does not have a return statement, otherwise known as a void function.

Here is another example of a void function. The function rounds any decimal number that a user inputs to 3 decimal places. 

Video Guide

From 1.41 to 4.58


Write a function that will return the number 1.2347586839402 to 5 decimal places