Átomo
13If
Controle de fluxo · Python

if / elif / else

Abrir na tabela

Definição

Executa blocos conforme condições. elif encadeia alternativas; else cobre o resto. Em uma linha, use a expressão condicional.

Exemplos

1. Cadeia de condições
if amount > 1000:
    tier = "high"
elif amount > 100:
    tier = "medium"
else:
    tier = "low"
2. Condição composta
if status == "paid" and not refunded:
    ship(order)
if region in ("BR", "AR") or amount == 0:
    skip(order)
3. Inline: expressão condicional
tier = "high" if amount > 1000 else "low"
label = f"{customer} ({'vip' if vip else 'regular'})"

No mesmo grupo