Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Because NumPy allocates contiguous memory buffers, it cannot store dynamic arbitrary types in individual cells. Instead, it relies on fixed-width, low-level C-compatible data types known as dtypes. Understanding dtypes is vital for preventing integer overflows and optimizing memory consumption.
NumPy offers granular control over bit-width and signedness:
| Category | Dtype Names | Bit Width | Permitted Range / Value |
|---|---|---|---|
| Integer (Signed) | int8, int16, int32, int64 | 8, 16, 32, 64 bits | Negative and positive whole numbers (int8: -128 to 127) |
| Integer (Unsigned) | uint8, uint16, uint32, uint64 | 8, 16, 32, 64 bits | Non-negative integers (uint8: 0 to 255; ideal for pixels!) |
| Floating Point | float16, float32, float64 | 16, 32, 64 bits | Real numbers with fractional decimals |
| Complex | complex64, complex128 | 64, 128 bits | Numbers with real and imaginary components ($3 + 4j$) |
| Boolean | bool or bool_ | 8 bits (1 byte) | True or False |
| String / Unicode | str_ or U<N> | Variable | Fixed-width Unicode characters |
To convert an existing array from one data type to another, use the astype() method.
astype() always creates and returns a NEW deep copy of the array in memory; it never mutates the original array in-place!When using small fixed-width integer types (like int8 or uint8), exceeding the maximum boundary value wraps around without warning:
In financial calculations or large aggregates, always use int64 or float64 to avoid silent arithmetic overflow!
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Test your understanding of this lesson with 5 questions. Each question has one correct answer.