A data type specifies the kinds of values a program can work with and the operations they support. A concrete representation also determines how those values are stored. In quant work, the choice can decide whether an instrument identifier survives an import and whether a cash calculation preserves its intended precision. Python’s data model makes type a property of each object.
Digits alone do not tell you which type to choose. An account code is an identity; a position size is a quantity. Both might arrive in a file as text.
Preserve an identifier; calculate a quantity
Suppose a fictional feed supplies the identifier
"0012" and two USD cash amounts already expressed in whole cents. Keep the identifier as text and use integers for the fixed-scale amounts:instrument_id = "0012"
parsed_id = int(instrument_id)
cash_cents = [105, 215]
print(instrument_id, type(instrument_id).__name__, len(instrument_id))
# 0012 str 4
print(parsed_id, type(parsed_id).__name__)
# 12 int
print(sum(cash_cents))
# 320
print("105" + "215")
# 105215The conversion to
int succeeds, but the result contains no record of the original leading zeros. Converting 12 back to text gives "12"; restoring "0012" requires a separately known width rule. For an identifier used to join position and reference data, losing that distinction can break a match.The cash total is 320 cents, or USD 3.20, under the stated scale of 100 cents per USD. Text concatenation produces
"105215", not a monetary sum. These behaviours follow the built-in numeric and sequence operations. Successful execution does not establish that an operation makes financial sense.Choose the representation from the field’s meaning. Identifier text and a fixed-scale monetary quantity need different operations, even when both contain digits.
A Python type and a NumPy dtype answer different questions
Python is dynamically typed: a name can be rebound to an object of another type. This does not mean objects have no types or that arbitrary operations are valid. In the example,
instrument_id refers to a str and parsed_id to an int. Rebinding a name does not turn the original string object into an integer.A NumPy array is itself an
ndarray object. Its dtype describes how each array item is represented. For an ordinary numerical array, an explicit dtype fixes the numerical representation across its elements:import numpy as np
cash_cents = np.array([105, 215], dtype=np.int64)
print(type(cash_cents).__name__, cash_cents.dtype)
# ndarray int64
print(int(cash_cents.sum()), cash_cents.itemsize)
# 320 8Here each integer item occupies 8 bytes. That is the item size, not the array’s total memory footprint. NumPy’s dtype reference also covers structured records and object references: an object-dtype array can refer to Python objects of different types. A dtype need not describe a simple column of numbers.
Python integers can grow beyond a fixed machine width, subject to available memory. NumPy’s fixed-width integers have bounded ranges, so integer overflow is a separate risk. Inspect range and calculation rules before choosing a narrower dtype; do not assume a valid input guarantees a representable result. See NumPy’s data-type guide.
Examples were executed with Python 3.14.4 and NumPy 2.5.1. The sources describe Python 3.14 and NumPy 2.5.
Representation is only part of the contract
| Field meaning | Possible representation | Additional rule to specify |
|---|---|---|
| Instrument identifier | str | Preserve significant characters; validate the identifier format |
| Cash amount at a fixed scale | Integer cents | Currency, scale, range and treatment of fractional cents |
| Model output or return | Binary floating point | Units, acceptable error and handling of nonfinite values |
| Eligibility flag | bool | How unknown or missing eligibility is represented |
The integer
105 does not carry “USD cents” in its type. Two arrays with the same dtype can use different currencies or scales. Validate those conventions at the data boundary, alongside missing-value and domain rules.Binary floating point is useful for numerical models, but many decimal fractions are approximated; display formatting does not change the stored value. Where a monetary workflow requires decimal arithmetic, Python’s
decimal module offers decimal representation with explicit precision and rounding controls. Integer cents also need a rounding policy when an operation produces fractional cents. No single numeric type settles every monetary convention.Finally, a type annotation describes an intended contract for tools and readers. Writing an annotation does not itself convert input, validate currency or enforce that contract at runtime, as the Python typing documentation states.
Further learning
Python Syntax and Control Flow applies numbers, strings and Boolean decisions to a position rule; the full lesson is free with sign-in. NumPy develops array shape, dtype and numerical operations; its full lesson requires membership, with a public preview available.