This page was generated from /home/docs/checkouts/readthedocs.org/user_builds/blm/checkouts/latest/docs/tutorials/tutorial-4-Functions in Python.ipynb.
Interactive online version:
Slideshow:
4.4. Functions in Python¶
In this tutorial, you will learn how to use functions in Python and why are they extremely useful for the BLM course
What you will learn here:
How to write simple functions in Python?
How to call functions?
How to pass variables and parameters to functions?
How to use
returnin functions?How to integrate functions together?
These packages are necessary later on. Load all the packages in one place for consistency
[1]:
import numpy as np
import pandas as pd
Let’s say you want a function that simply prints Hello World for you. You can define this function as following:
[4]:
def your_printer():
print('Hello World')
As you can see, it is very simple to define the function, and when you run the above cell, nothing happens because we have not called the function yet. This is the handy part of functions in Python (or any other programming languages). You define the function once, and then you can all it whenever you want, and it does the job for you. You can call the function simply:
[3]:
your_printer()
Hello World
Congrats! you just wrote and called the first function in Python.
But the application of functions goes beyond just printing. You can pass variables into function and they do the job for you. For example, let’s look modify your_printer function to print what we pass to it:
[5]:
def your_printer(your_word):
print(your_word)
Now let’s call this function:
[6]:
your_printer('This is your word!')
This is your word!
You can call a function multiple times, and this is where functions save you time and space in coding:
[7]:
your_printer('first word')
your_printer('second word')
your_printer('third word')
first word
second word
third word
Sometimes, you would like to pass a variable to a function, and the function does some mathematical operations and return the results to you for future use. In this case, you use return in functions. For example
[8]:
def sum_two_number(number1,number2):
return number1+number2
The above function, simply gets two number and returns the sum of them to you. You can use it as follows
[9]:
sum_numbers=sum_two_number(1,2)
print(sum_numbers)
3
Then you can use the result of the function to do other stuff. For example:
[10]:
print(sum_two_number(sum_numbers,4))
7
Now let’s have a more complex example. Let’s say you have the following equations:
\(q_1=x^2+x\)
\(q_2=\sin(q_1)+\frac{1}{q_1+1}\)
\(q_3=q_2*q_1+x^3\)
\(q_4=q_1+q_2+q_3+x\)
Now given a value for \(x\), you like to find the value of \(q_4\). This is where functions come handy. Let’s implemet this:
[14]:
############################## q1
def cal_q1(x):
return x**2+x
############################## q2
def cal_q2(q1):
return np.sin(q1)+1/(q1+1)
############################## q3
def cal_q3(q1,q2,x):
return q2*q1+x**3
############################## q4
def cal_q4(x):
q1=cal_q1(x)
q2=cal_q2(q1)
q3=cal_q3(q1,q2,x)
return q1+q2+q3+x
now we can calculate values of \(q_4\) given any arbitrary value for \(x\)
[15]:
cal_q4(12)
[15]:
1758.5598148460792
[16]:
cal_q4(1)
[16]:
7.727892280477045
[17]:
cal_q4(5)
[17]:
130.3710196531213