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 introduces the relevant knowledge of "how to write Python conditional sentences". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
0. Original code
This is a very simple piece of code to judge whether a person is an adult by age. Because there are too many lines of code, some people are reluctant to write it, because it does not reflect their years of Python skills.
If age > 18: return "adult" else: return "minor"
Below, I list five variant writing methods of this code, one is better than the other, and it is easier to understand it alone. If you put it in the engineering code, people who have not used these methods will certainly look confused and then inadvertently shout: shit, you can still write like this, and then you have to start swearing: is this the code for people to see? (except for the first one)
1. First kind
Syntax:
If else
Examples
> age1 = 20 > age2 = 17 > msg1 = "mature" if age1 > 18 else "minor" > print msg1 has come of age > msg2 = "mature" if age2 > 18 else "minor" > print msg2 minor >
two。 The second kind
Grammar
And or
Examples
> > msg1 = age1 > 18 and "Adult" or "minor" > > msg2 = "mature" if age2 > 18 else "minor" > print (msg1) has come of age > print (msg2)
3. The third kind
Grammar
("false", "true") [condition]
Examples
> msg1 = ("underage", "underage") [age1 > 18] > print (msg1) underage > msg2 = ("underage", "underage") [age2 > 18] > print (msg2) underage
4. The fourth kind
Grammar
(lambda:, lambda:) [] ()
Examples
> msg1 = (lambda: "minor", lambda: "minor") [age1 > 18] () > print (msg1) has come of age > msg2 = (lambda: "minor", lambda: "minor") [age2 > 18] () > print (msg2) minor
5. The fifth kind
Syntax:
{True:, False:} []
Example:
> msg1 = {True: "adult", False: "minor"} [age1 > 18] > print (msg1) has come of age > msg2 = {True: "adult", False: "minor"} [age2 > 18] > print (msg2) minor
6. The sixth kind
Grammar
(() and (,) or (,)) [0]
Examples
> msg1 = ((age1 > 18) and ("minor",) or ("minor",) [0] > print (msg1) has come of age > msg2 = ((age2 > 18) and ("minor",) or ("minor",) [0] > print (msg2) minor "what is the way to write Python conditional statements" is introduced here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.