In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use and and or in Python". In daily operation, I believe many people have doubts about how to use and and or in Python. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use and and or in Python". Next, please follow the editor to study!
1. And
In Python, and and or perform Boolean logical calculus as you would expect, but they do not return Boolean values; instead, they return one of the values they actually compare.
For example:
123456 >'a 'and' b''b'> 'and' b''>'a 'and' b 'and' c'c'
Calculates the value of an expression from left to right in a Boolean context, and and returns the last value if all values in the Boolean context are true.
If a value in the Boolean context is false, and returns the first false value
2. Or:
12345678 >'a'or'b''a'> >''or'b''> >''or [] or {} {} > 0 or'a'or'c 'a'
When using or, values are evaluated from left to right in a Boolean context, just like and. If a value is true, or immediately returns that value
If all values are false, or returns the last false value
Note that or performs expression calculus in the Boolean context until the first true value is found, and then ignores the remaining comparison values
3. And-or:
And-or combines the previous two grammars and can be inferred.
1234567891011 > > a = 'first' > b =' second' > 1 and an or b 'first' > (1 and a) or b' first' > 0 and an or b 'second' > (0 and a) or b' second' >
This syntax looks similar to bool in C? A: B expression. The entire expression calculates from left to right, so the and expression is evaluated first. 1 and 'first' evaluates to' first', then 'first' or' second' calculates to 'first'.
0 and 'first' evaluates to False, then 0 or' second' evaluates to 'second'.
And-or is mainly used to imitate the ternary operator bool?a:b, that is, when the expression bool is true, take an or b.
And-or skill, bool and an or b expression, when a's value in Boolean context is false, it will not be like C language expression bool? A: B work like that.
IV. Safe use of and-or
one
two
three
four
five
six
> a = "" > b = "second" > (1 and [a] or [b]) ['] > (1 and [a] or [b]) [0]'>
Since [a] is a non-empty list, it can never be false. Even if an is 0 or''or other false values, the list [a] is true because it has an element.
A responsible programmer should encapsulate the and-or technique into a function:
one
two
3def choose (bool,a,b): return (bool and [a] or [b]) [0] print choose (1,', 'second') #' this ends the study of "how to use and and or in Python", hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.