Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Operator and

Returns the first falsy operand or the last operand reached.

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

Syntax

left and right

Returns the first falsy operand or the last operand reached.

Use this operator as a guard before accessing a value that requires a nonempty container. The first operand is evaluated exactly once.

Truth testing uses the Boolean protocol or, in its absence, the length protocol. The returned result can be a list, string or number because the operator forwards the selected object.

The right expression is evaluated only if the left operand tests true.

Syntax

left and right

__bool__, __len__ · left and right

Worked example

0 and 1 / 0 → 0
3 and [] → []

Examples

print([] and 1 / 0)
print("ok" and 17)

Result

[]
17

The right expression is evaluated only if the left operand tests true.

Sources