Numeric Types Begin with Literal Notation
Python has three built-in numeric types: int for integers, float for approximate real-number calculations, and complex for complex numbers. The type determines how Python represents a value and performs arithmetic with it.
The interactive shell evaluates a numeric literal and displays its value. Compare the literals below.
>>> 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 .
Types of Number Data
Python's three core built-in numeric types are int, float, and complex. An int stores a whole number of unlimited size, a float stores an approximation of a real number, and a complex stores a real and an imaginary part.
Integer
An integer is a whole number: positive, negative, or zero. Python integers can grow as large as the available memory allows. They do not have a fixed 32-bit or 64-bit limit.
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.
Float
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
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
A complex number can be plotted on a coordinate plane. Its real part sets the horizontal coordinate, and its imaginary part sets the vertical coordinate.
The type Function for Checking Data Types
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.
Constructors for Creating Number Objects
Python provides constructors that create numeric objects from other types. Use them, for example, to convert text input into a number before calculating with it.
Constructor float
The float() constructor converts another number or valid numeric text into a floating-point value.
>>> 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 .
Constructor int
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.
Constructor complex
The complex() constructor creates complex numbers from one or two 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 .
Number Data Types Summary Table
The table collects the three numeric types with examples and a short description, so you can look up a type in one place.
| 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 |