What does += mean in Python?

后端 未结 6 496
被撕碎了的回忆
被撕碎了的回忆 2020-12-01 08:29

I see code like this for example in Python:

    if cnt > 0 and len(aStr) > 1:
        while cnt > 0:                  
            aStr = aStr[1:]+a         


        
6条回答
  •  抹茶落季
    2020-12-01 09:07

    a += b
    

    is in this case the same as

    a = a + b
    

    In this case cnt += 1 means that cnt is increased by one.

    Note that the code you pasted will loop indefinitely if cnt > 0 and len(aStr) > 1.

    Edit: quote Carl Meyer: ``[..] the answer is misleadingly mostly correct. There is a subtle but very significant difference between + and +=, see Bastien's answer.''.

提交回复
热议问题