Built-in Knowledge: Entities, Units, and Dates
Prerequisites: Associations
The Wolfram Language includes curated data about the real world, and types for units and dates that make calculations with them safe.
Quantities and units
UnitConvert[Quantity[26.2, "Miles"], "Kilometers"]
Quantity[100, "Kilometers"]/Quantity[2, "Hours"]
UnitConvert[Quantity[100., "DegreesFahrenheit"], "DegreesCelsius"]
QuantityMagnitude[Quantity[5, "Meters"]]
42.1648 km
50 km/h
37.7778 °C
5
Quantity[magnitude, unit]carries its unit through arithmetic; dividing kilometers by hours gives a speed.- Adding incompatible units (meters plus seconds) is an error instead of a silent bug.
- Unit names are spelled out (
"Meters","Pounds"); in a notebook, Ctrl + = lets you type natural language like3 feetand converts it to the rightQuantity(this needs an internet connection).
Dates
DateDifference[DateObject[{2024, 1, 1}, "Day"], DateObject[{2024, 3, 15}, "Day"]]
DayName[DateObject[{2024, 3, 15}]]
74 days
Friday
DateObject, DatePlus, DateRange, DateString, and Now cover date
arithmetic and formatting, including time zones.
Entities
An entity is a real-world thing — a country, a city, a chemical, a planet — with properties you can query. Entity data comes from Wolfram's servers, so these need an internet connection, and values such as populations change over time:
(* illustrative: needs an internet connection *)
france = Entity["Country", "France"];
france["Capital"]
france["Population"]
EntityValue[EntityClass["Country", "EuropeanUnion"], "Population"]
Interpreter["Country"]["japan"]
france["Capital"]returns another entity — Paris — that you can query further.france["Population"]returns aQuantityof people.EntityValuefetches a property for many entities at once.Interpreterturns free text into entities, dates, numbers, and more.
Similar domain functions exist for chemistry (ChemicalData), finance
(FinancialData), astronomy (PlanetData), geography (GeoDistance,
GeoGraphics maps), and dozens of other areas.
Free-form input
In a notebook, starting a cell with = sends it to Wolfram|Alpha and
returns the answer along with Wolfram Language code you can reuse.
WolframAlpha["query"] does the same from code.
Why it matters
Built-in knowledge saves the data-gathering step: a question like "how
does GDP per person relate to life expectancy across countries?" is a few
lines of code, and units keep the calculations honest. For reproducible
work, store the data you fetched (for example with Export or
DumpSave), since curated values are updated over time.