0

Few days ago, in my exam I had this question: 4. Rewrite the following code without if statement. The code should be single line. (without using any other loops)

If not flora and fauna:
      return True
else:
      return False

I could not answer for this question but still it seems very interesting/strange for me since I'm not pro in python. I have tried a lot of ways like extend() function but still don't know how to do this.

Any ideas or hints?

EDIT: I framed the question wrongly. Sorry for this. Since I do not have much experience I thought this problem can be solved only by making lists.

rd9911
  • 115
  • 1
  • 11

1 Answers1

6

Since you return True when the if is true, and False when it's false, the straightforward solution is to just return what the if is evaluating directly:

return not flora and fauna

Note that this might not return True or False, but rather a truthy or falsy value (e.g. for a falsy flora, the expression evaluates to whatever fauna is; if flora, fauna = 0, "Spam", the expression evaluates to "Spam", which is truthy, but not True), and fauna is "Spam". If you need a true True/False, just convert to bool:

return bool(not flora and fauna)

or use a conditional expression to achieve the same effect:

return True if not flora and fauna else False

or (we're getting into "playing around for fun territory now):

return not not (not flora and fauna)

or (distribute one not):

return not (flora or not fauna)

or (faster than bool, slower than not not, probably not worth the import though):

from operator import truth

return truth(not flora and fauna)

99+% of the time though, you don't need true True/False, so return not flora and fauna is really the way to go.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • Lol. I didn't know there's a truth operator – Mad Physicist Dec 10 '20 at 01:27
  • 1
    @MadPhysicist: Yup. And it's faster than `bool`, because `bool` is a constructor (that can't avoid some overhead and flexibility involved in constructors that `bool` doesn't benefit from since it always returns an existing object), while `operator.truth` is a simple function. The only real advantage to `bool` is you can call it without arguments (where `truth` requires exactly one positional argument). `bool` is faster than it used to be (they finally banned keyword arguments for it in 3.7, that lets it use much simpler argument parsing), but still slower than `truth`. – ShadowRanger Dec 10 '20 at 01:32
  • 1
    Just for comparison, on my Python x64 3.8.5 Alpine Linux install (running under WSLv2), in `ipython3`, `%%timeit x = False; from operator import truth as t` with a test body of `t(x)` runs in about 32 ns per loop; `%%timeit x = False; b = bool` with a test body of `b(x)` is about 69 ns per loop. Of course, using `not not x` gets it down to 20.5 ns, and using plain `x` is 11 ns, so the real answer is to not worry about `True`/`False` and just return the truthy/falsy thing. – ShadowRanger Dec 10 '20 at 01:43
  • If both are bools, you could also do `return flora < fauna`. – Kelly Bundy Dec 10 '20 at 01:44
  • @KellyBundy: Sure, but that's making pretty strict assumptions with little way to ensure that they're valid. You're not supposed to care about whether a given value is actually a `bool` (idiomatic code that actually needs `True`/`False` typed out in it is fairly uncommon), but that solution requires it, and silently misbehaves in many cases where the assumption is violated. – ShadowRanger Dec 10 '20 at 01:48
  • Well I wouldn't use it in those cases. And I wouldn't call it an assumption. When I write code, I *know* the possible types of my data. I probably wouldn't get much right if I didn't :-) – Kelly Bundy Dec 10 '20 at 01:58
  • @KellyBundy: I've [said it before](https://stackoverflow.com/questions/33161448/getting-only-element-from-a-single-element-list-in-python/33161467#comment54132042_33161448), and I'll say it again: "If I had a nickel for every time some 'known' behavior in production code turned out to be dead wrong... Well, I probably wouldn't be *rich*, but I'd be able to take the family out to a *really* nice dinner." I like my code to (as much as possible) either do the right thing silently all the time, or fail loudly when the assumptions are violated; I don't trust myself like you do. :-) – ShadowRanger Dec 10 '20 at 02:11