Home  >  Article  >  Backend Development  >  Tips for Python to return True or False

Tips for Python to return True or False

不言
不言forward
2018-11-23 17:09:1315328browse

The content of this article is about how to run python on notepad. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Tips for Python to return true or false values ​​(True or False). This article discusses the simplest way to write conditional judgment statements. This article gives two concise ways to write. Friends who need it can refer to it

The following code:

def isLen(strString):
    if len(strString)>6:
        return True
    else:
        return False

Perhaps you have discovered that in Python 3 there is actually a way to complete the function with only one line:

The code is as follows:

>>> def isLen(strString):
       return True if len(strString)>6 else False

But. . . Could it be simpler?

How to use Python to express conditional statements more easily, just for fun :)

One way is to use list index:

The code is as follows:

>>> def isLen(strString):
       #这里注意false和true的位置, 多谢网友@小王的指正
       return [False,True][len(strString)>6]

The principle is very simple. The Boolean value True is indexed and evaluated as 1, while False is equal to 0. Can it be simpler?

The above is the detailed content of Tips for Python to return True or False. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete