Skip to content

Commit 7cb7735

Browse files
authored
Merge pull request #93 from UoMResearchIT/81-output-missing-for-print-statement-in-units-chapter
81 output missing for print statement in units chapter
2 parents dfa8597 + c18ccaa commit 7cb7735

1 file changed

Lines changed: 35 additions & 4 deletions

File tree

episodes/06-units_and_quantities.md

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ When printing the variable the unit information will now be attached:
5555
```python
5656
print(length)
5757
```
58+
```output
59+
26.2 m
60+
```
5861

5962
The type of this new variable is an `astropy` `Quantity`:
6063

@@ -93,26 +96,39 @@ distance_end = 23 * u.km
9396
length = distance_end - distance_start
9497
print(length)
9598
```
99+
```output
100+
19.99999 km
101+
```
96102

97103
And it also enables the combining of quantities, for example, to calculate a speed:
98104

99105
```python
106+
distance = 45 * u.km
100107
time = 15 * u.minute
101-
speed = length / time
108+
speed = distance / time
102109
print(speed)
103110
```
111+
```output
112+
3.0 km / min
113+
```
104114

105115
By default the units library will select units to report for these values based on what the units are of the objects that you have passed it. You can, as before, convert these to the units of your choice:
106116

107117
```python
108118
print(speed.to(u.km/u.s))
109119
```
120+
```output
121+
0.05 km / s
122+
```
110123

111124
You can also convert the units to the base (irreducible) units for the unit system you are using with the `decompose` function (changing the unit system choice will be covered later):
112125

113126
```python
114127
print(speed.decompose())
115128
```
129+
```output
130+
50.0 m / s
131+
```
116132

117133
::::::::::::::::::::::::::::::::::::::::: callout
118134

@@ -121,10 +137,13 @@ print(speed.decompose())
121137
If you wish to reduce to different base units you can pass a list of those units to the `bases` parameter when calling the `decompose` function:
122138

123139
```python
124-
print(speed.decompose(bases=['km','s']))
140+
print(speed.decompose(bases=["km", "s"]))
141+
```
142+
```output
143+
0.05 km / s
125144
```
126145

127-
Note that the order of the values in the list passed to `bases` parameter doesn't matter. However, the base units you choose must be either one of the original units used, or a standard base unit. This function cannot be used to convert from km to cm, for example. Instead it is useful where you only want to reduce select units within the object.
146+
Note that the order of the values in the list passed to the `bases` parameter doesn't matter. However, the base units you choose must be either one of the original units used, or a standard base unit. This function cannot be used to convert from km to cm, for example. Instead it is useful where you only want to reduce select units within the object.
128147

129148

130149
::::::::::::::::::::::::::::::::::::::::::::::::::
@@ -134,6 +153,9 @@ You can change the base system using functions such as `si` (to change to the de
134153
```python
135154
print(speed.cgs)
136155
```
156+
```output
157+
5000.0 cm / s
158+
```
137159

138160
::::::::::::::::::::::::::::::::::::::: challenge
139161

@@ -151,7 +173,7 @@ print(jerk)
151173
```
152174

153175
```output
154-
0.1111111111111111 m / (h min2)
176+
0.11111111111111112 m / (h min2)
155177
```
156178

157179
For the report we are writing we need to convert this to the units `km/hour^3`, which of
@@ -189,6 +211,9 @@ These can then be used in the same manner as the standard units:
189211
```python
190212
speed.to(imperial.mile/u.hour)
191213
```
214+
```output
215+
111.84681 mi/h​
216+
```
192217

193218
## Equivalent Units
194219

@@ -205,12 +230,18 @@ This, however, does not work as above:
205230
```python
206231
(656.281 * u.nm).to(u.Hz)
207232
```
233+
```output
234+
UnitConversionError: 'nm' (length) and 'Hz' (frequency) are not convertible
235+
```
208236

209237
Instead we need to inform `units` what unit equivalency we want it to use, which can be specified with the `equivalencies` option:
210238

211239
```python
212240
(656.281 * u.nm).to(u.Hz, equivalencies=u.spectral())
213241
```
242+
```output
243+
4.5680502×10^14 Hz
244+
```
214245

215246
In this case we use the `spectral` equivalence library, which allows conversions between wavelengths, wave number, frequency, and energy equivalent pairs. There are a number of other built-in equivalency libraries, for commonly used pairings.
216247

0 commit comments

Comments
 (0)