For AI agents: use /llms.txt for the Nakafa content index.
Python has built-in numeric types for whole numbers, approximate real-number calculations, and complex numbers. Choosing the right type matters because each one represents values and performs arithmetic differently.
Python is very flexible in handling numbers. When you type numbers in the Python shell, Python immediately recognizes and displays them back. Let's see how Python works with numbers.
>>> 4
4
>>> 4.
4.0
>>> 0.30
0.3
>>> 0.0000999
9.99e-05
>>> 2. + 1j
(2+1j)Python parses each literal from its notation. creates an integer, while the decimal point in makes it a floating-point literal whose displayed value is .
Python's three core built-in numeric types are int, float, and complex.
An integer is a whole number: positive, negative, or zero. Python integers have arbitrary precision, so their size is limited by available memory rather than a fixed 32-bit or 64-bit range.
Valid integer examples
Special features of integers in Python
Python allows underscores between digits to make long literals easier to scan. For example, has the same value as . The underscores affect only the source notation.
A float stores a binary floating-point approximation. Float literals can contain a decimal point or an exponent. They are useful for measurements and numerical calculations, but many decimal fractions cannot be represented exactly in binary.
Common float examples
Scientific notation for very small or large numbers
The exponent notation e or E is convenient for very small or very large float literals. For example:
The notation or in Python is the same as the scientific notation you learned in mathematics. The number equals , and equals .
Complex numbers have real and imaginary parts. In a Python literal, the suffix marks the coefficient of the imaginary unit. Complex arithmetic is common in signal processing, electrical engineering, and scientific computing.
Complex number examples
Imagine complex numbers like coordinates on a map. The real part is the horizontal position, and the imaginary part is the vertical position.
The type() function reports the exact class of an object. It is useful when exploring values in the Python shell or diagnosing unexpected data.
>>> type(5)
<class 'int'>
>>> type(.1)
<class 'float'>
>>> type(2j)
<class 'complex'>For a strict equality check you can compare type(value) with a class. In ordinary application code, isinstance(value, SomeType) is often more flexible because it also recognizes subclasses.
Python provides constructors to convert or create number objects from other types. This is very useful when you need to convert data from one format to another.
The float() constructor converts other numbers into decimal numbers.
>>> float(2)
2.0
>>> float()
0.0When you call float(2), Python converts the integer to float . If you call float() without arguments, Python gives the default value .
The int() constructor converts a finite float to an integer by truncating toward zero.
>>> int(2.1)
2
>>> int(2.9)
2
>>> int(-2.9)
-2int() does not round to the nearest integer. Both positive examples become , while int(-2.9) becomes because truncation moves toward zero.
The complex() constructor creates complex numbers from real numbers.
>>> complex(2.)
(2+0j)
>>> complex(0., 2.)
2jcomplex(2.) creates a complex number with real part and imaginary part . Meanwhile, complex(0., 2.) creates a pure imaginary number .
| Category | Type | Examples | Description |
|---|---|---|---|
| Integer | int | and | Whole numbers, can use underscore for clarity |
| Floating-Point | float | and | Decimal numbers, supports scientific notation with or |
| Complex | complex | and | Complex numbers with as imaginary unit |