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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.