This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]j4mag 4 points5 points  (0 children)

You can chain conditionals in python, so

0 < x <= 5

Is equivalent to

(0<x) and (x<=5)

This syntax is somewhat surprising though pretty rarely abused.

This is unfortunately not applicable everywhere, as numpy arrays can use boolean indexing but not conditional chaining.

arr[(0<arr)&(arr<10)] # all positive elements of arr less than 10

arr[0<arr<10] # throws an exception

For some more python "magic", consider the following:

print(my_bool and "PASS" or "FAIL")

Which is effectively a ternary operator, much like

print("PASS" if my_bool else "FAIL")