short-circuit evaluation
Stopping the evaluation of and or or as soon as the answer is certain, so the right-hand side may never run.
In more detail
False and anything is False, and True or anything is True, so Python skips the second part. That makes guards safe: len(s) > 0 and s[0] == "x" never indexes an empty string.
Related terms
- logical operator — An operator such as AND, OR or NOT that combines or reverses true-or-false conditions.
- condition — An expression that is tested for being true or false, such as weight > 50, to decide what a program does next.
Taught in
- Compound Boolean Expressions — Hands-on Python: Complete Video Course & Book
- More String Methods — Hands-on Python: Complete Video Course & Book
- Any Type As A Condition — Hands-on Python: Complete Video Course & Book
- Conditions The Exam Loves — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Control Flow Sprint — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep