Skip to content
Tutorials Center
My learning

Topics

Python Reference ยท Numbers and Binary Data

Python numeric types and conversion

How Python's numeric types work: int, float, complex and bool, mixed arithmetic and promotion rules, conversion functions, rounding and the pitfalls to avoid.

Python has three built-in number types: int (whole numbers of any size), float (64-bit binary floating point) and complex (a pair of floats). bool is a subclass of int, so True behaves like 1 and False like 0.

Beyond the built-ins, the standard library adds decimal.Decimal for exact decimal arithmetic and fractions.Fraction for exact ratios. This page explains how the types mix and how to convert between them.

Try it

Python Mixing types

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Converting and rounding

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

How the types mix

In arithmetic Python converts the "narrower" operand to the "wider" one, following the order bool โ†’ int โ†’ float โ†’ complex. So 1 + 2.0 is a float, and 1 + 2j is a complex.

  • Division / always returns a float, even for 6 / 3. Floor division // keeps the type: int for two ints, float if either operand is a float.
  • Integers never overflow. Very large ints just use more memory. Converting a huge int to float raises OverflowError.
  • Floats are approximate. 0.1 + 0.2 is not exactly 0.3. Compare with math.isclose(), or use Decimal or Fraction when you need exactness.
  • Equal numbers hash equally. 1, 1.0 and True are equal and have the same hash, so they collide as dict keys or set elements.

Conversion functions

Call Result
int(x) Truncates a float toward zero. Parses strings (surrounding spaces and _ between digits allowed). int(s, base) parses other bases; base 0 reads prefixes like 0x.
float(x) Parses decimal text including "inf", "nan" and exponents.
complex(real, imag) or complex("1+2j") The string must not contain spaces around +.
bool(x) False for zero, empty containers, None; otherwise True.
round(x, n) Rounds half to even, so round(2.5) is 2.
math.floor, math.ceil, math.trunc Round toward negative infinity, positive infinity and zero.

Usage notes

  • int("3.5") fails. Convert through a float first: int(float("3.5")).
  • round() uses banker's rounding and works on the stored binary value, so round(2.675, 2) gives 2.67.
  • bool is an int, so sum([True, False, True]) is 2. That is useful for counting, but confusing in isinstance(x, int) checks.
  • Complex numbers cannot be ordered: 1j < 2j raises TypeError. Equality works.
  • Division by zero raises ZeroDivisionError for ints and floats alike; floats can overflow to inf in some operations like 1e308 * 10.
  • Underscores in literals (1_000_000) are ignored and only help readability.