Understanding Numbers in Python: A Deep Dive into Numeric Types, Operations, and Precision

Harsh GuptaHarsh Gupta
5 min read

Python’s number system isn’t just about int and float. In fact, numbers in Python include a rich collection of types such as decimals, fractions, complex numbers, and even booleans and sets. Let’s explore how Python treats and manipulates these data types.

📌 Numbers Are Objects in Python

Fire up your Python shell:

x = 2
y = 3
z = 4
x + y * z  # Avoid this in production code
x + (y * z)  # Use this for clarity

Python performs arithmetic with type promotion, meaning the result adopts the more precise type:

40 + 2.23  # Outputs: 42.23
# make them type consistant, convert 40 to type float or convert 2.23 to type int

int(2.23)     # 2
float(40)     # 40.0

✨ Operator Overloading

Python supports operator overloading:

'code' + 'blocks'  # Outputs: 'codeblocks'

This is a form of operator overloading. Python figures out the types of the operands (str in this case), and performs an operation accordingly. If they were numbers, it would add; if strings, it concatenates. This makes Python elegant but also a bit magical—so be cautious and intentional.

Comma-Separated Values — Tuple Formation

When you use commas like this, Python returns a tuple. You'll see this pattern a lot—while unpacking variables, returning multiple values from functions, or processing API results. So keep this in mind:

x, y, z  # Outputs: (2, 3, 4)
x + 1, y * 2  # Outputs: (3, 6)

Decimal Precision and Rounding

result = 1 / 3.0
print(result)  # Outputs: 0.3333333333333333

Make sure both operands are either integers or floats, like 1.0 / 3.0 or 1 / 3. Decide how much precision you want to show, especially for user-facing outputs. We’ll cover formatting tricks in the string-handling blog post.

Difference Between repr, str, and print()

repr('python')  # Outputs: "'python'"
str('python')   # Outputs: 'python'
print('python') # Outputs: python
  • str() is for user-friendly output

  • repr() is for unambiguous, developer-facing output (often used in debugging)

  • print() just displays the content

Boolean Values Are Numbers

1 < 2  # True
5.0 == 5.0  # True
4.0 != 5.0  # True
x < y < z  # True

Python evaluates chains like x < y < z cleanly from left to right.

But be cautious:

1 == 2 < 3  # False

Why? Because Python reads it as (1 == 2) and (2 < 3). The first part is False, so the entire statement becomes False.

The math Module

import math
math.floor(3.5)   # Outputs: 3
math.floor(-3.5)  # Outputs: -4

floor() returns the largest integer less than or equal to the number.

math.trunc(2.8)    # Outputs: 2
math.trunc(-2.8)   # Outputs: -2

trunc() simply chops off the decimal part and moves towards 0.

Complex Numbers

(2 + 1j) * 3  # Outputs: (6 + 3j)

You might not use them daily in web development, but Python supports complex numbers natively.

Octal, Hex, and Binary

0o20    # Octal for 16
0xFF    # Hexadecimal for 255
0b100   # Binary for 4

Use built-ins to convert:

oct(16)   # '0o20'
hex(255)  # '0xFF'
bin(4)    # '0b100'

You can even convert from various bases:

int('64', 8)     # 52
int('64', 16)    # 100
int('100', 2)    # 4

Random Numbers

import random
random.random()  # e.g. 0.58937 (between 0 and 1)

Need a value between 1–10?

random.randint(1, 10)

Choose from a list:

arr = ["python", "javascript", "c++", "rust", "ruby on rails"]
random.choice(arr) //outputs: "javascript"

Shuffle an array:

random.shuffle(arr)

Decimal Arithmetic (Accurate Precision)

0.1 + 0.1 + 0.4
# Outputs: 0.6000000000000001

#if we do 
0.1 + 0.1 + 0.1 - 0.3
# Outputs: 5.551115123125783e-17

now this something we were not expecting, so what kind of maths is python applying here.

so decimal precision is a bit problematic, and we can import many things, so there is one more library called decimal

from decimal import Decimal
Decimal('0.1') + Decimal('0.1') + Decimal('0.1') - Decimal('0.3')
# Outputs: Decimal('0.0')

# In-contrast
0.1 + 0.1 + 0.1 - 0.3  # Outputs: 5.551115123125783e-17

This is due to how floating-point numbers are represented in binary. Decimal is your solution for precise financial or scientific calculations.

Fractions

from fractions import Fraction
Fraction(2, 7)  # Outputs: Fraction(2, 7)

This gives exact values, not approximations.

Sets as Numeric Types

now lets talk about another data type that is part of number itself and it is also categorized under number

setone = {1, 2, 3, 4}
setone & {3}  # Intersection => {3}
setone | {1, 3, 7}  # Union => {1, 2, 3, 4, 7}

# special
setone - {1, 2, 3, 4}  # Difference => set()
# shouldn't it should print empty parantheses "{}"

but we were expecting empty parentheses or something like this {}, but we got set() reason - because empty parentheses is considered as dictionary in python

Why not {}? Because:

type({})  # <class 'dict'>

Empty curly braces represents to a dictionary, not a set.

Wrapping Up

Numbers in Python aren't just for math—they're full of behavior, structure, and hidden details that make Python both powerful and flexible. Whether you're a beginner or just looking to understand Python more deeply, mastering these concepts will help you write better, cleaner, and more predictable code.

10
Subscribe to my newsletter

Read articles from Harsh Gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Harsh Gupta
Harsh Gupta

I am a CSE undergrad learning DevOps and sharing what I learn.