Short-Circuit Evaluation

say it: “SHORT SUR-kit ih-val-yoo-AY-shun” /ˈʃɔrt ˈsɜrkɪt ɪˌvæljuˈeɪʃən/

Python stops checking an and/or as soon as it knows the answer.

What is short-circuit evaluation?

Short-circuit evaluation means Python stops checking an and or or as soon as it knows the answer. You can use it to check something is safe before using it.

cart = []
# len(cart) > 0 is False, so cart[0] is never checked (no error)
if len(cart) > 0 and cart[0] == "gift card":
    print("Gift card first")

Level: Apprentice · More Python terms at this level

Updated

In other subjects