1

I know I can use assignment operators with arithmetic operators in Python, for example:

x = 0x8
x |= 0x1  # x equals 9

I'd like to know if this is also possible with logical operators, for example something like:

x = 2 > 3  # False
y = 4 > 3  # True
x or= y  # x equals True

Is there something similar to that =or operation that I can use?

16
  • 1
    @bipll true, but the logical operators are not limited to int or bool types only. I can also use [] or {0+1j} Commented Jul 10, 2018 at 12:10
  • 1
    If you're certain the arguments will always be booleans, x |= y is functionally identical to your proposed x =or y statement. Commented Jul 10, 2018 at 12:11
  • 1
    Ok, that's a reasonable requirement. Just making sure we're all on the same page :-) Commented Jul 10, 2018 at 12:13
  • 1
    @bipli you shouldn't assume that either my operands nor the operation result is a boolean, in the example I presented in the comments my operands are a list and a dictionary, and the result is a dictionary Commented Jul 10, 2018 at 12:26
  • 1
    @scharette I'll maybe edit it later, but as the and and or operators are defined for many types, so should the answer be Commented Jul 10, 2018 at 12:27

1 Answer 1

1

what version of python are you in? python 3.6.5 can deal with x = False; x |= True, that yields True for x afterwards.

Sign up to request clarification or add additional context in comments.

2 Comments

So just x = True, then? If the OP wanted to set x to a specific value (True), they wouldn't need to use any operators, would they?
well, x|=y works in the same way - maybe i don't quite understand the significance of his question, but it looks to me like op wants to have the equivalent to x += 4 instead of x = x + 4 for logical operations

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.