Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking up statements using the 'and' keyword [duplicate]

I am doing this following:

if ycoords[0] > 0 and ycoords[1] > 0 and ycoords[2] > 0:
    # do stuff

Can you shorten this code by doing something like:

if (ycoords[0] and ycoords[1] and ycoords[2]) > 0:
    # do stuff
like image 529
Edward Garemo Avatar asked Jan 20 '17 10:01

Edward Garemo


3 Answers

Yes, you could use all:

if all(x > 0 for x in ycoords):

or ycoords[:3] if ycoords has more than 3 elements.

like image 72
Jean-François Fabre Avatar answered Oct 30 '22 08:10

Jean-François Fabre


No, you can however simply use min to simplify the test syntactically:

if min(ycoords[0],ycoords[1],ycoords[2]) > 0:
    # do stuff

and given that ycoords exactly has three elements, even shorter:

if min(*ycoords) > 0:
    #do stuff

you can here, as @Tagc says, omit the asterisk (*):

if min(ycoords) > 0:
    #do stuff

but this will result in some overhead.

Another option is to use an all:

if all(x > 0 for x in [ycoords[0],ycoords[1],ycoords[2]]):
    # do stuff

or again, if ycoords contains only these three elements:

if all(x > 0 for x in ycoords):
    # do stuff
like image 36
Willem Van Onsem Avatar answered Oct 30 '22 07:10

Willem Van Onsem


Something that is not intuitive is that and:

"neither and nor or restrict the value and type they return to False and True, but rather return the last evaluated argument"

Just open a python terminal and do:

>> 4 and 3
3

Why is this important to take in account?

You might think that:

(ycoords[0] and ycoords[1] and ycoords[2]) > 0 

is equivalent to:

ycoords[0] > 0 and ycoords[1] > 0 and ycoords[2] > 0

or that is equivalent to:

(bool(ycoords[0]) and bool(ycoords[1]) and bool(ycoords[2])) > 0

but it's instead equivalent to:

ycoords[2] > 0

So, be careful because the interpreter is not doing what you think is doing.

like image 30
Sembei Norimaki Avatar answered Oct 30 '22 06:10

Sembei Norimaki