Link to booklet is copied to clipboard!

Saved to booklet!

Removed from booklet!

Started capturing to booklet.

Stopped capturing to booklet.

Preferring latest booklet.

No longer prefers latest booklet.

Entered edit mode.

Exited edit mode.

Often, when coding some problem in *Python*, you face rather
complex mathematical expressions that cannot be written clearly as
one-line lambda-functions. You would then
use the standard Python *function* as introduced in this how-to.
As an example, consider that you have
three points defining a triangle:

and would like a function that receives the coordinates for the three
points, and returns the *area* of the triangle they form.
The mathematical operation you'll need is the following: Setup the two
vectors from one of the points to each of the other points:

Now evaluate the cross-product of the two vectors:

and take the length of that vector, and you have the area of the
parallelogram that two vectors define.

Now, the area of the triangle is half the area of the parallelogram:

Consider we would like to find the areas of these two triangles:

The first triangle has these three corners:

P0 = [2,1] P1 = [8,1] P2 = [5,5]

and its area can be calculated with these five lines:

v1 = np.array(P1) - np.array(P0) v2 = np.array(P2) - np.array(P0) v1_cross_v2 = [0, 0, v1[0] * v2[1] - v2[0] * v1[1]] A = np.linalg.norm(v1_cross_v2) A_tri = A/2

Providing the result:

A_tri

12.0

The five lines doing the calculation are nicely readably, but clearly,
they would not easily be written as a lambda function. Hence, when
calculating the area of the second triangle we would be tempted to
copy-paste the five lines, which for many reasons would be a bad idea.

In stead of copy-pasting code sections and reusing them, one would
rather want to introduce a Python *function*:

def area_tri(P0,P1,P2): v1 = np.array(P1) - np.array(P0) v2 = np.array(P2) - np.array(P0) v1_cross_v2 = [0, 0, v1[0] * v2[1] - v2[0] * v1[1]] A = np.linalg.norm(v1_cross_v2) A_tri = A/2 return A_tri

The function declaration starts with
As with the lambda function, a function declared with the above method
using
*indentation*-

`def`

and is followed
by the function name, here `area_tri`

. Then comes some
arguments in parentheses, `(`

`)`

, here
`P0`

, `P1`

, and `P2`

. Finally,
the first line of the function declaration ends with `:`

.
The following code lines will belong to the function as long as they
are indented (by some amount, typically 4 spacings). The function may
contain a statement with a `return`

keyword followed by
a single or a list of variables.
`def`

-`:`

-`return`

syntax is called by supplying values (or variables) as arguments. For
the first triangle we have:
area_tri([2,1],[8,1],[5,5])

12.0

So, the function returns the value of
And now, with practically no extra code, we get the area of the second triangle:

`A_tri`

as this is
what was written after `return`

in the function declaration.
area_tri([4,1],[2,5],[8,3])

10.0

Once deleted this booklet is gone forever!

Choose which booklet to go to: