Function¶
In the context of programming, a function is a sequence of statements that performs a computation. Functions has three parts; argument, script, and output. Python has two kinds of function: built-in function that is in the core of Python or are collected as package. User-defined function that is written by user.
Built-in function¶
Python has a number of functions in its core that are always available, see
To round the value, use theround(value,size) function User function¶
Functions has three parts; argument, script, and output. It has simple structure
For instance write a function get two argument, add them together and return it.
If you do not specify the arguments, use a * argument,
You can define a default value for argument.
You can define an optional argument.
def letterGrade(score):
if score >= 90:
letter = 'A'
elif score >= 80:
letter = 'B'
elif score >= 70:
letter = 'C'
elif score >= 60:
letter = 'D'
else:
letter = 'F'
return letter
In-line function¶
A simple function can be written in one line,
Such function is more suitable for using inside the other operation, the follow get first and second name, then it sort according the last name. ``` names = ['Sam Amiri', 'Leila Alimehr','Ryan Amiri'] sorted(names, key=lambda name: name.split()[-1].lower())
sorted(names, key=lambda name: name.split()[-1].lower()) ['Leila Alimehr', 'Sam Amiri', 'Ryan Amiri'] sorted(names) ['Leila Alimehr', 'Ryan Amiri', 'Sam Amiri']
def divide(x, y): try: x / y except: print('Can not divide by zero!') else: return x / y## Map and Filter Python access to a higher order function which allows a function operates on other functions, either by taking a function as its argument, or by returning a function. Most popular ones are ```map``` (apply function on element) and ```filter ``` (apply function, if it is true return element) ``` x=[-1,0,1] list(map(abs, x)) list(filter(lambda x: x <= 0,x)) ``` Example: Write a function to divide two number, if the denominator is zero, stop the function and give an notification.
divide(3,1) divide(3,0)
The function is also can be rewritten using ```raise```, which raise an error and stop the function.
def divide(x, y):
"""Divide Function"""
if y == 0:
raise Exception('Can not divide by zero!')
return x / y
## Decorators
Decoreators in Python allows you to take a function and add additional uses without modifying its structure, the following example is from [ref](https://realpython.com/primer-on-python-decorators/#functions)
```{Python, echo = FALSE, message = FALSE}
def my_decorator(func):
def wrapper():
print("Something is happening before the function is called.")
func()
print("Something is happening after the function is called.")
return wrapper
def say_whee():
print("Whee!")
say_whee()
say_whee = my_decorator(say_whee)
say_whee()
The decorator often simplify using @name of decorator