“num - 1” vs “num -= 1”

前端 未结 9 668
梦谈多话
梦谈多话 2021-01-23 07:18

In line 4 why do we have to add \"=\" after \"-\" ?

num = 5
if num > 2:
    print(num)
    num -= 1
print(num)
9条回答
  •  悲哀的现实
    2021-01-23 07:52

    The = is needed to assign the result of the subtraction back to num.

    The following:

    num -= 1
    

    subtracts one from num and assigns the result back to num.

    On the other hand, the following:

    num - 1
    

    subtracts one from num and discards the result.

提交回复
热议问题