Search

Number Type

Notice
The Python posts on this page are based on what I learned from the book Jump to Python, interpreted and organized in my own way. If you find any mistakes or inaccuracies, please feel free to let me know, I’ll review and correct them as soon as possible.
Most programming languages, including Python, have data types, which are the fundamental way of classifying what kind of value a piece of data represents.
Among them, the Number Type is the most basic, it serves as the foundation of all calculations and logical operations.
In this section, we’ll explore how numbers are declared in Python
and how they work under the hood.
The main numeric types in Python are Integer, Float, Octal, and
Hexadecimal.

How to Create and Use Numbers

Integer

An integer represents a number without a decimal point,
such as 10, -5, or 0.
In Python, simply assigning a number to a variable automatically
creates an integer value.
a = 10 b = -5 c = 0 print(a, b, c) # 10 -5 0 print(type(a)) # <class 'int'>
Python
복사
Unlike many other languages, Python’s int type has no fixed size limit.
This means it can handle extremely large numbers without overflow, Python automatically allocates more memory when needed.
Integers are used in almost every part of a program for loop counters, indexes, arithmetic logic, and more. They form the backbone of most logical structures in programming.

Float

A float represents a number that contains a decimal point, such as 3.14, -0.001, or 1.0. Python automatically treats these as values of type float.
a = 3.14 b = -0.001 c = 1.0 print(a, b, c) # 3.14 -0.001 1.0 print(type(b)) # <class 'float'>
Python
복사
Floats in Python are stored using the floating-point representation,
which expresses decimal values as approximations in binary (base 2).
This allows for a very wide range of numbers, but it also introduces a subtle limitation, not all decimal values can be represented exactly in binary.
For example, 0.1 and 0.2 cannot be expressed precisely in base 2.
When converted to binary, they become infinitely repeating fractions.
As a result, Python stores the closest possible approximation.
print(0.1 + 0.2) # 0.30000000000000004 print(0.1 + 0.2 == 0.3) # False
Python
복사
At first glance, this might seem like a bug, but it’s actually a universal limitation of floating-point arithmetic in all computer systems, not just Python.
The computer doesn’t make a mistake, it simply cannot represent some decimal values exactly in binary.
To minimize this issue, you can use the round() function
or the decimal module for higher precision
from decimal import Decimal print(round(0.1 + 0.2, 2)) # 0.3 print(Decimal('0.1') + Decimal('0.2') == Decimal('0.3')) # True
Python
복사
In most real-world cases, this tiny rounding error is negligible,
but when working with financial data or high-precision calculations,
it’s important to use decimal or fractions to ensure accurate numerical results.

Octal and Hexadecimal

Numbers can be represented in different bases (or “radices”). While humans commonly use base 10, computers work internally with base 2 (binary).
To represent certain values more compactly, Python also supports octal (base 8) and hexadecimal (base 16) notation.
To write these numbers, prefix them with 0o for octal
and 0x for hexadecimal.
a = 0o12 # Octal (equals 10 in decimal) b = 0xA # Hexadecimal (equals 10 in decimal) print(a, b) # 10 10
Python
복사
You can also use the int() function
to convert strings of different bases into decimal integers.
print(int('0xff', 16)) # 255 print(int('0o77', 8)) # 63
Python
복사
Working with number systems isn’t just theoretical, it appears everywhere in programming, memory addresses, color codes, file permissions, and even network packet structures rely on base 8 or base 16.
Understanding these representations helps you see how computers interpret data beneath the surface, how they translate abstract numbers into structured, logical meaning.

Operators for Using Number Types

Now that we understand how numbers are represented in Python, let’s explore how they are used and manipulated through operators.
Operators are symbols that tell Python to perform specific mathematical or logical operations.
They form the core of all computational logic from simple arithmetic to complex algorithms.

Arithmetic Operators

Python supports the basic four arithmetic operators, addition, subtraction, multiplication, and division.
a = 7 b = 3 print(a + b) # 10 print(a - b) # 4 print(a * b) # 21 print(a / b) # 2.3333333333333335
Python
복사
The +, -, and * operators behave just as expected.
However, one key difference is that the / operator always returns a float, even when the result could be expressed as an integer.
For example, 6 / 3 will return 2.0 rather than 2. This ensures consistency in division operations, especially when dealing with non-integer results.

Exponentiation Operator *

The double asterisk ** is used to represent exponentiation — raising a number to a certain power.
print(2 ** 3) # 8 print(5 ** 2) # 25
Python
복사
This operator is especially useful in mathematical calculations, such as squaring, cubing, or computing roots (x ** 0.5 for the square root).
While simple, it forms the backbone of formulas, geometry, and numerical analysis in code.

Modulo Operator %

The modulo operator % returns the remainder of a division.
print(7 % 3) # 1 print(10 % 2) # 0
Python
복사
It’s commonly used for checking even or odd numbers, or for controlling repetitive patterns such as loops or periodic behaviors.
if n % 2 == 0: print("Even") else: print("Odd")
Python
복사
Modulo arithmetic is a surprisingly deep concept, it underlies things like hash functions, encryption, and time-based calculations.

Floor Division Operator //

While / performs normal division, the // operator performs floor division, returning only the integer part of the result.
print(7 // 3) # 2 print(10 // 2) # 5
Python
복사
This means any decimal portion is discarded (not rounded). Floor division is particularly useful when working with array indexes, grid positions, or any scenario where only whole-number results make sense.

Compound Assignment Operators

Compound assignment operators combine an operation and assignment into one step.
a = 10 a += 5 # a = a + 5 a *= 2 # a = a * 2 print(a) # 30
Python
복사
These operators, such as +=, -=, *=, /=, //=, and %= make code shorter and often more readable.
They also help emphasize the idea that programming is about state change each operation transforms the value of a variable into a new one.