Skip to content

Commit 4e1980e

Browse files
authored
Merge pull request #989 from DevinBayly/patch-1
show an anti-idiom first
2 parents 2d2eb88 + 68b5a13 commit 4e1980e

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

_episodes/08-func.md

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,30 @@ keypoints:
3131
then call it with different parameter values to customize its behavior."
3232
---
3333

34-
At this point,
35-
we've written code to draw some interesting features in our inflammation data,
36-
loop over all our data files to quickly draw these plots for each of them,
37-
and have Python make decisions based on what it sees in our data.
38-
But, our code is getting pretty long and complicated;
39-
what if we had thousands of datasets,
40-
and didn't want to generate a figure for every single one?
41-
Commenting out the figure-drawing code is a nuisance.
42-
Also, what if we want to use that code again,
43-
on a different dataset or at a different point in our program?
34+
At this point, we've seen that code can have Python make decisions about what it sees in our data. What if we want to convert some of our data, like taking a temperature in Fahrenheit and converting it to Celsius. We could write something like this for converting a single number
35+
36+
~~~
37+
fahrenheit_val = 99
38+
celsius_val = ((fahrenheit_val - 32) * (5/9))
39+
~~~
40+
{: .language-python}
41+
42+
and for a second number we could just copy the line and rename the variables
43+
44+
~~~
45+
fahrenheit_val = 99
46+
celsius_val = ((fahrenheit_val - 32) * (5/9))
47+
48+
fahrenheit_val2 = 43
49+
celsius_val2 = ((fahrenheit_val2 - 32) * (5/9))
50+
~~~
51+
{: .language-python}
52+
53+
But we would be in trouble as soon as we had to do this more than a couple times.
4454
Cutting and pasting it is going to make our code get very long and very repetitive,
4555
very quickly.
4656
We'd like a way to package our code so that it is easier to reuse,
47-
and Python provides for this by letting us define things called 'functions' ---
48-
a shorthand way of re-executing longer pieces of code.
57+
a shorthand way of re-executing longer pieces of code. In Python we can use 'functions'.
4958
Let's start by defining a function `fahr_to_celsius` that converts temperatures
5059
from Fahrenheit to Celsius:
5160

0 commit comments

Comments
 (0)