Expressions and Operators
Tython expressions are strict, predictable, and statically checked.
Primary Expressions
Supported primary expression forms:
- Literals:
1,3.14,true,false,none,"abc" - Identifiers:
count,user_name - Parenthesized expressions:
(a + b) - List literals:
[1, 2, 3] - Matrix literals via constructor:
Matrix([1, 2, 3]) - Calls:
add(1, 2) - Field access:
user.name - Indexing:
items[0] - Ternary expressions:
if cond: x else y
Operator Set
Unary
-xfor numericx(intorfloat)not xfor booleanx
+x is not supported.
Arithmetic
Supported:
+,-,*,/,//,%
Rules:
- Numeric operators require numeric operands.
+supportsstr + strconcatenation.- Implicit conversion is forbidden (
"a" + 1is invalid). /is float division.
Comparisons
Supported:
==,!=,<,<=,>,>=
Rules:
- Chained comparisons are not allowed (
1 < x < 10is invalid). is,is not,in,not inare not supported.x == noneis valid.
Boolean
Supported keyword operators:
and,or,not
All boolean operators require boolean operands.
Conditions
if and while conditions must be bool.
Valid:
Invalid:
Assignment
Assignment is statement-only.
Valid:
Invalid:
Expression Statements
Only call expressions are allowed as standalone statements.
Valid:
Invalid:
Ternary
Tython ternary syntax is:
Rules:
- Ternary condition must be
bool. - Ternary is expression-only (not a standalone statement).
- When embedded in operator expressions, parenthesize it.
Valid:
Invalid:
Indexing
Tython indexing is strict:
- zero-based
- index expression must be
int - negative indexes are rejected when statically known
- slicing is not supported (
items[1:3]is invalid)
See Matrix for Matrix-specific indexing, methods, and operator rules.
Precedence
Tython follows Python precedence for the supported operator subset.
Call / field / index bind tighter than arithmetic and comparison operators.