# 6 Function in Python
If we familiar about function in math and spreadsheet, you can understand easily.
The spreadsheet gives us a ready use function. For example average(A1
In mathematics, a function show of how a varying quantity of variable depends on another quantity. … If the f is a name of
Python has many functions. There are a built-in function in python, functions in modules, and we can make new Built-in.
A built-in function is a function that Python already provides if we install it. We can see the built-in function here.
Then, to call
>>> x =[2,4,5,6] >>> sum(x) 17 >>> len(x) 4
Function in Standard Library
Python also has a hidden function at modules in the standard library folder. We have to call its modules to use it. This is
>>> import random >>> random.randint(0, 20) 7 >>> random.randint(500, 50000) 18601
We can read the notation is
This is list of modules standard libray.
Third Party Library
As I talk in the introduction, Python
pip install modulename
After install third party modules successfully, we can import to use it.
modulename is name of modules that you want to install. Dont type module name. If you install BeautifulSoup modules, you can type pip install BeautifulSoup
pip install BeautifulSoup
User Defined Function
If we want to make our own function, we can make it in python.
Syntax to create our create from scratch function is:
def NAME( PARAMETERS ): STATEMENTS
For example, if we want to create average function. We can make function like in code below.
>>>def average(xx): av = sum(xx)/len(xx) return av y = [1,2,3,4,5] print average(y)
That is example of user-defined function.