Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Expressions and operators

Operator precedence determines expression grouping, and parentheses can override it.

Version: Python 3.14; repository image, production runtime not attested

Syntax

value = 2 + 3 * 4
selected = left or right

Operator precedence determines expression grouping, and parentheses can override it.

Multiplication binds before addition, giving 14 and 20. or returns the first truthy operand or the last one evaluated, so the result can be a string. A chained comparison evaluates its middle expression only once.

mark sees the empty string and fallback, then sees 2 just once in the separate comparison.

assert 2**3**2==512
assert -2**2==-4 and (-2)**2==4
events=[]
assert ('ready' or events.append(1))=='ready' and not events
assert not (3<2<events.append(2)) and not events

Examples

seen = []
def mark(x):
    seen.append(x)
    return x

print(2 + 3 * 4, (2 + 3) * 4)
print(mark("") or mark("fallback"), seen)
seen.clear()
print(1 < mark(2) < 3, seen)

Result

14 20
fallback ['', 'fallback']
True [2]

mark sees the empty string and fallback, then sees 2 just once in the separate comparison.

Sources