Python Numeric Data Types
Handling numbers incredibly easy to understand not just how to use them, but how they work inside the computer.
1. Integers int
Integers are whole numbers (positive, negative, or zero) without any decimal point.
x = 10
y = -5
z = 0
print(type(x)) # <class 'int'>- Unlimited Precision –
- In languages like Java, an integer has a maximum size (e.g., up to 2 billion).
- If you go over, it crashes (Overflow Error).
- Python is different. Python integers automatically expand to use as much memory as your computer has.
- You can calculate $2^{1000}$ without any special libraries.
# This works perfectly in Python huge_number = 9999999999999999999999999999999
- Visual Separators _
- When writing code, use underscores to make large numbers readable.
- Python ignores them during execution.
salary = 10_000_000 # Same as 10000000 print(salary) # Output: 10000000
- Binary, Octal, & Hexadecimal
- Python understands more than just base-10 numbers.
- Binary (Base 2): Prefix 0b (e.g., 0b101 → 5)
- Octal (Base 8): Prefix 0o (e.g., 0o10 → 8)
- Hexadecimal (Base 16): Prefix 0x (e.g., 0xFF → 255)
- Python understands more than just base-10 numbers.
2. Floating Point Numbers float
A float is any number with a decimal point. Python implements them as 64-bit double-precision numbers (following the IEEE 754 standard).
price = 19.99
pi = 3.14159
val = 1.0 # Even with .0, this is a floatScientific Notation – For extremely large or small numbers, use e (exponent).
- 1.5e2 means 1.5 \times 10^2 = 150.0
- 3e-4 means 3 \times 10^{-4} = 0.0003
—
Computers store numbers in binary (0s and 1s). Some simple decimals (like 0.1) translate to infinite repeating fractions in binary. Because memory is limited, the computer chops off the end, leading to tiny errors.
The “0.1 + 0.2” Problem:
val = 0.1 + 0.2
print(val)
# Output is NOT 0.3
# Output: 0.30000000000000004
NEVER compare floats using ==.
- if 0.1 + 0.2 == 0.3: (This will fail)
- if abs((0.1 + 0.2) – 0.3) < 0.00001: (Check if they are “close enough”)
3. Complex Numbers complex
Python is one of the few languages with built-in support for complex numbers (Real + Imaginary).
- Syntax: Use j for the imaginary part (mathematics uses $i$, but engineering uses $j$).
z = 3 + 4j # 3 is Real, 4j is Imaginary
print(z.real) # 3.0
print(z.imag) # 4.0
print(z * 2) # (6+8j) -> Math just works4. Booleans bool
Booleans represent logical truth: True or False. They are essential for control flow (if/else).
Booleans are actually Integers
- True is stored as 1
- False is stored as 0
You can mathematically add them (though you shouldn’t, for code clarity):
print(True + True) # Output: 2
print(False * 100) # Output: 0Truthy vs. Falsy (The Golden Rule)
In Python, every object can be evaluated as True or False inside an if condition.
- Falsy (Evaluates to False):
- The number Zero: 0, 0.0, 0j
- Empty sequences: “”, [], (), {}
- The constants: None, False
- Truthy (Evaluates to True):
- Everything else! (e.g., -5, ” “, [0])
# Practical Example
username = ""
if username:
print(f"Welcome {username}")
else:
print("Username is empty!") # This runs because "" is Falsy5. The None Type
None is a special data type called NoneType. It represents null, void, or nothingness.
- It is not 0.
- It is not False.
- It is not an empty string “”.
- It is simply… nothing.
Best Practice:
When checking for None, always use is instead of ==.
- correct if variable is None:
- wrong if variable == None:
6. Type Casting (Conversion)
You can convert one type to another explicitly.
| Function | Description | Example | Result |
| int() | Converts to Integer | int(3.99) | 3 (Truncates decimal) |
| float() | Converts to Float | float(5) | 5.0 |
| str() | Converts to String | str(10) | “10” |
| bool() | Converts to Boolean | bool(0) | False |
Warning: You cannot convert a “word” into a number directly.
int(“Hello”) → ValueError (Crash!)
7. Mathematical Functions
Python has built-in functions for number manipulation.
- abs(-5) → 5 (Absolute/Positive value)
- pow(2, 3) → 8 ($2^3$)
- round(3.7) → 4 (Nearest whole number)
- round(3.14159, 2) → 3.14 (Round to 2 decimals)
Mastery Summary Table
| Type | Examples | Key Characteristic |
| int | 10, -5, 1_000 | Whole numbers. Unlimited size. |
| float | 10.5, 1e5, 0.001 | Decimal numbers. Precision issues exist. |
| complex | 2+3j | Scientific numbers. |
| bool | True, False | True=1, False=0. Check Truthy/Falsy rules. |
| None | None | Represents absence of value. Use is None to check. |