You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _episodes/08-func.md
+21-12Lines changed: 21 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,21 +31,30 @@ keypoints:
31
31
then call it with different parameter values to customize its behavior."
32
32
---
33
33
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.
44
54
Cutting and pasting it is going to make our code get very long and very repetitive,
45
55
very quickly.
46
56
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'.
49
58
Let's start by defining a function `fahr_to_celsius` that converts temperatures
0 commit comments