Is there a difference between 'and' and '&' with respect to python sets?

前端 未结 5 543
臣服心动
臣服心动 2020-12-06 13:28

I got very good help for question check if dictionary key has empty value . But I was wondering if there is a difference between and and & in p

5条回答
  •  生来不讨喜
    2020-12-06 14:00

    Yes and is a logical and whereas & is a bitwise and. See example -

    >>> 1 and 2
    2
    >>> 1 & 2
    0
    

    The first result is due to short circuiting. Python tests 1 and finds it true and returns the 2. But, the second part does 01 (Binary 1) & 10 (Binary 2) hence evaluating to 00 (1 & 0, 0 &1) , which is 0.

提交回复
热议问题