Python Numbers

Learn Python numeric types including integers, floating-point numbers, and complex numbers. Understand int vs float, type conversion, numeric literals, precision, and when to use each number type.

Published

Updated

Read time 7 min read

Reviewed byDeepak Prasad

Python Numbers

Python gives you three built-in numeric kinds: integers (int), floating-point (float), and complex (complex). Literals pick the type for you—7 is an int, 7.0 is a float, and 1+2j is complex. This guide covers each type, int vs float, bases and underscores, why floats can look “wrong,” conversions, operators, type checks, and common errors. For how Python reports types in general, see type of variable in Python.

Tested on: Python 3.13.3; kernel 6.14.0-37-generic; Ubuntu 25.04.


Python numeric types

Type Used for Example meaning
int Whole numbers counts, indexes, quantities, IDs
float Decimal / scientific notation prices, measurements, averages
complex Real plus imaginary part math, science, signal processing

Integers in Python

An int is a whole number: positive, negative, or zero, with no fractional part. You can group digits with underscores for readability (1_000_000 is the same value as 1000000).

python
positive = 42
negative = -7
zero = 0
big = 9_007_199_254_740_991
print(positive, negative, zero, big)
print(type(positive))
Output

This prints the four values on one line and <class 'int'> for the type.

In Python 3, integers are arbitrary precision in normal use: they grow until memory runs out, unlike fixed-width integers in many lower-level languages (Python 2’s separate long type is gone).

Integer literals in other bases use a prefix: 0b binary, 0o octal, 0x hexadecimal. Each still has type int.

Prefix Number system
0b / 0B Binary
0o / 0O Octal
0x / 0X Hexadecimal
python
print(0b1010, 0o12, 0xFF)
Output

This prints 10 10 255.


Floats in Python

A float is a binary floating-point number. It can be written with a decimal point (3.14), scientific notation (1.5e-3), or underscores (1_000.5). Values can be very large or very small in magnitude, but many decimal fractions cannot be represented exactly—small rounding differences are normal.

python
a = 10.0
b = -2.5
c = 6.02e23
d = 1_234.5
print(a, b, c, d)
Output

For money or exact decimal arithmetic, prefer decimal.Decimal instead of bare float.


int vs float in Python

Feature int float
Value type Whole number Fractional / scientific
Precision Exact for integers Approximate for many decimals
Decimal point No Yes
Typical use counts, indexes, IDs measurements, prices, averages
Arithmetic Exact while result fits integer rules Often promotes mixed expressions to float

When to use int and when to use float

Use int for counts, loop indexes, user IDs, numbers of files, whole-year ages, and any quantity that must stay whole.

Use float for prices (when Decimal is not required yet), percentages, height, weight, distance, averages, temperature, and other measurements where a fractional part is normal.

python
# int: things you count or index discretely
user_id = 1042
file_count = 12
age_years = 36
row_index = 0

# float: measurements that can be fractional
price_usd = 19.99
temperature_c = 36.6
distance_km = 42.195

print(type(user_id).__name__, type(price_usd).__name__)
print(file_count, age_years, row_index)
print(price_usd, temperature_c, distance_km)
Output

The first line prints int float; the next two lines show the sample values with their usual types.


Why float values can be imprecise

Computers store most float values in base two. Decimals such as 0.1 have no finite binary expansion, so stored values are approximations. That is why 0.1 + 0.2 == 0.3 can be false while the difference is tiny.

python
x = 0.1 + 0.2
print(x)
print(x == 0.3)
Output

You usually see 0.30000000000000004 and False. For equality checks on floats, compare with a tolerance or round to a fixed number of decimal places; for financial rules, use Decimal.


Python complex numbers

A complex value has a real and an imaginary part. The imaginary unit is written as j (3+4j). You can also call complex(real, imag).

python
z = 2 + 4j
print(z.real, z.imag, z.conjugate())
Output

This prints 2.0 4.0 and the conjugate representation for (2-4j) as a complex value. Most everyday programs rarely need complex; int and float cover typical data work.


Converting between int, float, and complex

Call Role
int(x) Truncate toward zero for floats; parse strings such as "42"
float(x) Produce a floating-point value
complex(re, im) Build a complex number from components

