🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEscipy

scipy Documentation

LOADING ENGINE...

constants.metric

AI & DATA SCIENCE // constants-metric

scipy.constants includes a large collection of metric (SI) unit conversion constants and prefix multipliers, like scipy.constants.metric_ton and scipy.constants.kilo, for converting between everyday and SI units.

Syntax

from scipy import constants
constants.metric_ton
constants.kilo

Deep Dive Course

Beyond physical constants like the speed of light, scipy.constants also bundles a large set of plain unit-conversion multipliers — SI prefixes, kilo equals 1e3, mega equals 1e6, milli equals 1e-3, and so on, and named unit conversions, metric_ton equals 1000, meaning 1 metric ton equals 1000 kilograms, along with mile, inch, pound, gallon, and many others — all expressed as the multiplication factor needed to convert that unit into the corresponding SI base unit. This makes unit conversions in code both self-documenting and less error-prone than hardcoding a conversion factor you have to look up and verify yourself each time.

1Understanding constants.metric

Beyond physical constants like the speed of light, scipy.constants also bundles a large set of plain unit-conversion multipliers — SI prefixes, kilo equals 1e3, mega equals 1e6, milli equals 1e-3, and so on, and named unit conversions, metric_ton equals 1000, meaning 1 metric ton equals 1000 kilograms, along with mile, inch, pound, gallon, and many others — all expressed as the multiplication factor needed to convert that unit into the corresponding SI base unit. This makes unit conversions in code both self-documenting and less error-prone than hardcoding a conversion factor you have to look up and verify yourself each time.

💡

Use scipy.constants.find('ton'), or similar searches, to discover the exact name of a specific unit conversion constant you need, rather than guessing at its name or hardcoding the conversion factor yourself.

editor.html
from scipy import constants

weight_tons = 2.5
weight_kg = weight_tons * constants.metric_ton
print(weight_kg)
localhost:3000

2Practical Example

Here is a real-world application of constants.metric showing how it is used in production SciPy code.

editor.html
from scipy import constants

distance_km = 5 * constants.kilo
print(distance_km)
localhost:3000

3Best Practices

Follow these guidelines when working with constants.metric:

1. Use scipy.constants' named unit conversions instead of hardcoding conversion factors, for both readability and to avoid manual arithmetic mistakes

2. Use constants.find(substring) to search for a specific constant's exact name when you're not sure how it's spelled or named

3. Multiply by the SI-prefix constants, kilo, mega, milli, etc., instead of writing out the equivalent power-of-ten literal directly, for clarity about what the number represents

⚠️

Tip: Use scipy.constants.find('ton'), or similar searches, to discover the exact name of a specific unit conversion constant you need, rather than guessing at its name or hardcoding the conversion factor yourself.

editor.html
from scipy import constants

weight_tons = 2.5
weight_kg = weight_tons * constants.metric_ton
print(weight_kg)
localhost:3000

Examples

Example 01Basic Usage
from scipy import constants

weight_tons = 2.5
weight_kg = weight_tons * constants.metric_ton
print(weight_kg)
Example 02Advanced Example
from scipy import constants

distance_km = 5 * constants.kilo
print(distance_km)

Best Practices

  • Use scipy.constants' named unit conversions instead of hardcoding conversion factors, for both readability and to avoid manual arithmetic mistakes
  • Use constants.find(substring) to search for a specific constant's exact name when you're not sure how it's spelled or named
  • Multiply by the SI-prefix constants, kilo, mega, milli, etc., instead of writing out the equivalent power-of-ten literal directly, for clarity about what the number represents

Interview Question

Why does scipy.constants.kilo equal 1000 as a plain float, rather than being a specialized 'unit' object?

Hint: Think about what these SI-prefix constants are actually meant to do — represent a value, or represent a unit conversion factor.

Each of these constants is simply the numeric multiplier needed to convert a value expressed in that prefix/unit into the corresponding SI base unit — kilo is 1000 because multiplying a value in kilometers by 1000 converts it to meters, the SI base unit for length. They're deliberately kept as plain floats rather than wrapped in a specialized unit-aware object, keeping the API simple: you multiply your value by the constant to convert into SI base units, or divide by it to convert from SI base units back into that unit, without needing a more complex unit-tracking system.

Exercises

MediumPractice using constants.metric in a real scenario.
View Solution
from scipy import constants

weight_tons = 2.5
weight_kg = weight_tons * constants.metric_ton
print(weight_kg)

Frequently Asked Questions

Why does scipy.constants.kilo equal 1000 as a plain float, rather than being a specialized 'unit' object?

Each of these constants is simply the numeric multiplier needed to convert a value expressed in that prefix/unit into the corresponding SI base unit — kilo is 1000 because multiplying a value in kilometers by 1000 converts it to meters, the SI base unit for length. They're deliberately kept as plain floats rather than wrapped in a specialized unit-aware object, keeping the API simple: you multiply your value by the constant to convert into SI base units, or divide by it to convert from SI base units back into that unit, without needing a more complex unit-tracking system.

Related Functions

constants-piconstants-cdf-astype