Seven ways to write Python conditional statements, how many do you know?

Some people say that Python is an easy language to get started, but a difficult language to master. I very much agree with this.

There are many (and more and more) advanced features in the Python language, which are very popular among Python enthusiasts. In the eyes of these people, those who can write advanced features that ordinary developers can't understand are masters and gods.

But you have to know that in teamwork, showing off skills is a taboo.

Why do you say that? Let me talk about my opinion:

The simpler the code and the clearer the logic, the less likely to make mistakes;
in teamwork, your code is not only maintained by you, and reducing the cost of others’ reading/understanding of the code logic is a
simple code with good moral character . Will use the most basic syntactic sugar, complex advanced features, and will have more dependencies (such as language versions)

In this series, I will summarize and list the dazzling skills I have seen. Let’s warm up today and write a very simple conditional judgment statement that makes people want to scold the streets. Here , If you are a Python enthusiast, you can learn some cool code writing skills, but learning is learning, I hope you use it in different scenarios.

Original code

This is a very simple code that judges whether a person is an adult based on age. Due to the number of lines of code, some people are reluctant to write this way, because it does not reflect their years of Python skills.

if age > 18:
    return "已成年"
else:
    return "未成年"

Below I have listed six variants of this code. One is 6 more than one. It is easier to understand if you put it out separately. If you put it in the engineering code, people who have never used these methods will definitely look confused. After understanding, I yelled inadvertently: Damn it, can you write like this? , And then I'm going to start cursing the street: Is this the code for people to see? (Except for the first type)

The first

grammar:

<on_true> if <condition> else <on_false> 

example

>>> age1 = 20
>>> age2 = 17
>>> 
>>> 
>>> msg1 = "已成年" if age1 > 18 else "未成年"
>>> print msg1
已成年
>>> 
>>> msg2 = "已成年" if age2 > 18 else "未成年"
>>> print msg2
未成年
>>> 

The second

grammar

<condition> and <on_true> or <on_false>

example

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
>>> msg1 = age1 > 18 and "已成年" or "未成年"
>>> msg2 = "已成年" if age2 > 18 else "未成年"
>>> 
>>> print(msg1)
已成年
>>> 
>>> print(msg2)
未成年

The third

grammar

(<on_true>, <on_false>)[condition]

example

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
>>> msg1 = ("未成年", "已成年")[age1 > 18]
>>> print(msg1)
已成年
>>> 
>>> 
>>> msg2 = ("未成年", "已成年")[age2 > 18]
>>> print(msg2)
未成年

The fourth

grammar

(lambda: <on_false>, lambda:<on_true>)[<condition>]()

example

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
>>> msg1 = (lambda:"未成年", lambda:"已成年")[age1 > 18]()
>>> print(msg1)
已成年
>>> 
>>> msg2 = (lambda:"未成年", lambda:"已成年")[age2 > 18]()
>>> print(msg2)
未成年

The fifth

grammar:

{
    
    True: <on_true>, False: <on_false>}[<condition>]

example:

>>> msg1 = {
    
    True: "已成年", False: "未成年"}[age1 > 18]
>>> print(msg1)
已成年
>>> 
>>> msg2 = {
    
    True: "已成年", False: "未成年"}[age2 > 18]
>>> print(msg2)
未成年

Sixth

grammar

((<condition>) and (<on_true>,) or (<on_false>,))[0]

example

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
>>> msg1 = ((age1 > 18) and ("已成年",) or ("未成年",))[0]
>>> print(msg1)
已成年
>>> 
>>> msg2 = ((age2 > 18) and ("已成年",) or ("未成年",))[0]
>>> print(msg2)
未成年

The above code is relatively simple and can be understood by looking carefully, I won't explain it.

Seeing this, is there any posture? After learning Python for so long, and so many show operations, it is really a long time to see. . Among the six ways of writing, I recommend the first one, which I often use, concise and straightforward, with few lines of code. Although other writing methods can be written, they will not be used. I don't want to meet colleagues who will use these writing methods in public code for the rest of my life.

Guess you like

Origin blog.csdn.net/sinat_38682860/article/details/109312858