How to toggle a value in Python

后端 未结 17 1000
梦谈多话
梦谈多话 2020-12-07 07:46

What is the most efficient way to toggle between 0 and 1?

17条回答
  •  无人及你
    2020-12-07 08:30

    I use abs function, very useful on loops

    x = 1
    for y in range(0, 3):
        x = abs(x - 1)
    

    x will be 0.

提交回复
热议问题