▶ Try Python

Python Numbers

int, float, complex — and the math that goes with them.

Three numeric types

Python
x = 42 # int — any size, no overflow y = 3.14 # float — IEEE 754 double (64-bit) z = 2 + 3j # complex — real + imaginary print(z.real, z.imag) # 2.0 3.0

Arithmetic operators

Python
10 + 3 # 13 — addition 10 - 3 # 7 — subtraction 10 * 3 # 30 — multiplication 10 / 3 # 3.3333... — TRUE division (always float) 10 // 3 # 3 — floor division (integer quotient) 10 % 3 # 1 — modulo (remainder) 2 ** 10 # 1024 — exponentiation -7 // 2 # -4 — floor division rounds toward -∞

Floating point gotcha

Python
0.1 + 0.2 # 0.30000000000000004 ← floating point! 0.1 + 0.2 == 0.3 # False # Use round() for display: round(0.1 + 0.2, 2) # 0.3 # Or decimal module for exact money arithmetic: from decimal import Decimal Decimal("0.1") + Decimal("0.2") # Decimal('0.3') — exact

Useful built-in math functions

Python
abs(-5) # 5 round(3.567, 2) # 3.57 max(1,2,9,4) # 9 min(1,2,9,4) # 1 sum([1,2,3,4]) # 10 pow(2, 8) # 256 (same as 2**8)

The math module

Python
import math math.sqrt(16) # 4.0 math.ceil(4.1) # 5 — round up math.floor(4.9) # 4 — round down math.log(100, 10) # 2.0 — log base 10 math.pi # 3.141592653589793 math.e # 2.718281828459045 math.factorial(5) # 120