Valid numeric strings convert; invalid text raises ValueError. Conversion details for floats and strings are expanded in the next sections.


Convert float to int in Python

int(x) on a float drops the fractional part toward zero—it does not “round to nearest” unless you use round() first.

python
print(int(98.6))
print(int(-2.7))
Output

This prints 98 and -2.

Requirement Typical approach
Remove fractional part int(x) (truncate toward zero)
Round to nearest integer round(x) then possibly int(...)
Floor math.floor()
Ceil math.ceil()

Convert int to float and string to number

float(3) yields 3.0, which is useful before division-heavy code or APIs that expect floats. Very large integers may lose precision if you convert them to float because float has finite range and mantissa bits.

Strings must contain only a valid literal (optional surrounding whitespace is allowed for int/float):

python
print(int(" -7 "))
print(float("3.14"))
Output

This prints -7 and 3.14. A string like "10px" raises ValueError.


Numeric operations and mixed int and float

Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ True division (result is float in Python 3)
// Floor division
% Remainder
** Power

Mixed int and float operands usually produce a float (1 + 1.0 == 2.0). Using / on two integers still yields a float. Floor division // returns int when both operands are int and float when either operand is float. See Python operators for precedence and the full operator list.


Check numeric type in Python

Use type(x) to print or inspect a single value’s type, and isinstance(x, (int, float, complex)) in real code when you accept several numeric kinds.

python
n = 5
print(type(n))
print(isinstance(n, int))
Output

Because bool is a subclass of int, isinstance(True, int) is True. When you must exclude booleans, test type(x) is int or isinstance(x, bool) first. Returning conversions from functions is covered in return values from functions.


Common numeric errors

  • ValueError when int("12a") or float("3.1.4") cannot parse.
  • TypeError when applying numeric operators to non-numeric types without converting first.
  • Surprise near-equality on floats; avoid naive == for decimals.
  • Using commas as thousands separators (1,000) builds a tuple in Python, not an integer—use underscores instead.
  • Leading zero on plain integer literals (0123) is a syntax error in Python 3; use 0o for octal.

For handling failures in your own APIs, see custom exceptions in Python.


Python numbers quick reference

Task Approach
Store a whole number int literal or int()
Store a decimal measurement float literal or float()
Store real + imaginary complex(...) or x+yj
Truncate float to int int(x)
Parse a numeric string int(s) / float(s) with validation
Inspect type type(x), isinstance(x, T)
Exact decimal rules decimal.Decimal

int vs float vs complex summary

Type Exact? Decimal fraction? Common use
int Yes for integers No counts, indexes
float Approximate for many decimals Yes science, UI, stats
complex Components are floats Yes (imaginary axis) engineering math

When should you use each numeric type?

Use int when the value must stay whole, represents a count or index, or you need exact integer arithmetic. Use float when fractional values are inherent and small rounding error is acceptable, or when libraries expect floats. Use complex when the domain truly needs imaginary numbers; otherwise skip it to keep models simple.


Summary

  • Python’s main numeric types are int, float, and complex; literals choose the type for you.
  • int is unbounded in practice and supports binary/octal/hex prefixes and digit underscores.
  • float uses binary floating point—handy for science, but not exact for every decimal; use Decimal when money or exact decimals matter.
  • Conversions use int(), float(), and complex(); int on a float truncates toward zero, not “normal rounding.”
  • Mixed arithmetic with float tends to promote results to float; / always produces float in Python 3.
  • Check types with type and isinstance, remembering bool is an int subclass.

References


Frequently Asked Questions

1. What is the difference between int and float in Python?

int stores whole numbers exactly; float stores binary floating-point values that often approximate decimals, and mixing int with float in arithmetic usually promotes the result to float.

2. Does int() round a float in Python?

int(x) truncates toward zero—it drops the fractional part; use round(), math.floor(), or math.ceil() when you need rounding behavior instead of truncation.

3. How do I check if a value is an int and not a bool?

Because bool is a subclass of int, isinstance(True, int) is True; use type(x) is int when you must exclude booleans, or check isinstance(x, bool) first.
Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with more than 15 years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive …