Let's start with some common units, like Energy.

In [1]:

```
# Uncomment the subsequent lines in this cell to install dependencies for Google Colab.
# !pip install pymatgen==2022.7.19
```

In [1]:

```
import pymatgen.core as mg
# The constructor is simply the value + a string unit.
e = mg.units.Energy(1000, "Ha")
# Let's perform a conversion. Note that when printing, the units are printed as well.
print(f"{e} = {e.to('eV')}")
# To check what units are supported
print(f"Supported energy units are {e.supported_units}")
```

In [2]:

```
dist = mg.units.Length(65, "mile")
time = mg.units.Time(30, "min")
speed = dist / time
print(f"The speed is {speed}")
# Let's do a more sensible unit.
print(f"The speed is {speed.to('mile h^-1')}")
```

The speed is 2.1666666666666665 mile min^-1 The speed is 130.0 mile h^-1

**space-separated powers of units**. Powers are specified using "^". E.g., "kg m s^-1". Only **integer powers** are supported.

Now, let's do some basic science.

In [3]:

```
g = mg.units.FloatWithUnit(9.81, "m s^-2") # Acceleration due to gravity
m = mg.units.Mass(2, "kg")
h = mg.units.Length(10, "m")
print(f"The force is {m * g}")
print(f"The potential energy is force is {(m * g * h).to('J')}")
```

The force is 19.62 N The potential energy is force is 196.20000000000002 J

In [4]:

```
made_up = mg.FloatWithUnit(100, "Ha^3 bohr^-2")
print(made_up.to("J^3 ang^-2"))
```

2.9592449550010817e-50 J^3 ang^-2

In [5]:

```
try:
made_up.to("J^2")
except mg.units.UnitError as ex:
print(ex)
```

Units ('kg', 3) and ('kg', 2) are not compatible!

In [6]:

```
dists = mg.units.LengthArray([1, 2, 3], "mile")
times = mg.units.TimeArray([0.11, 0.12, 0.23], "h")
print(f"Speeds are {dists / times}")
```

Speeds are [ 9.09090909 16.66666667 13.04347826] mile h^-1