In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to understand Python and or short-circuit logic, concise and easy to understand, absolutely can make your eyes shine, through the detailed introduction of this article I hope you can gain something.
The short-circuit logic rules are as follows:
The expression operates from left to right. If the left logical value of or is True, all expressions after short circuit or (whether and or or) are directly output by the left expression of or. If the logical value on the left side of or is False, the expression on the right side of or is output, regardless of whether the subsequent expression is true or false, and the result of the entire expression is the result of the subsequent expression.
The expression is operated from left to right. If the left logical value of and is False, all subsequent and expressions are short-circuited until there is an or. The left expression of and is output to the left of or to participate in the next logical operation. If the logical value on the left side of and is True, then the expression after it is output. Whether the expression after it is true or false, the result of the whole expression is the result of the expression after it.
Short circuit logic cannot be used if the left side of or is False or the left side of and is True.
Note:
In Python, and has precedence over or, and both and or return values and do not convert to True or False. When not and and or are run together, the priority is not>and>or
In Python, None, 0 in any numeric type, empty string "", empty tuple (), empty list [], empty dictionary {} are treated as False, and custom types, if the__ nonzero __ () or__ len __ () methods are implemented and the method returns 0 or False, its instance is also treated as False, and all other objects are True.
Here is the simplest logical operation:
True and True ==> True True or True ==> True
True and False ==> False True or False ==> True
False and True ==> False False or True ==> True
False and False ==> False False or False ==> False
Let's take another concrete example:
Let's define a set of functions:
1> def a():
2> print ('A')
3> return 1
4> def b():
5> print ('B')
6> return 1
7> def c():
8> print ('C')
9> return []
10> def d():
11> print ('D')
12> return []
13> def e():
14> print ('E')
15> return 1
Example 1:
17> if a() and b() and c() and d() and e():
18> print ('ok')
#Show results as follows
A
A() is false, followed by and statements, all short circuits, and finally only returns the expression of a(). Remember that all expressions that are shorted are not printed. So, just print A here.
Example 2:
17> if a() and b() and c() and d() and e():
18> print ('ok')
#Show results as follows
A
B
C
Python executes a() from left to right first,a() returns a logical value of True, followed by an and statement, so it cannot be short-circuited, and then continues to perform logical operations with b(), a() and b() output the logical value of b() True, and then with c() for logical operations, b() and c() output the logical value of c() False, and then all are and statements, then all short-circuited, and finally only A B C is printed.
Example 3:
17> if a() or b() or c() or d() or e():
18> print ('ok')
#Show results as follows
A
Ok
#Show results as follows
A
ok
The logical value of a() is True, followed by an or statement, all short-circuited, and finally only A is printed, while the if statement is True, so an ok is printed.
Example 4:
17> if a() or b() or c() or d() or e():
18> print ('ok')
#Show results as follows
A
B
C
Ok
Python executes a() from left to right first,a() returns a logical value of False, followed by an or statement, so it cannot be short-circuited, then continues to perform logical operations with b(), a() or b() outputs the logical value False of b(), then performs logical operations with c(), b() or c() outputs the logical value True of c(), followed by an or statement, all short-circuited, and finally only A B C ok is printed.
Example 5:
26> if a() and b() and c() and d() or e() and f() or g() and h():
27> print ('ok')
#The output is as follows:
A
E
F
Ok
From left to right, first the logical value of a() is False, and then there are three and statements until the or statement: a() and b() and c() and d(), all of which are short-circuited. Only output a(), get a() or e() is True, output e() , get e() and F() is True , output f(), followed by or statement, then short circuit all after. Finally, only A E F OK was printed.
The above is how to understand the short-circuit logic of and and or operations in Python. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.