Python Topics : Numeric Types
The Basics
MDAS - multiplication, division, addition, subtraction
division always returns a floating point number
to do floor (integer) division and get an integer result use the // operator
to calculate the remainder use the % operator
>>> 17 / 3
5.666666666666667
>>> 17 // 3  # floor division discards the fractional part
5
>>> 17 % 3  # the % operator returns the remainder of the division
2
use the ** operator to calculate powers
>>> 5 ** 2  # 5 squared
25
>>> 2 ** 7  # 2 to the power of 7
128
full support for floating pointmath
operators with mixed type operands convert the integer operand to floating point
>>> 4 * 3.75 - 1
14.0
in interactive mode, the last printed expression is assigned to the read-only variable _
>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06
Integers (int)
integers (int) are whole numbers that can be positive, negative, or zero
Python can handle integers of arbitrarily large size—limited only by the available system memory
positive_integer = 123
negative_integer = -456
Floating-point Numbers (float)
Floating-point numbers (float) represent real numbers and include decimal points
they are essential for representing fractional values and are used in virtually all types of numerical computations
the flexibility of float types comes with a trade-off in terms of precision
can introduce rounding errors in calculations due to the way computers represent these numbers in binary
real_number = 123.456
scientific_notation = 1.23456e2  # Equivalent to 123.456
Fractions
the Fraction type from the fractions module allows for the representation of numbers as fractions
makes it possible to perform exact arithmetic operations without the rounding errors associated with floats
useful in mathematical problems that require high precision over decimal representations
from fractions import Fraction

fraction_value = Fraction(1, 3)  # Represents 1/3
Decimal
the Decimal type from the decimal module is for apps which demand high precision
Decimal types maintain their accuracy by avoiding common rounding errors that occur due to the binary floating-point representation
from decimal import Decimal

decimal_value = Decimal('0.1')  # More accurate representation of 0.1
Complex Numbers
Complex numbers have both a real and an imaginary part
are crucial in various scientific fields
simplifies the handling of complex numbers through the complex type
allows for straightforward operations on these numbers
complex_number = 3 + 4j  # Real part: 3, Imaginary part: 4
Numpy
NumPy (Numerical Python) is a crucial package for numerical computing
the NumPy array is a powerful multidimensional array
allows for fast and flexible operations on large datasets essential for data science, specifically in array manipulation, mathematical operations, and statistical analysis
NumPy arrays are more efficient and compact than Python's built-in sequences
provides faster access for reading and writing items and making mathematical operations more convenient and efficient
element-wise matrix operations are slow and cumbersome with Python lists
element-wise matrix operations are much more efficient with NumPy arrays
import numpy as np

arr = np.array([1, 2, 3, 4, 5])

doubled_arr = arr * 2  # Efficient element-wise multiplication
NumPy offers a range of numeric datatypes
  • int32
  • int64
  • float32
  • float64
  • complex64
  • complex128
improves flexibility and efficiency in numerical computations
choosing the right datatype can greatly influence memory usage and computational speed
makes NumPy ideal for managing large datasets and high-precision calculations
by explicitly defining datatypes NumPy ensures efficient memory usage and optimal computational performance
# Creating arrays with specific datatypes
int_array = np.array([1, 2, 3, 4], dtype='int32')
float_array = np.array([1.1, 2.2, 3.3], dtype=np.float64)

# Performing operations with datatype considerations
result = int_array + float_array  # Result is upcasted to float64
index