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

前端 未结 9 670
梦谈多话
梦谈多话 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 08:06

    -= is an operator. This operator is equals to subtraction.

    num -= 1 means is num = num - 1

    It is used to subtraction from the itself with given value in right side.

提交回复
热议问题