In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how python uses ternary operators to carry out conditional assignment. It has certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let Xiaobian take you to understand it together.
Conditional assignment using ternary operators
The ternary operator is a shortcut to if-else statements, i.e. conditional operators:
[return value if expression is true] if [expression] else [return value if expression is false]
Here are a few examples you can use to make your code compact and concise. The following statement says,"If y is 9, assign x 10, otherwise assign x 20." We can also extend the chain of operations if necessary.
x = 10 if (y == 9) else 20
Similarly, we can do this with classes:
x = (classA if y == 1 else classB)(param1, param2)
In the example above classA and classB are two classes, one of which has a constructor called.
Here is another example of multiple conditional expressions chained together to calculate a minimum:
def small(a,b,c):returnaifa
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.