andExpert Examples

Logical AND operator; returns True if both operands are true

and bytecode: jump-if-false

How 'and' compiles to conditional jumps.

python
import dis

def and_expr(a, b):
    return a and b

def and_chain(a, b, c):
    return a and b and c

print("a and b:")
dis.dis(and_expr)
print("\na and b and c:")
dis.dis(and_chain)

# 'and' compiles to:
# 1. Evaluate left operand
# 2. COPY + POP_JUMP_IF_FALSE -> skip right operand
# 3. POP (discard left value)
# 4. Evaluate right operand
# This is why 'and' returns values, not just True/False

'and' compiles to a conditional jump that skips the right operand if the left is falsy. The left value stays on the stack as the result, which is why 'and' returns the actual value, not a boolean.

Want to try these examples interactively?

Open Expert Playground