In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "python how to judge the first number and the second number", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "python how to judge the first number and the second number"!
Cmp = lambda a, b: 0 if a = = b else 1 if a > b else-1print (cmp (3)) print (cmp (2)) print (cmp (2)) out:1-10
The above code may be difficult to read directly, so let me break it down:
Def cmp2 (a, b): if a = = b:return 0else:if a > b:return 1else:return-1print (cmp2 (3,2)) print (cmp2 (2,3)) print (cmp2 (2,2)) out:1-10
You can see that the functions of the above two functions are exactly the same, and the first way of writing is much simpler!
Let's familiarize ourselves with this writing again:
0 if a = b else (1 if a > b else-1)
This statement is the structure of a ternary expression nested within a ternary expression, evaluated from left to right.
The ternary expression first looks for if, then finds if to judge a = = b (judgment element):
If this condition is met, the 0 to the left of if (result element 1) is returned as the result of the entire expression.
If the judgment condition is not satisfied, the ternary expression to the right of the judgment condition (result element 2) is returned.
There is still a ternary expression on the right. Continue to evaluate the ternary expression and find if judgment a > b (judgment element):
If this condition is met, the 1 to the left of if (result element 1) is returned as the result of the entire expression.
If the judgment condition is not satisfied, the-1 (result element 2) to the right of the judgment condition is returned as the operation result of the whole expression.
Python's ternary expression is very flexible, and you can learn from the above case that the result element of a ternary expression can be a ternary expression, that is, a ternary expression can be used to nest a ternary expression.
Let me add:
The judgment element, result element 1 and result element 2 of a ternary expression can all be expressions.
The examples are as follows:
Suppose you want to compare two numbers, unequal returns 1, equality returns 0.
Cmp = lambda a moment bju 1 if an else b else 0
Print (cmp (2p3))
Print (cmp (2jue 2))
You can see that the judgment condition is not a comparison operator! In fact, according to amurb, the value is not equal to 0 to return to the left (result element 1), equal to 0 to return to the right (result element 2).
Ternary expressions are very useful when all three elements can be completed with one expression.
Thank you for your reading, the above is the content of "how python judges the first number and the second number". After the study of this article, I believe you have a deeper understanding of how python judges the first number and the second number, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.