Short-circuit methods in Python

Python Methods and Functions

Short-circuiting in boolean statements

The diagram below gives an idea of ​​short-circuiting in the case of boolean expressions. Boolean operators are ordered in ascending order of precedence.

or: When the interpreter Python looks at a or expression, it takes the first statement and checks if it's true. If the first statement is true, then Python returns the value of that object without checking for the second statement. The program doesn't bother with the second statement. If the first value is false, only then Python checks the second value and then the result is based on the second half. 
and: For the expression and Python uses a short-circuit method to check if the first statement is false, then the whole statement must be false, so it returns that value ... Only if the first value is true does it check the second statement and return the value. 
An expression containing and and or stops execution when the true value of the expression is reached. Evaluation occurs from left to right.

# Python code to demonstrate short-circuiting
# using both and or

 
# helper function

def check ():

return "geeks"

 
# use a demonstration expression
# prints geeks and executed
# since both are required for validation

print ( 1 and check () )

 

 
# use a demonstration expression
# prints 1
# as soon as if 1st value is true, or
# does not require checking of calls ()

print ( 1 or check ())

  
# using a demonstration expression
# prints geeks
# returns true when checked
# occurs. 1 not executed

print ( 0 or check () or 1 )

  
# use an expression for demonstration
# prints 1
# as the last value is required for evaluation
# full expression due to & quot; and & quot;

print ( 0 or check () and 1 )

Output:

 geeks 1 geeks 1 

Short closure in all () and l any ()

Built-in functions

# Python code to demonstrate short-circuiting
# using all () and any ()

 
# helper function

def check (i):

print ( "geeks" )

return i

 
# using all ()
# stops execution on false
# tells the compiler that even if
# is false, it can't be true, so stop
# do next.
# prints 3 geeks

print ( all (check (i) for i in [ 1 , 1 , 0 , 0 , 3 ]))

 

print ( "" )

< p>  
# using any ()
# stops executing when true
# tells the compiler that even if
# true, the expression is true so stop
# continue.
# prints 4 geeks

print ( any (check (i) for i in [ 0 , 0 , 0 , 1 , 3 ]))

Output:

 geeks geeks geeks False geeks geeks geeks geeks True 

Short-circuiting in conditional statements

Conditional statements are also short-circuited, because when receiving a result expression no further execution is required.

Output:

 False geeks True geeks False 

This article courtesy of Manjit Singh . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.





# Python code to demonstrate short-circuiting
# using conditionals operators

 
# helper function

def check (i):

  print ( "geeks" )

return i

 
# using conditional expressions
# from 10 to 11
# no further execution took place
# check the truth value.

print ( 10 & gt;  11 & gt; check ( 3 ))

 

print ( "" )

 
# using conditional expressions
# from 11 more than 10
# further execution
# check the truth value.
# return true as 11 & gt; 3

print ( 10 & lt; 11 & gt; check ( 3 ))

  

 

print ( " " )

  

 
# using conditional expressions
# from 11 more than 10
# further execution
# check the truth value.
# return false as 11 & lt; 12

print (  10 & lt;  11 & gt; check ( 12 ))