Lesson · Language Fundamentals
Built-in Scalar Types
57 min readBuilt-in Scalar Types
Python's scalar objects represent numbers, truth values, and the absence of a value. Their operations look simple, but details such as arbitrary-precision integers and floating-point approximation matter in real programs.
Learning goals
- Name the five scalar types in the worked example,
int,float,complex,bool, andNone, and prove each withtype. - Explain why Python integers never overflow, and what arbitrary precision costs as values grow.
- Demonstrate that
0.1 + 0.2is not exactly0.3and say why the stored values are approximations. - Use
boolas a subclass ofintthat still expresses logical intent, andNoneas the dedicated "no value" object. - Choose between
type(x) == intandisinstance(x, int)and say what each check rejects.
Mental model
The five types split into three ideas: the number types (int, float, complex), the truth type (bool, which is also a kind of int), and None, the single object that means "no value". Two rules tie them together: conversions between number types are explicit function calls, and every value can answer type(value).__name__ with one of these names.
Core idea
Every value in the worked example is an object with a type, and the type decides what its operations mean and how exact the result is. The scalars differ in what they promise: int is exact and unbounded, float is fixed-precision binary and approximate for decimals, bool answers yes-or-no questions, and None marks absence, so the skill is matching the type to the promise the data needs.
int
int is Python's exact whole-number type, with no fixed maximum size: it grows as far as memory allows, and integer arithmetic never overflows. Literals like 42 create ints, int() parses strings or truncates floats toward zero, and / produces a float while // stays whole.
float
float holds a 64-bit binary floating-point number and is the result of / and of literals like 3.5. Most decimal fractions have no exact binary representation, so results can be slightly off and should be compared with a tolerance or computed with decimal when exactness matters.
complex
complex stores a real part and an imaginary part and is written with a j suffix, as in 2 + 4j. The parts are read as .real and .imag, abs() returns the magnitude, and operations such as (-1) ** 0.5 produce a complex result.
bool
bool has exactly two instances, True and False, and is a subclass of int with values 1 and 0. It is the type comparisons return, and bool(x) follows the truthiness rules: 0, 0.0, "", empty containers, and None are false.
None
None is the single instance of NoneType, used to mean "no value". It is falsy, functions that end without returning hand it back, and the correct test is x is None because there is only ever one of it.
Integer precision
Python integers are arbitrary precision: they grow to hold larger values instead of overflowing at fixed 32-bit or 64-bit limits. Larger integers consume more memory and computation.
Floating-point approximation
Most decimal fractions have no exact finite representation in binary floating point. The stored value is therefore the nearest representable approximation, which becomes visible in some calculations.
Truth values
True and False are instances of bool, and Boolean contexts also accept other objects through truthiness rules. bool is a subclass of int, but code should still use it to express logical intent.
Numeric conversions
Numeric constructors such as int() and float() convert compatible values explicitly. Conversion can lose information, such as truncation when converting a float to an integer.
type()
type(obj) returns the class object that produced obj, so type(42) is the class int and type(42).__name__ is the string "int". Use it to inspect what an object is; use isinstance instead when subclasses should also pass.
isinstance()
isinstance(obj, cls) returns True if the object is an instance of that class or of any subclass of it, and it accepts a tuple of classes to test against several types at once. It is the intended check for "is this the kind of thing I can handle", while type(obj) is cls accepts no subclasses.
Walkthrough
- The list literal builds five objects of five types:
42is anint,3.5afloat,2 + 4jacomplex,Trueabool, andNoneis the singleNoneTypeinstance. - The loop binds each object to
valuein turn;repr(value)shows the value in a form close to its source spelling, andtype(value)returns the class object whose__name__reads as a plain string for display. Truedeserves a closer look:boolsubclassesint, soTrue == 1andisinstance(True, int)both hold, buttype(True).__name__printsboolbecausetypereports the exact class.- Predict before running: the five names in output order, what
repr(3.5)shows, and the name printed forTrue.
Experiments to try
- Add
10 ** 50tovaluesand rerun; confirmintrepresents it exactly, then multiply two such values and note that nothing overflows. - Print
0.1 + 0.2and compare the result with0.3using==and thenmath.isclose; explain why the two comparisons disagree. - Print
isinstance(True, int),True + True, andtype(True) is int; explain each result usingboolas anintsubclass. - Convert in both directions with
int(3.9),float(1),complex(3), andbool(""); record which conversions lose information and which only classify.
Common mistakes
- Expecting
floatmath to be exact for decimal fractions:0.1 + 0.2prints0.30000000000000004because both operands are stored as the nearest binary approximations, not the decimal values. - Comparing floats with
==after a computation: the tiny representation error makes exact equality unreliable, so usemath.iscloseorDecimalwhen the comparison matters. - Using
type(x) == intfor a value that might be a subclass:Trueis anintbuttype(True) is intisFalse, so the check rejects valid inputs;isinstance(x, int)accepts them. - Confusing
Nonewith0or"": all three are falsy, but onlyNonemeans "no value", so code that testsif not xtreats all three alike; compare explicitly withx is Nonewhen absence is what you mean.
Practice: Easy
- Run the worked example, then add
print(type(value) is int)inside the loop and predict which of the five values printsTrue. - Bind
x = 42and printisinstance(x, int)andisinstance(x, (int, float)); state what the tuple form of the second argument allows. - Evaluate
bool(0),bool(0.0),bool(""),bool([]), andbool(None); group the results and write the rule in one line.
Practice: Medium
- Compute
0.1 + 0.2 - 0.3and print it withrepr; then compare0.1 + 0.2with0.3usingmath.iscloseand explain why one comparison succeeds where==fails. - Explore
complex: print(2 + 4j).real,(2 + 4j).imag,abs(3 + 4j), andcomplex(2, 4); explain what each expression represents. - Show that
Trueis anint: printTrue + True,[True, False].count(1), andisinstance(True, int); decide in one sentence whether summing booleans is good style.
Practice: Hard
- Write a
describe(value)function that printsrepr(value)andtype(value).__name__and returns the type name; test it on one value of each scalar type plus a string and a list. - Build a small table in code with one row per scalar type: a literal,
type().__name__,bool(value), and one supported operation. Fill at least three types. - Investigate precision limits: print
2 ** 53 + 1,float(2 ** 53 + 1), and2 ** 53 + 1 == float(2 ** 53 + 1); explain why the int is exact while the float is not.
Lesson checklist
- I can name the type of each value in the worked example and prove it with
type. - I can explain why integers do not overflow and what that costs.
- I can show and explain one case where float arithmetic is approximate.
- I can choose between
typeandisinstancedepending on whether subclasses should count.
Remember
These five scalar types are the values behind most simple programs: exact but unbounded ints, approximate binary floats, complex numbers with `j`, boolean values that are also ints, and the single `None` object. Read them as a set of promises, and match the type to the promise the data needs.
Interview angles
What is the main purpose of **int**?+
int is the exact whole-number type: it has no maximum size, arithmetic on ints never overflows, and division with / leaves the type behind to produce a float. Counting, indexing, and any whole-number work where exactness matters should stay in int.
What should you pay attention to when using **float**?+
float stores numbers as binary fractions in a fixed 64-bit layout, so most decimals are approximations (0.1 is not exact). It is the right type for measurement-like work where a tiny error is acceptable, and the wrong one for exact decimal values such as money.
How does **complex** fit into this lesson?+
complex represents numbers with a real and an imaginary part, written with a j suffix (2 + 4j). The parts are read as .real and .imag, abs() gives the magnitude, and Python uses the type wherever complex results arise ((-1) ** 0.5).
Why does `type(True) is int` print `False` even though `isinstance(True, int)` is `True`?+
type returns the exact class of the object, and the class of True is bool; isinstance also accepts subclasses, and bool subclasses int. Use isinstance when subclasses should pass and type(...) is only when the exact type matters.