Arithmetic in Python - 1

Arithmetic in Python: A Practical Guide (with Libraries)

Arithmetic in Python — From Basics to Powerful Libraries

Learn core operators, precedence, practical examples, and the most useful Python libraries for exact and advanced arithmetic: math, decimal, fractions, NumPy, and SymPy.

1) Introduction

Arithmetic in Python covers addition, subtraction, multiplication, division, powers, remainders, and more. Python’s syntax is clean, and mathematics is built in—no special package needed to get started.

Tip: Use parentheses generously to make your intent clear and avoid precedence mistakes.

2) Arithmetic Operators

Basics

# Addition
a, b = 10, 5
print(a + b)   # 15

# Subtraction
print(a - b)   # 5

# Multiplication
print(a * b)   # 50

# True division (float)
print(a / b)   # 2.0

# Floor division (integer part)
print(a // b)  # 2

# Modulus (remainder)
print(a % b)   # 0

# Exponentiation (power)
print(a ** b)  # 100000

Notes

  • / always returns a float (e.g., 2.0).
  • // discards the fractional part.
  • % is perfect for divisibility checks (remainder 0 ⇒ divisible).
  • ** is power (e.g., 2**5 == 32).

3) Order of Operations (PEMDAS)

Python follows: Parentheses → Exponents → Multiplication/Division/Floor/ModAddition/Subtraction.

result = 10 + 5 * 2 ** 2
print(result)  # 30  (2**2 = 4; 5*4 = 20; 10+20 = 30)

# Use parentheses to be explicit
result2 = (10 + 5) * (2 ** 2)
print(result2)  # 60

4) Integers vs Floats (and Mixing)

# Integers
print(7 + 3)     # 10

# Floats
print(7.5 - 2.3) # 5.2

# Mixing int and float -> float result
print(5 + 2.5)   # 7.5
Why it matters: Floats can introduce rounding errors (binary floating-point). For money or exact ratios, see decimal and fractions.

5) Practical Examples

5.1 Simple CLI Calculator

x = int(input("Enter first number: "))
y = int(input("Enter second number: "))

print("Sum:", x + y)
print("Difference:", x - y)
print("Product:", x * y)
print("Quotient:", x / y)
print("Remainder:", x % y)

5.2 Area of a Circle

radius = float(input("Enter radius: "))
area = 3.14159 * (radius ** 2)
print("Area of circle:", area)

6) Useful Libraries for Arithmetic

6.1 math — Standard Math Functions

Use when: you need square roots, powers, factorials, GCD, trigonometry, constants (pi, e), etc.

import math

print(math.sqrt(16))     # 4.0
print(math.pow(2, 5))    # 32.0 (float)
print(2 ** 5)            # 32   (Python operator)
print(math.factorial(5)) # 120
print(math.gcd(24, 36))  # 12

6.2 decimal — Exact Decimal Arithmetic

Use when: working with money or when float rounding errors are unacceptable.

from decimal import Decimal, getcontext

getcontext().prec = 5  # set precision (total significant digits)
a = Decimal('1.1')
b = Decimal('2.2')
print(a + b)  # 3.3 (exact)

6.3 fractions — Rational Numbers

Use when: you want exact arithmetic with ratios like 1/3 or 2/5 (no rounding).

from fractions import Fraction

f1 = Fraction(1, 3)
f2 = Fraction(2, 5)
print(f1 + f2)  # 11/15
print(f1 * f2)  # 2/15

6.4 numpy — Fast Vectorized Arithmetic

Use when: doing array/matrix math, data science, or performance-critical numeric work.

import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

print(a + b)  # [5 7 9]
print(a * b)  # [ 4 10 18]
print(a.dot(b))  # 32 (1*4 + 2*5 + 3*6)

6.5 sympy — Symbolic Mathematics

Use when: you need algebra, calculus, exact simplification, solving equations, symbolic differentiation/integration.

import sympy as sp

x = sp.Symbol('x')
expr = x**2 + 2*x + 1
print(sp.expand(expr))   # x**2 + 2*x + 1
print(sp.factor(expr))   # (x + 1)**2

solution = sp.solve(sp.Eq(expr, 0), x)
print(solution)          # [-1]

7) Quick Cheat-Sheet

  • Division: / → float, // → floor division.
  • Remainder: a % b (0 means “a is divisible by b”).
  • Power: a ** b.
  • Precision: Money ⇒ decimal, ratios ⇒ fractions.
  • Performance arrays: numpy.
  • Algebra/solving: sympy.

Jump to Operators   Jump to Libraries

Post a Comment

0 Comments

Me