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/05-units_and_quantities.md
+30-8Lines changed: 30 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,13 +24,7 @@ One of the most common problems in physics and the development of physical model
24
24
25
25
In this lesson we will look at one of the available libraries in python for helping to avoid such problems. This is the [units](https://docs.astropy.org/en/stable/units/index.html) module in the [astropy](https://docs.astropy.org/en/stable/index.html) library.
26
26
27
-
## Installing a new python library
28
-
29
-
Following the conda lesson, we install the astropy library using:
30
-
31
-
```bash
32
-
conda install astropy
33
-
```
27
+
## Importing a module from a python library
34
28
35
29
The `units` module can then be imported in your scripts using:
36
30
@@ -40,6 +34,24 @@ from astropy import units as u
40
34
41
35
Importing as `u` is not necessary, but it is a common shorthand for this module, and so we'll use it below.
42
36
37
+
::::::::::::::::::::::::::::::::::::::::: callout
38
+
39
+
As you are likely using the Jupyter interface to follow this lesson, it is likely that the `astropy` library will already be installed there. If not, then you may encounter an error when running this command. We will go into more detail on installing packages in a later lesson; for now, installing the library will depend on how you set your environment up initially; if you used `pip` (or are in Jupyter and `astropy` doesn't work), then you need to use
40
+
41
+
```bash
42
+
pip install astropy
43
+
```
44
+
45
+
Alternatively, if you installed some form of `conda`, then you can use
46
+
47
+
```bash
48
+
conda install astropy
49
+
```
50
+
51
+
We will go into more detail about `conda` in a later lesson.
Units can be added to a python variable by multiplying that variable by the unit:
@@ -421,12 +433,22 @@ This is verging on unreadable (as is demonstrated by us having to split a simple
421
433
422
434
Fortunately there is support for cleaner temperature conversions in the [pint](https://pint.readthedocs.io/en/latest/index.html) package. This has similar functionality to the `astropy.unit` package, but has been built from the ground up with a focus on different aspects. These include unit parsing and standalone unit definitions, uncertainties integration, and (important for our example here), cleaner treatment of temperature units.
423
435
424
-
Pint can be installed using conda (and the conda-forge channel):
436
+
::::::::::::::::::::::::::::::::::::::::: callout
437
+
438
+
Pint should already be installed if you are using Jupyter; however, as before, if you need to install it yourself, it can be installed using conda (and the conda-forge channel) with:
0 commit comments