Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Keyword elif

Test an alternative condition only after all earlier if and elif conditions were false.

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

Syntax

if test_a:
    ...
elif test_b:
    ...

Test an alternative condition only after all earlier if and elif conditions were false.

As soon as one condition is true its suite runs and subsequent conditions are skipped. An optional else covers the all-false path.

This ordered selection matters when conditions overlap or have side effects.

Examples

def classify(n):
    tested = []
    def check(name, result):
        tested.append(name)
        return result
    if check("low", n < 0):
        label = "low"
    elif check("even", n % 2 == 0):
        label = "even"
    elif check("high", n > 5):
        label = "high"
    else:
        label = "other"
    return label, tested
print(classify(8), classify(3))

Result

('even', ['low', 'even']) ('other', ['low', 'even', 'high'])

This ordered selection matters when conditions overlap or have side effects.

Sources