Unlock Python’s Numeric Types: Integers, Floats, Complex & Memory Tricks
This article explains Python’s numeric types—including integers, floats, and complex numbers—covers their immutability, memory representation such as the small integer object pool, demonstrates type conversion functions, and introduces the math library for basic scientific calculations, all illustrated with clear code examples.
Introduction
Hello, I am a Go advanced learner sharing Python numeric type fundamentals.
1. Numeric Types
Numeric types store mathematical values and are immutable; assigning a new value creates a new object.
Relationship between Python variables and data types
Variables are references to objects; they have no type themselves. Objects such as 1, [2,3,4], "haha" have types.
a = 1 # create integer object 1
a = 2 # create integer object 2, a now points to 2The change is in the variable’s reference, not the object itself.
2. Python’s three numeric types
1. Integers (int)
Integers are whole numbers, positive or negative, without a decimal point. Python 3’s int can act as a long integer.
Examples: 1, 100, -8080, 0.
Hexadecimal uses 0x prefix, e.g., 0xff00; octal uses 0o prefix, e.g., 0o12.
Python’s integer size is 32 bits and typically allocated contiguously.
What is address space?
Address space refers to the memory occupied by any computer entity, including physical and virtual memory.
print(id(-2))
print(id(-1))
print(id(0))
print(id(1))
print(id(2))The addresses differ by 32 bytes because Python pre‑allocates a block for integer objects.
Small integer object pool
Python creates a pool of 262 integer objects ranging from -5 to 256, so these integers are reused without new allocation.
Verification:
print(id(-6))
print(id(-5))
print(id(-4))
print(id(255))
print(id(256))
print(id(257))IDs show the pool covers -5 to 256. Python also has an integer buffer that temporarily retains recently deleted large integers.
a = 1000000
print(id(a))
del a
b = 1000000
print(id(b))The same address indicates reuse from the buffer.
2. Floating‑point numbers (float)
Floats represent real numbers, e.g., 1.23, 3.14, -9.01, and can be written in scientific notation like 1.23e9.
3. Complex numbers (complex)
Complex numbers consist of a real and an imaginary part, expressed as a + bj or complex(a, b). The real and imaginary parts are floats.
Numeric type conversion
Python provides built‑in conversion functions:
int(x): convert to integer, truncating floats.
float(x): convert to float.
complex(x): convert to complex with zero imaginary part.
complex(x, y): convert to complex with specified real and imaginary parts.
Invalid conversions raise exceptions, e.g., int("haha").
a = 10.53
b = 23
print(int(a))
print(float(a))
print(complex(a))
print(complex(a, b))3. math library (mathematical calculations)
Import the math module for scientific functions such as abs(), exp(), fabs(), max(), min(), etc.
Common constants:
pi – mathematical constant π
e – natural constant e
Example usage:
import math
print(math.log(2))
print(math.cos(30))
print(math.cos(60))
print(math.sin(30))
print(math.sin(math.degrees(30)))
print(math.sin(math.radians(30)))4. Summary
The article detailed Python’s numeric types, explained integers, floats, and complex numbers, discussed memory aspects like the small integer pool and buffer, and showed type conversion and basic math library usage, providing practical code examples for better understanding.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
