Python a &= b meaning?

前端 未结 5 1404
萌比男神i
萌比男神i 2020-12-31 10:55

What does the &= operator mean in Python, and can you give me a working example?

I am trying to understand the __iand__ operator.

I just d

5条回答
  •  一个人的身影
    2020-12-31 11:31

    to put it in simple terms. Under the hood it does bit-wise binary operation.

    for example 5 in binary is 0101 and 3 in binary is 0011

    now do "And" operation between them (when both are 1 the result is one, 0 otherwise) and you will get binary 0001 which means 1 in decimal.

    x = 5
    x &= 3
    print(x)
    output >>> 1
    

提交回复
热议问题