Functions in python are sections of code meant for a specific purpose (or, function). Functions help to modularize the code and make it reusable. By the end of this tutorial, you’ll have an idea about the anatomy of a python function, how to define and call them, the scope of variables used inside the functions, etc.
This is the fourth article in our series Python for Data Science.
- In the first article, we introduced how data science is changing the world and why Python is preferred by a majority of data science practitioners.
- The second article explained some of the fundamental building blocks of programming in python – expressions, variables, data types, operators, comments, input/output functions, etc.
- In our third article, we looked at the flow of control in python using constructs like conditionals and loops.
Table of Contents
- Components of a python function
- Scope of variables
- Function Arguments
- Recommended Reading
Components of a python function
There are multiple components that go into constituting a function.
A function in python is defined using the def
keyword followed by the function name. The function name is followed by parenthesis ()
which can have optional arguments. The body of a function may start with an optional docstring followed by the statements to be run in the function.
The function name: Function names follows the same rules as for naming variables in python.
Arguments: We can pass values as input to a function. These values are called arguments or function parameters. They are optional.
Docstring: Docstring, short for Documentation String, it’s is an optional statement at the beginning of the function body. It’s generally used to describe what the function does. It is a good development practice to include them.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
For a function, we can access its docstring with the __doc__
attribute. See the example below:
def add_numbers(x, y): """ Add the two numbers passed as arguments and return the sum """ total = x+y return total print(add_numbers.__doc__)
The above code gives the following output on execution:
Add the two numbers passed as arguments and return the sum
Return Statement: The return statement exits a function and shifts the control back to where the function was called from with the returned value. For example:
def add_numbers(x, y): """ Add the two numbers passed as arguments and return the sum """ total = x+y return total print(add_numbers(2, 3))
The above code gives the following output on execution:
5
In case a return statement is not present or return
is used without any expression inside a function, a None
object is returned by the function. For example, if we comment out the return statement from the above function and call it, we get a None
object.
def add_numbers(x, y): """ Add the two numbers passed as arguments and return the sum """ total = x+y # return total print(add_numbers(2, 3))
The above code gives the following output on execution:
None
Once a python function has been defined, it can be called from another section of the code like another function, program, or even the console. We can call a function by simply typing the function name with appropriate parameters. In the example above, the function add_numbers is called inside the print()
function by using the statement add_numbers(2, 3)
.
Scope of variables
The scope of a variable refers to the segment of the code in which the variable is accessible. Variables defined inside a function have their score limited to the function. They cannot be accessed from the outside and only exit in memory as long as the function executes.
Variables with limited scope are called local variables. In the above examples of the function add_numbers()
, total
is a local variable. Variables that can be accessed throughout the program are called global variables. The example below illustrates the difference:
x = 10 # global variable def sample_function(): y = 20 # local variable print("Value of x inside the function: ", x) print("Value of y inside the function: ", y) # call the sample_function() sample_function() # accessing the variables x and y outside the function print("Value of x outside the function: ", x) print("Value of y outside the function: ", y)
The above code gives the following output on execution:
Value of x inside the function: 10 Value of y inside the function: 20 Value of x outside the function: 10 ------------------------------------------------------------------------ NameError Traceback (most recent call last) <ipython-input-2-59c0eb47437f> in <module> 10 # accessing the variables x and y outside the function 11 print("Value of x outside the function: ", x) ---> 12 print("Value of y outside the function: ", y) NameError: name 'y' is not defined
We see that the global variable x
is accessible inside as well as outside the function but the local variable y
is only accessible inside the function. This is the reason we get an error on accessing y
outside the function.
If the same variable is defined inside as well as outside the function, for the instructions local to the scope of the function, the value of the local variable is used. See the example below:
x = 10 # global variable x def sample_function(): x = 20 # local variable x print("Value of x inside the function: ", x) # call the sample_function() sample_function() # accessing the variable x outside the function print("Value of x outside the function: ", x)
The above function gives the following output on execution:
Value of x inside the function: 20 Value of x outside the function: 10
Function Arguments
Functions can the following different types of arguments.
Positional Arguments: Positional arguments are arguments that need to be included in the proper position or order.
def add_numbers(x, y): """ Add the two numbers passed as arguments and return the sum """ total = x+y return total sum_total = add_numbers(2, 3)
In the above example, during the call of the function add_numbers()
, 2
and 3
are positional arguments. They are assigned to x
and y
respectively because of the position they occur.
Keyword Arguments: Keyword arguments are preceded by an identifier during the function call.
def add_numbers(x, y): """ Add the two numbers passed as arguments and return the sum """ total = x+y return total sum_total = add_numbers(y=2, x=3)
In the above example, 2
and 3
are keyword arguments. They are assigned to y
and x
respectively.
We can also assign a default value to an argument. This default value is used when no value is passed for that argument. The example below illustrates the use of default value in an argument.
def add_numbers(x, y=3): """ Add the two numbers passed as arguments and return the sum """ print("x =", x) print("y =", y) print("***") total = x+y return total sum_total = add_numbers(2) print("sum = ", sum_total)
The above code gives the following output on execution:
x = 2 y = 3 *** sum = 5
In the above example, the function definition gives a default value of 3
to the argument y
. Hence, when a single argument is passed, it’s used as the value for x
, while for y
, its default value is used.
Recommended Reading
In this tutorial, we looked at functions in python, their individual components, the different types of arguments, and the scope of local and global variables along with some examples. If you’d like to dive deeper, we recommend the following (opening available) resources to supplement the topics covered –
- Chapter-3 of the book Automate the Boring Stuff with Python. This book is specially designed to have an implementation first approach. The online version of the book is freely available.
- Python docs on function definitions.
In the next and final article in this series, we’ll be covering important built-in data structures in python.
Here’s the complete list of articles of our five-part tutorial series on Python for Data Science:
If you found this article useful do give it a share! For more such articles subscribe to us.
In the next article in this Python for Data Science series, we’ll be covering the different data structures in python. Stay tuned and happy learning!