Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

Short-Circuit Operators in Python

$
0
0

In python short circuiting is supported by various boolean operators and functions. By short circuiting we mean the stoppage of execution of boolean operation if the truth value of expression has been determined . The evaluation of expression takes place from left to right .

Using Boolean Operators

The chart given below gives an insight of the short circuiting of in case of boolean expressions. Boolean operators are ordered by ascending priority.


Short-Circuit Operators in Python

An expression containing and and or stops execution when the truth value of expression has been achieved . Evaluation takes place from left to right .

# python code to demonstrate short circuiting
# using and and or
# helper function
def check():
return "geeks"
# using an expression to demonstrate
# prints "geeks", and gets executed
# as both are required to check truth value
print (1 and check())
# using an expression to demonstrate
# prints 1
# as only if 1st value is true, or
# doesnt require call check() fun
print (1 or check())
# using an expression to demonstrate
# prints "geeks"
# the value returns true when check
# is encountered. 1 is not executed
print (0 or check() or 1)
# using an expression to demonstrate
# prints 1
# as last value is required to evaluate
# full expression because of "and"
print (0 or check() and 1)

Output:

geeks
1
geeks
1

Using all() and any()

Inbuilt functionsall() and any() in python also support short-circuiting. Example below would give you clear insight on how it works.

# python code to demonstrate short circuiting
# using all() and any()
# helper function
def check(i):
print ("geeks")
return i
# using all()
# stops execution when false occurs
# tells the compiler that even if one is
# false, all cannot be true, hence stop
# execution further.
# prints 3 "geeks"
print (all(check(i) for i in [1, 1, 0, 0, 3]))
print("\r")
# using any()
# stops execution when true occurs
# tells the compiler that even if one is
# true, expression is true, hence stop
# execution further.
# 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

Using conditional operators

Conditional operators also follow short circuiting as when expression result is obtained, further execution is not required.

# python code to demonstrate short circuiting
# using conditional operators
# helper function
def check(i):
print ("geeks")
return i
# using conditional expressions
# since 10 is not greater than 11
# further execution is not taken place
# to check for truth value.
print( 10 > 11 > check(3) )
print ("\r")
# using conditional expressions
# since 11 is greater than 10
# further execution is taken place
# to check for truth value.
# return true as 11 > 3
print( 10 < 11 > check(3) )
print ("\r")
# using conditional expressions
# since 11 is greater than 10
# further execution is taken place
# to check for truth value.
# return false as 11 < 12
print( 10 < 11 > check(12) )

Output:

False
geeks
True
geeks
False

This article is contributed by Manjeet Singh (S. Astha) . If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Viewing all articles
Browse latest Browse all 9596

Trending Articles