Skip to content
Discussion options

You must be logged in to vote

Thanks for the question! Instead of x < 3 and x > 1, you should use (x < 3) & (x > 1). The reason this is required is that there is no way to overload the and operator in Python, so we follow NumPy's convention of using bitwise & for element-wise boolean logic.

Note that due to Python's operator precedence conventions, the parentheses in the expression above cannot be omitted.

Similarly, in this context you would use | instead of or, ~ instead of not, and as a bonus you get ^ for XOR, which has no python keyword equivalent.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@ToshiyukiBandai
Comment options

@patrick-kidger
Comment options

Answer selected by ToshiyukiBandai
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants