Reputation: 1
I'm very new to python. How can I convert a unit in python? I mean not using a conversion function to do this. Just as a built-in syntax in python, like the complex numbers works. E.g., when I typed 1mm in python command line, and expect the result is 0.001
>>> 1mm
0.001
#Just like the built-in complex numbers or scintific expressions
>>> 1j
1j
>>> 1e3
1000
I totally have no idea, do anybody knows how complex number or scintific expressions work in Python? Or any idea on how to do it.
thanks,
Upvotes: -3
Views: 3167
Reputation: 1
Python doens't have built-in units, so you'll need to install a package specifically for that.
Axiompy is a package that can do this.
Install with pip install axiompy
If you want to convert from millimetres to metres, like in the question, you'd use:
from axiompy import Units
units = Units()
print(units.unit_convert(3 * units.millimetre, units.metre))
Upvotes: 0
Reputation: 40993
If you are doing scientific work with physical units, it is a good idea to use a units library (not built-in) like quantities which also supports scientific packages like numpy. For example:
>>> from quantities import meter
>>> q = 1 * meter
>>> q.units = 'ft' # or 'foot' or 'feet'
>>> print q
3.280839895013123 ft
Upvotes: 0
Reputation: 114048
how bout
mm = 0.001
1*mm
not sure if that is what you are asking for ... if you have ever messed with report lab they do simillar stuff. (although they use it to convert pixels to actual border sizes and what not)
eg:
inch = DPI*some_thing
margin = 2*inch
Upvotes: 1