Python Variable & Data Type

Python Numbers

Learn Python Numbers with clear explanations, Python examples, and practical programming guidance.

What are numbers in Python?

Numbers are built-in values used for counting, measuring, calculating, and representing mathematical quantities. Python provides three main numeric types: integers, floating-point numbers, and complex numbers.

TypeExampleWhen to use it
int42, -7, 0Whole numbers. Python integers can grow beyond a fixed machine-size limit.
float3.14, -0.5, 2e3Numbers with a fractional part or scientific notation.
complex3 + 4jValues with real and imaginary parts, often used in scientific and engineering calculations.

Checking a number's type

Use type() to inspect the class of a numeric value:

whole_number = 6
decimal_number = 8.3
complex_number = 2 - 5j

print(type(whole_number))
print(type(decimal_number))
print(type(complex_number))
<class 'int'>
<class 'float'>
<class 'complex'>

Python integers and arithmetic

An integer is a whole number without a decimal part. It can be positive, negative, or zero. Python integers support arbitrary precision, so the practical limit is available memory:

a = 8
b = 3

print(a + b)   # addition: 11
print(a - b)   # subtraction: 5
print(a * b)   # multiplication: 24
print(a / b)   # true division: 2.6666666666666665
print(a // b)  # floor division: 2
print(a % b)   # remainder: 2
print(a ** b)  # exponentiation: 512

The / operator returns a float, // performs floor division, % returns the remainder, and ** raises a number to a power. Floor division rounds toward negative infinity.

Python floating-point numbers

A floating-point number contains a decimal point or uses scientific notation. Python's ordinary float is normally implemented using IEEE 754 double precision. It is fast and useful, but it cannot represent every decimal fraction exactly.

temperature = 5.85253
balance = -7.23
scientific_value = 3e6

print(temperature)
print(balance)
print(scientific_value)
5.85253
-7.23
3000000.0

Small rounding differences are normal with binary floating-point arithmetic. Use math.isclose() for approximate comparisons and consider decimal.Decimal when exact decimal arithmetic is required, such as financial calculations.

import math

result = 0.1 + 0.2
print(math.isclose(result, 0.3))
True

Python complex numbers

A complex number has a real part and an imaginary part. Python uses j for the imaginary unit:

first = 4 + 7j
second = 6 - 2j

print(first)
print(second)
print(first.real)
print(first.imag)
print(first.conjugate())

Complex values support addition, subtraction, multiplication, division, and exponentiation. The real and imag attributes return the two parts, while conjugate() returns the complex conjugate.

Converting numeric values

Python can convert compatible numeric values implicitly, and you can request a conversion explicitly with int(), float(), or complex():

integer_value = 13
float_value = 4.6

total = integer_value + float_value
quotient = integer_value / 5

print(total, type(total))
print(quotient, type(quotient))

print(int(7.9))
print(float(6))
print(complex(8))
17.6 <class 'float'>
2.6 <class 'float'>
7
6.0
(8+0j)

Converting a float to an integer removes the fractional part; it does not round to the nearest integer. Invalid text conversion raises ValueError, so validate external input before converting it.

Generating random numbers

The random module generates pseudorandom values for simulations, games, testing, and sampling. It is not suitable for passwords or security tokens; use the secrets module for those cases.

import random

print(random.randrange(5, 25))
print(random.random())

colors = ["red", "green", "blue"]
print(random.choice(colors))

Random results vary each time the program runs. Use random.seed() when a repeatable sequence is useful for a test or demonstration.

Mathematical operations with the math module

The math module provides constants and functions for square roots, logarithms, trigonometry, factorials, and other common calculations:

import math

print(math.pi)
print(math.sqrt(81))
print(math.log10(10000))
print(math.factorial(5))
3.141592653589793
9.0
4.0
120

Tips for working with Python numbers

  • Use int for exact whole-number counts and indexes.
  • Use float for approximate measurements and general decimal calculations.
  • Use decimal.Decimal when exact decimal arithmetic is important.
  • Use math.isclose() instead of direct equality for approximate float results.
  • Use complex when the problem requires real and imaginary components.
  • Use secrets, not random, for security-sensitive random values.

Further Reading

Continue learning with these related Python tutorials:

Continue learning