简体   繁体   English

我们可以在 return 语句中使用逻辑运算符吗

[英]Can we use logical operators with the return statement

Can I use the not operator in this case and output the else as false using not operator在这种情况下我可以使用 not 运算符吗? output 使用 not 运算符可以将 else 设为 false

 # NEW TO PYTHON
 def is_positive(number):
  if number > 0:
    return bool(number)
  else:
    return not bool(number)

Yes you can.是的你可以。

But a better code would look like that:但是更好的代码应该是这样的:

def is_positive(number):
    return (number > 0)

Your code returns True if number == 0 , even if it enters the else section, since bool(number) always return True if number != 0 .如果number == 0 ,您的代码将返回True ,即使它进入else部分也是如此,因为bool(number)如果number != 0总是返回True If you want that, use >= instead of > .如果需要,请使用>=而不是>

In your code, you're calculating two boolean values, while one is enough.在您的代码中,您正在计算两个 boolean 值,而一个就足够了。 You're calculating a boolean number > 0 , and if it is True , you're calculating a new boolean that will always be True if the first condition is valid, and same for the else statement, so it's like writing:您正在计算一个 boolean number > 0 ,如果它是True ,那么您正在计算一个新的 boolean 如果第一个条件有效,它将始终为True ,对于else语句也是如此,所以它就像写:

def is_positive(number):
    if number > 0:
        return True
    else:
        return False

Now you can see more clearly that if number > 0 is True , it returns True , and if number > 0 is False , it returns False .现在您可以更清楚地看到,如果number > 0True ,则返回True ,如果number > 0False ,则返回False But number > 0 is by itself a boolean statement that evaluates to True and False .但是number > 0本身就是一个 boolean 语句,其计算结果为TrueFalse For example, if you know number > 0 , your code is equivalent to:例如,如果您知道number > 0 ,则您的代码等效于:

def is_positive(number):
    if True:
        return True
    else:
        return False

And if not:如果不:

def is_positive(number):
    if False:
        return True
    else:
        return False

So as you can see, you can directly return number > 0 .如您所见,您可以直接返回number > 0

Yes.是的。 you can.你可以。 bool function will return true with any number except zero (even negative one). bool function 将返回除零以外的任何数字(甚至是负一)的true值。 But this code is not good.但是这段代码不好。 you can do it in many better ways.您可以通过许多更好的方式来做到这一点。 The best way is to do it like this.最好的方法是这样做。

def is_positive(number):
    return (number > 0)

If is a statement that does this: If the condition was true do this else do that. If 是执行此操作的语句:如果条件为真,则执行此操作,否则执行此操作。 In your code, it is like this: if the condition was true, return true else return false.在您的代码中,它是这样的:如果条件为真,则返回真,否则返回假。 So you can simply say return the answer of the condition instead of saying if it was true return true.所以你可以简单地说返回条件的答案,而不是说如果它是真的返回真。 if the number is positive, the answer to number > 0 will become true so it's simply like to say return true and if not, the answer to the condition will become false so it's like to say return false.如果数字为正,则number > 0的答案将变为真,因此就像说返回真,如果不是,则条件的答案将变为假,因此就像说返回假。 Actually, when you call this method, it will check the condition first.实际上,当您调用此方法时,它会先检查条件。 if it was true, it is replaced with true and if not, it is replaced with false.如果为真,则将其替换为真,否则,将其替换为假。

You cannot, Syntactically speaking: you can.你不能,从句法上讲:你可以。 the program will compile, However.该程序将编译,但是。 it will work differently than you think.它的工作方式与您想象的不同。

Every number except for a zero converted into a bool gives True .除了零转换为bool值之外的每个数字都给出True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM