In addition to integers (int) and floating-point numbers (float), Python supports several other numeric types, particularly through the use of libraries like NumPy. Here are some additional numeric types:
1. Complex Numbers (complex)
- Definition: Numbers that have a real part and an imaginary part, represented as
a + bj, whereais the real part andbis the imaginary part. - Example:
z = 3 + 4j print(z) # Output: (3+4j) print(type(z)) # Output: <class 'complex'>
2. NumPy Numeric Types
When using the NumPy library, you have access to a wider range of numeric types, including:
numpy.int32,numpy.int64: Integer types with fixed sizes (32-bit or 64-bit).numpy.float32,numpy.float64: Floating-point types with fixed sizes (32-bit or 64-bit).numpy.uint8,numpy.uint16,numpy.uint32,numpy.uint64: Unsigned integer types that only represent non-negative values.
Example with NumPy
import numpy as np
# Creating NumPy arrays with different numeric types
arr_int = np.array([1, 2, 3], dtype=np.int32)
arr_float = np.array([1.0, 2.0, 3.0], dtype=np.float64)
print(arr_int.dtype) # Output: int32
print(arr_float.dtype) # Output: float64
Summary
- Complex Numbers: Useful in fields like engineering and physics.
- NumPy Types: Provide more control over memory usage and performance for large datasets.
For further exploration, consider checking out NumPy documentation or relevant labs on LabEx that focus on numeric types and their applications!
