Operators
Building reliable AI applications requires computing metrics, partitioning prompt tokens, verifying budgets, and combining logic guards. Python operators are the computational machinery that drives these decisions.
Arithmetic: Chunks, remainders, and powers.
In AI engineering, you constantly compute token counts, divide long documents into chunks for embedding, and scale model architecture parameters. Python provides a dedicated set of arithmetic operators to handle each scenario.
While addition (+), subtraction (-), and multiplication (*) behave like standard calculator math, division has three distinct forms that every AI practitioner must master:
ratio = 1350 / 500 # True division: always float 2.7
chunks = 1350 // 500 # Floor division: integer 2 full chunks
leftover = 1350 % 500 # Modulo: integer 350 tokens remaining
/) returns a float. For example, 10 / 2 evaluates to 5.0, not the integer 5.When partitioning a 1,350-token document into 500-token chunks for a RAG pipeline, floor division (//) tells you how many full chunks you have, and modulo (%) tells you how many leftover tokens must go into the final tail chunk:
1350 / 500 = 2.7Always gives a float. Represents the exact fractional ratio of chunks.
1350 // 500 = 2Counts complete chunks that fit into the total without any fraction.
1350 % 500 = 350Gives leftover tokens remaining after all full chunks are extracted.
(1350 // 500) * 500 + (1350 % 500) == 1350 evaluates to True. The full chunks and remainder perfectly reconstruct the original total.//) yields 2 full chunks, modulo (%) yields 350 leftover tokens, and true division (/) yields 2.7.Notice how floor division and modulo complement each other:
//gives the integer quotient (rounds towards negative infinity). If both operands are integers, the result is anint. If either operand is afloat(e.g.1350.0 // 500), Python returns a float (2.0).%gives the integer remainder left over after all full chunks are accounted for.(chunks * chunk_size) + leftover == totalis an exact identity that reconstructs the original document length.
Powers and dimensions: ** versus ^
In AI, model dimensions, vocabulary sizes, and context limits are routinely powers of two (e.g. 28 = 256, 210 = 1,024, 212 = 4,096).
In Python, the exponentiation operator is double asterisk (**):
context_limit = 2 ** 10 # 1024
2^10. In Python, the caret ^ is the bitwise XOR operator. Running 2 ^ 10 evaluates to 8, not 1024! Always use ** for exponentiation.
** scales powers (while ^ is bitwise XOR), and == compares values without mutating variables.Comparisons: Evaluating truth and thresholds.
AI scripts constantly evaluate thresholds before calling an API: Does the prompt fit in our token budget? Did the model return high confidence? Is the finish reason valid?
Comparison operators inspect values and return a bool (True or False):
within_budget = 1350 <= 1024 # False
has_tail = 350 > 0 # True
is_standard = 500 == 500 # True
=) assigns a value to a variable name. A double equals sign (==) compares two values and returns True or False. Writing chunk_size = 500 overwrites the variable, while chunk_size == 500 inspects it without changing it.Python provides six core comparison operators:
==(equal to) and!=(not equal to)<(strictly less than) and<=(less than or equal to)>(strictly greater than) and>=(greater than or equal to)
Boolean logic: Combining guards with and, or, not.
Real-world AI pipelines often require multiple conditions to be met simultaneously before making a costly LLM call. Python provides three logical operators: and, or, and not.
can_process_directly = (total_tokens <= 1024) and (total_tokens > 0)
needs_warning = (total_tokens > 1024) or (total_tokens <= 0)
is_safe = not (total_tokens > 1024)
Python evaluates logical operators using two fundamental principles:
- Short-circuit evaluation: In an
andexpression, if the left operand isFalse, Python immediately returnsFalsewithout evaluating the right operand. In anorexpression, if the left operand isTrue, Python immediately returnsTrue. - Grouping with parentheses: While Python has built-in precedence (
notbinds tighter thanand, which binds tighter thanor), production AI teams do not rely on memorizing precedence tables. Wrap each comparison in parentheses(...)so the logic is unambiguous to readers and debuggers.
Inspect operators in the AI pipeline sandbox.
Test different document sizes and observe how arithmetic, comparisons, and boolean guards behave interactively.
- Select the Division & Chunks tab. Observe that
1350 / 500gives2.7(float),1350 // 500gives2(int), and1350 % 500gives350(int). - Toggle Type: float to see how
//produces2.0(float) when given float operands. - Switch to Powers vs XOR. Compare
2 ** 10(1024) with2 ^ 10(8) to see why the caret is not a power operator. - Switch to Thresholds & Equality and verify that comparisons evaluate to
bool. - Switch to Boolean Guards to trace how step-by-step short-circuiting works with
and,or, andnot.
AI Pipeline Operator Sandbox
Interactive Lab1350 / 500 = 2.70Always returns a float, even if the division is perfectly exact.
1350 // 500 = 2Counts completed chunks. Both operands are int, so result is int 2.
1350 % 500 = 350Leftover tokens remaining in the partial final chunk.
remainder = 1350 % 500 # 350
Reconstruction identity: (2 * 500) + 350 == 1350 evaluates to True.
Check your understanding.
Take this with you.
Division in Python has three distinct tools: true division (/) always returns a float, floor division (//) calculates completed integer chunks, and modulo (%) isolates leftover remainders. For powers, always use **; never use ^ (which is bitwise XOR). Comparisons (==, !=, <=, etc.) produce booleans, and double equals (==) tests equality without modifying the variable. Group boolean guards with explicit parentheses (...) for readable and reliable code.