Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in OR condition which side evaluate first in python?

if cpu_expensive_condition() or simple_condition():
        do_something()

out of two condition in OR statement in above python code which will be evaluate first ? , and is it compulsory that both will be evaluate ?

like image 916
iamgopal Avatar asked Jul 23 '12 12:07

iamgopal


People also ask

Does Python evaluate from left to right?

Order of Evaluation In Python, the left operand is always evaluated before the right operand. That also applies to function arguments. Python uses short circuiting when evaluating expressions involving the and or or operators.

Which is evaluated first and or or Python?

In an expression, Python interpreter evaluates operators with higher precedence first. And, except the exponent operator (**) all other operators get evaluated from left to right.

How are Python if statements evaluated?

The left clause will be evaluated first, and then the right one only if the first one is False . Without it breaking. Conversely, with an and clause, the right clause will only be evaluated if the first one is True : if person and person.name: # ...

What is conditional expression in Python?

Ternary operators are also known as conditional expressions are operators that evaluate something based on a condition being true or false. It was added to Python in version 2.5. It simply allows testing a condition in a single line replacing the multiline if-else making the code compact.


Video Answer


1 Answers

The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.

Quoted from Python Language Reference

like image 156
SingleNegationElimination Avatar answered Oct 20 '22 01:10

SingleNegationElimination