Numbers in Python: Integer, Float and Complex Types
Python's three numeric types are int, float, and complex. Code examples cover each type, type conversion rules, and built-in math functions.
Python has three numeric types: int for whole numbers, float for decimal values, and complex for numbers with a real and imaginary part.
The Python documentation on numeric types defines all three as subtypes of the abstract numbers.Number hierarchy. In practice, they behave differently in placement coding questions: the types have different precision characteristics, different conversion rules, and different built-in operations. This article covers each type with verified code examples and arithmetic.
The int type
Python 3 integers are whole numbers with arbitrary precision. There is no upper limit set by the language itself, only by the system’s available memory. That separates Python from C and Java, where int is bounded by 32 bits and wraps around at a fixed maximum. A Python integer can grow to millions of digits if needed.
x = 1
y = 882399773218279
z = -125634
print(type(x)) # <class 'int'>
print(type(y)) # <class 'int'>
print(type(z)) # <class 'int'>
The type() built-in returns the type of any value. For integers, the output is <class 'int'> regardless of the number’s size or sign.
Base prefixes for integer literals
Python allows writing integer literals in binary, octal, and hexadecimal as well as the default decimal:
| Base | Prefix | Example literal | Decimal value |
|---|---|---|---|
| Binary (base 2) | 0b or 0B | 0b1100 | 12 |
| Octal (base 8) | 0o or 0O | 0o14 | 12 |
| Hexadecimal (base 16) | 0x or 0X | 0xC | 12 |
All three store identically as a plain int at runtime. The prefix is a source-code notation only.
a = 0b1100 # binary: 1*8 + 1*4 + 0*2 + 0*1 = 12
b = 0o14 # octal: 1*8 + 4*1 = 12
c = 0xC # hexadecimal: C = 12
print(a) # 12
print(b) # 12
print(c) # 12
print(type(a)) # <class 'int'>
Placement questions sometimes test what type(0b1100) returns. The answer is <class 'int'>, not anything related to binary notation.
Python 3.6 and later also accept a _ separator inside numeric literals to improve readability in long numbers: 1_000_000 is a valid integer literal equal to one million. The _ is ignored at parse time; only the digits matter.
The digit-extraction pattern used in the Armstrong number check (% 10 to extract digits, // 10 to shift right) is one of the most common integer manipulation idioms in placement coding rounds.
The float type
A Python float is a decimal number stored in 64-bit IEEE 754 double-precision format. This standard gives approximately 15 to 16 significant decimal digits of precision. Decimal strings that exceed that precision are stored with rounding.
x = 12.3
y = 12.9829379485794548679 # 20 significant digits in the literal
z = -18.96
print(type(x)) # <class 'float'>
print(type(y)) # <class 'float'>
print(type(z)) # <class 'float'>
print(x) # 12.3
print(y) # 12.982937948579455 (stored at ~15 significant digits)
print(z) # -18.96
The 20-digit literal for y is stored as 12.982937948579455. The digits beyond position 15 are lost at the moment of storage, not at print time. This matters in financial or scientific code where rounding errors accumulate across operations.
Scientific notation
Python accepts float literals in scientific notation using e or E:
a = 23e2 # 23 * 10**2 = 2300.0
b = 1.5e-3 # 1.5 * 10**-3 = 0.0015
print(a) # 2300.0
print(b) # 0.0015
print(type(a)) # <class 'float'>
Scientific notation produces a float result regardless of whether the value is a whole number. 23e2 evaluates to 2300.0, not 2300.
Complex numbers in Python
Complex numbers take the form a + bj, where a is the real part and b is the imaginary coefficient. Python uses j as the imaginary unit, following the engineering convention rather than the mathematical convention of using i. Both conventions refer to the square root of -1.
x = -5j
y = 2 + 4j
z = 22j
print(type(x)) # <class 'complex'>
print(type(y)) # <class 'complex'>
print(type(z)) # <class 'complex'>
The .real and .imag attributes return the components of a complex number. Both attributes return float values, even when the components are whole numbers:
c = 3 + 7j
print(c.real) # 3.0
print(c.imag) # 7.0
One common error in placement questions: writing j alone is a variable name, not the imaginary unit. To write the number one times the imaginary unit, write 1j, not j.
Complex numbers appear in placement prep mainly in Python questions that test data types, type conversion errors, and type() identification. They are used directly in engineering and signal-processing code, but that goes beyond the scope of most campus placement rounds.
Type conversion between number types
Python provides three built-in functions for converting between numeric types:
int(x)— converts a float to int by truncating toward zero.int(3.9)gives3.int(-3.9)gives-3.float(x)— converts an int to float, adding a decimal point.float(5)gives5.0.complex(real, imag)— constructs a complex number. Theimagargument defaults to0when omitted.
b = 13.16
a = 5
print(int(b)) # 13 (float to int: truncated toward zero)
print(float(a)) # 5.0 (int to float)
print(complex(a)) # (5+0j) (int to complex, imaginary part = 0)
print(complex(b)) # (13.16+0j) (float to complex, imaginary part = 0)
print(complex(a, b)) # (5+13.16j) (explicit real and imaginary parts)
One conversion is blocked entirely: you cannot convert a complex number directly to int or float. Calling int(2+4j) raises a TypeError: can't convert complex to int. The reason is that the conversion would require discarding the imaginary part without telling the programmer which part to keep, and Python treats that as an error rather than making the assumption silently.
To convert a complex number’s real part to int, access .real explicitly first:
c = 2 + 4j
print(int(c.real)) # 2
The conversion hierarchy flows one direction: int can become float, and float can become complex. Reversing either step requires explicit handling.
The Python calculator program shows the practical side of these conversions: int(input(...)) parses user input as an integer, and switching to float(input(...)) changes the behaviour of the division result.
Built-in math functions for numbers
Python includes four built-in functions that work across all three numeric types without any import:
| Function | What it does | Example | Result |
|---|---|---|---|
abs(x) | Absolute value | abs(-10) | 10 |
pow(x, y) | x raised to the power y | pow(2, 3) | 8 |
round(x, n) | Rounds to n decimal places | round(12.3456, 2) | 12.35 |
divmod(x, y) | Returns (quotient, remainder) as a tuple | divmod(17, 5) | (3, 2) |
For square roots and trigonometric operations, the math module is needed:
import math
print(abs(-10)) # 10
print(pow(2, 3)) # 8
print(round(12.3456, 2)) # 12.35
print(math.sqrt(25)) # 5.0
The Python math module documentation lists all available functions. math.sqrt() always returns a float even when the result is a whole number. For integer-only square roots (Python 3.8 and later), math.isqrt(25) returns 5 as an int.
divmod(17, 5) returns (3, 2) because 17 divided by 5 is 3 with a remainder of 2. The tuple packs the quotient and remainder together, which is useful when both are needed at once. Placement questions on number theory, like checking divisibility or computing modular arithmetic, often use this pattern.
For a set of practice programs that apply these functions together with conditionals and loops, the Python example programs collection covers beginner-to-intermediate exercises across each numeric type.
Knowing when int() truncates, when float() adds a decimal point, and when a TypeError blocks a conversion is the kind of type-system detail that Python placement questions probe. If you want to run these conversions live and ask an LLM to explain what happens at each step, TinkerLLM provides a Python-ready environment for ₹299.
Primary sources
Frequently asked questions
Can Python integers overflow or have a maximum value?
No. Python 3 integers have arbitrary precision and grow as large as available memory allows. There is no equivalent of INT_MAX from C or Java. Python 2 had a separate long type for large integers; Python 3 merged both into a single int.
Why does Python use j for the imaginary unit instead of i?
Python follows the engineering convention, where j denotes the imaginary unit. Mathematicians typically use i. Both refer to the square root of -1. To write the imaginary unit in Python, use 1j. Note that j alone is treated as a variable name, not the imaginary unit.
How precise are Python floats?
Python floats follow the IEEE 754 double-precision standard and carry about 15 to 16 significant decimal digits. A literal like 12.9829379485794548679 is stored and displayed with limited precision — Python shows 12.982937948579455.
How do I convert a float to int in Python?
Use int(x). Python truncates toward zero: int(3.9) returns 3, and int(-3.9) returns -3. Use math.floor() for floor rounding or math.ceil() for ceiling rounding when you need those instead of truncation.
Can I convert a complex number directly to int or float?
No. int(2+4j) and float(2+4j) both raise a TypeError. Access the real part first with (2+4j).real, which returns a float, then convert that float to int if needed.
How do I write binary or hexadecimal integers in Python?
Use 0b or 0B for binary (0b1100 equals 12), 0o or 0O for octal (0o14 equals 12), and 0x or 0X for hexadecimal (0xC equals 12). All three evaluate to the same int type at runtime regardless of which prefix you use.
A self-paced playground for building with LLMs.
TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.
Try TinkerLLM (₹299 launch)