# 6 Function in Python

Posted by

Function is also the most used element in Python programming language. It is a sequence of statements. So, Function combine variable, data, and operator. The function is one of the most used element because it will reduce working time by reduce repeated statement and formula.

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:A10). This function will sum all numbers within A1:A10 cell, then divide it by count of numbers. Spreadsheet hide detail process of the average function. Only show name and its parameters. This will reduce complexity, and

In mathematics, a function show of how a varying quantity of variable depends on another quantity. … If the f is a name of function , the notation is y = f (x) (read f of x). Function will manipulate x based a formula.

Python has many functions. There are a built-in function in python, functions in modules, and we can make new Built-in.

Built-in Function

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 built-in function is simple. Syntax of built-in function is functionname(parameter). Example, we use the sum function below.

>>> x =[2,4,5,6]
>>> sum(x)
17
>>> len(x)
4

len(parameter)” is also built-in function in python. It mean quantity of data series.

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 example to use random modules

>>> import random
>>> random.randint(0, 20)
7
>>> random.randint(500, 50000)
18601

We can read the notation is randit function of (0,20) in the ‘random’ modules. Random module have randint function, or random integer.

This is list of modules standard libray.

  • os
  • shutil
  • glob
  • sys
  • re
  • math
  • urllib2
  • smtplib
  • date
  • zlib
  • Timer
  • unittest

Third Party Library

As I talk in the introduction, Python have big community. They create tons of library and always growing. We can use their modules by install them previously. It is website that store library by community: https://pypi.org. To install them, we can go to command prompt or terminal.

pip install modulename

After install third party modules successfully, we can import to use it.

import modulename

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.