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
that asks Python to run the [function]({{ page.root }}/reference.html#function) `loadtxt` which
72
72
belongs to the `numpy` library.
73
-
This [dotted notation]({{ page.root }}/reference.html#dotted-notation)
74
-
is used everywhere in Python: the thing that appears before the dot contains the thing that
75
-
appears after.
73
+
The dot notation in Python is used most of all as an object attribute/property specifier or for invoking its method. `object.property` will give you the object.property value,
74
+
`object_name.method()` will invoke on object_name method.
76
75
77
76
As an example, John Smith is the John that belongs to the Smith family.
78
77
We could use the dot notation to write his name `smith.john`,
@@ -205,16 +204,16 @@ first value in data: 0.0
205
204
{: .output}
206
205
207
206
~~~
208
-
print('middle value in data:', data[30, 20])
207
+
print('middle value in data:', data[29, 19])
209
208
~~~
210
209
{: .language-python}
211
210
212
211
~~~
213
-
middle value in data: 13.0
212
+
middle value in data: 16.0
214
213
~~~
215
214
{: .output}
216
215
217
-
The expression `data[30, 20]` accesses the element at row 30, column 20. While this expression may
216
+
The expression `data[29, 19]` accesses the element at row 30, column 20. While this expression may
218
217
not surprise you,
219
218
`data[0, 0]` might.
220
219
Programming languages like Fortran, MATLAB and R start counting at 1
@@ -411,11 +410,6 @@ maximum inflammation for patient 0: 18.0
411
410
~~~
412
411
{: .output}
413
412
414
-
Everything in a line of code following the '#' symbol is a
415
-
[comment]({{ page.root }}/reference.html#comment) that is ignored by Python.
416
-
Comments allow programmers to leave explanatory notes for other
417
-
programmers or their future selves.
418
-
419
413
We don't actually need to store the row in a variable of its own.
420
414
Instead, we can combine the selection and the function call:
Copy file name to clipboardExpand all lines: _episodes/08-func.md
+32-13Lines changed: 32 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,26 +31,45 @@ 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
50
59
from Fahrenheit to Celsius:
51
60
52
61
~~~
62
+
def explicit_fahr_to_celsius(temp):
63
+
# Assign the converted value to a variable
64
+
converted = ((temp - 32) * (5/9))
65
+
# Return the value of the new variable
66
+
return converted
67
+
53
68
def fahr_to_celsius(temp):
69
+
# Return converted value more efficiently using the return
70
+
# function without creating a new variable. This code does
71
+
# the same thing as the previous function but it is more explicit
72
+
# in explaining how the return command works.
54
73
return ((temp - 32) * (5/9))
55
74
~~~
56
75
{: .language-python}
@@ -897,7 +916,7 @@ readable code!
897
916
> > and does not alter `k` outside of its local copy.
898
917
> > Therefore the original value of `k` remains unchanged.
899
918
> > Beware that a local `k` is created because `f2k` internal statements
900
-
> > *affect* a new value to it. If `k` was only `read`, it would simply retreive the
919
+
> > *affect* a new value to it. If `k` was only `read`, it would simply retrieve the
0 commit comments