Defining functions

Functions are ensembles of instructions that usually receive specific inputs from you and provide a set of specific outputs related to these inputs. You can define them as one-liners, as follows:

def half(x):     return x/2.0

You can also define them as a set of many instructions in the following way:

import math def sigmoid(x):     try:         return 1.0 / (1 + math.exp(-x))     except:         if x < 0:              return 0.0         else:              return 1.0

Finally, you can create an anonymous function by using a lambda function. Think of anonymous functions as simple functions that you can define inline everywhere in the code, without using the verbose constructor for functions (the one starting with def). Just call lambda followed by its input parameters; then, ...

Get Python Data Science Essentials - Third Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.