Dictionaries and dict.get()
Busque valores por chave. get() retorna um valor padrão em vez de um erro quando a chave não existe.
Sintaxe
stats = {"hp": 20}stats["hp"]stats.get("battery", 0)
- Atribuir a uma chave nova a adiciona.
Exemplo
stats = {"hp": 20}
print(stats["hp"])
print(stats.get("battery", 0))Saída:
20 0
Erros comuns
- stats["missing"] gera um KeyError. Use get() quando a chave puder estar ausente.