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

前端 未结 9 654
梦谈多话
梦谈多话 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:01

    You do not have to do anything, unless you are required to do something for your program to run correctly. Some things are good practice, but don't let anyone or anything but the compiler and the specification convince you that you have to do something one way or another. In this case, n -= 1 is exactly the same as n = n - 1. Therefore, if you do not wish to put the - before the =, then don't. Use n = n - 1 instead.

提交回复
热议问题