Python Glossary Русский

all Python 3Conditionalsconstruct

if-elif-else

branching

A cascade that picks one of several branches. It tests the conditions in turn and runs the first block whose condition is true.

Syntax

if условие1:
    ...
elif условие2:
    ...
else:
    ...

The checks run top to bottom and stop at the first true condition — the rest aren't even evaluated, so branch order matters. Put stricter conditions first: if "score >= 60" comes before "score >= 90", the broad branch swallows everything and the strict one never fires.

Examples

score = 75
if score >= 90:
    grade = "A"
elif score >= 75:
    grade = "B"
elif score >= 60:
    grade = "C"
else:
    grade = "F"
print(grade)  # → B — сработала первая истинная ветка
hour = 14
if hour < 12:
    print("утро")
elif hour < 18:
    print("день")  # → день
else:
    print("вечер")

See also

Open in the full glossarydocs.python.orgReport an inaccuracy